试图从另一个类访问变量

时间:2018-03-22 20:09:40

标签: c++

我有问题从我的原始类中获取我的变量以在另一个类方法中打印 我的问题的简单例子:

说变量是在test.h文件中声明的:

class player{
private:
int x = 10;
void setX(); //method for setting X from user input
int getX(); //Method for retrieving variable
}

然后在另一个我想要打印X的类方法

 class inTheWoods{
 public:
 printInfo();
 }

test.cpp文件:

void player::setX(){
cout << "Set X to a number:" << endl;
cin >> x
}

int player::getX(){
return x;
} 


int inTheWoods::printInfo(){
player playerObj; //Player object to access methods in player class
cout << playerObj.getX();
}

main.cpp中:

int main(){
inTheWoods woodsObj;
woodsObj.printInfo();
return 0;
}

每当我运行类似于这个问题的程序时,int都没有正确显示并抛出一个奇怪的负数。我希望这不是太多的代码,而且我记录了所有内容

2 个答案:

答案 0 :(得分:1)

如果您希望这些类位于单独的文件中,它仍然可以工作:

<强> Main.cpp的

#include <iostream>
#include "inTheWoods.h"


int main()
{
    inTheWoods woodsObj;
    woodsObj.printInfo();
    return 0;
}

<强> Player.h

#pragma once
#include <iostream>
class Player
{
    int x = 10;
    void setX();
public:
    int getX();
};

<强> Player.cpp

#include "Player.h"

void Player::setX()
{
    std::cout << "Set X to a number:" << std::endl;
    std::cin >> x;
}

int Player::getX()
{
    return x;
}

<强> inTheWoods.h

//#pragma once
#include "Player.h"
#include <iostream>

class inTheWoods
{
public:
    void printInfo();
};

<强> inTheWoods.cpp

#include "inTheWoods.h"

void inTheWoods::printInfo()
{
    Player playerObj; //Player object to access methods in player class
    std::cout << playerObj.getX();
}

#pragma once是一个预处理程序,可防止多个包含,以防您不知道。您可以选择跳过它(Visual Studio使用该行自动生成文件)。

总之,这些是我需要从您的实施中做出的更改:

  1. getX()的声明从private移至public
  2. 在每个班级的末尾添加分号。
  3. 将回复类型添加到printInfo()。如果您不希望函数返回任何内容,则返回类型为void。除非你关心声明constructor,这似乎不是这种情况。

答案 1 :(得分:1)

这是工作的。提供构造函数始终是一个好习惯,以便创建具有某些默认值的实例。以下代码可以根据您的要求运行:

<强>的main.cpp

#include <iostream>
#include "inTheWoods.h"

int main()
{
    inTheWoods woodsObj;
    woodsObj.printInfo();
    return 0;
}

<强> player.h

#pragma once

class player
{
private:
    int m_x;
public:
    player();
    player(const int& x);

    void setX();            //method for setting X from user input
    const int& getX()const; //Method for retrieving variable
};

<强> player.cpp

#include "player.h"
#include <iostream>

player::player()              // defualt constructor
    :m_x(0) {}

player::player(const int& x)
    :m_x(x) {}                // parameterised

void player::setX()
{
    std::cout << "Set X to a number:" << std::endl;
    std::cin >> m_x;
}

const int& player::getX()const
{
    return m_x;
}

<强> inTheWoods.h

#pragma once

class inTheWoods
{
public:
    inTheWoods();
    ~inTheWoods();
    void printInfo();
};

<强> inTheWoods.cpp

#include "inTheWoods.h"
#include "player.h"
#include <iostream>
inTheWoods::inTheWoods() {}

inTheWoods::~inTheWoods() {}

void inTheWoods::printInfo()
{
    //Player object to access methods in player class
    player playerObj;      // will beinitialized with 0
    player playerObj2(10); // will beinitialized with 10
    std::cout << playerObj.getX() <<std::endl;
    std::cout << playerObj2.getX() <<std::endl;
}

修改:如果您希望允许用户设置值,则printInfo()必须如下:

void inTheWoods::printInfo()
{
    //Player object to access methods in player class
    player playerObj;      // will beinitialized with 0
    playerObj.setX();
    std::cout << playerObj.getX() <<std::endl;
}