在访问数据时遇到问题

在访问数据时遇到问题

问题描述:

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class X
{
public:
    int Data;
};

class Y : public X
{
public:
};

class Z
{
public:
    static Y y;
};

class myClass
{
public:
    void myRoutine()
    {
        cout<<Z::y.Data;
    }
};

int main()
{	
	myClass cls;
	cls.myRoutine();

	_getch();
	return 0;
}



此错误->



this Error->

1>experiment.obj : error LNK2001: unresolved external symbol "public: static class Y Z::y" (?y@Z@@2VY@@A)
1>C:\temp projs\experiment\Debug\experiment.exe : fatal error LNK1120: 1 unresolved externals




该代码不起作用.唐诺为什么..? :(

帮助我..




This code is not working. donno why..? :(

help me..

将数据设为静态有什么用?

what about making the Data as static?

class X
{
public:
   static uint32_t Data;
};




改进后,您必须在基类的基础上提及public. (If you do not choose an inheritance type, C++ defaults to private inheritance)




Improved, you have to mention public where inhering from the base class. (If you do not choose an inheritance type, C++ defaults to private inheritance)

class Y : public X
{
public:
};


类的静态成员必须初始化:

Static members of a class must be initialized:

Y Z::y;

int main()
{
    ...