Click here to Skip to main content
15,917,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#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->

VB
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..
Posted
Updated 4-May-12 4:53am
v7
Comments
Sandeep Mewara 4-May-12 10:06am    
What problem?
Harmanjeet Singh 4-May-12 10:08am    
sir, i mentioned. could'nt access that variable "Data"

what about making the Data as static?

C++
class X
{
public:
   static uint32_t Data;
};



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)

C++
class Y : public X
{
public:
};
 
Share this answer
 
v3
Comments
Harmanjeet Singh 4-May-12 10:44am    
i tried, no use..
Harmanjeet Singh 4-May-12 11:06am    
its already public X
Static members of a class must be initialized:

Y Z::y;

int main()
{
    ...
 
Share this answer
 
Comments
Schehaider_Aymen 4-May-12 11:31am    
My Gosh !!! This one drove me crazy, i forgot that.
nv3 4-May-12 11:58am    
Happens to all of us once in a while :-)
Harmanjeet Singh 4-May-12 11:31am    
Really awesome Bro :) will give u max points as i can.. Thanx a lot! :)
:D
nv3 4-May-12 11:57am    
You are welcome, and thanks also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900