我们只是说我们有两个类,A
和B
。
这是对他们俩的代码如何从其他类访问不同类中的变量?
class A
{
public:
int x;
};
class B
{
public:
int y;
void FindY() { y = x + 12; }
};
void something()
{
A fs;
B fd;
fs.x = 10;
fd.FindY();
}
的问题是,我想访问X,但我不想通过什么作为参数传递给我的功能我看着朋友和继承,但似乎都没有帮助我,纠正我,如果我错了。
一些如何我需要找到函数FindY()
中的x。
我正在与静态方法,但在我的情况下,我得到这个错误。
错误2错误LNK2001:无法解析的外部符号“公用:静态类std::vector<class GUIDialog *,class std::allocator<class GUIDialog *> > Window::SubMenu" ([email protected]@@[email protected]@@[email protected]@@@[email protected]@@[email protected]@A) C:\Users\Owner\documents\visual studio 2010\Projects\Monopoly\Monopoly\Window.obj
这是我如何宣称它
static vector<GUIDialog *> SubMenu;
我得到的,因为
SubMenu.resize(3);
`x`不无A`的`一个实例存在。你需要从* somewhere *中得到它。 – GManNickG 2010-12-22 09:35:57
您需要创建类“A”的对象,然后使用“A.x”。你在问那个笏还是我错过了什么? – Naveen 2010-12-22 09:36:01