1

I have a class Class1 in some header file Class1.hpp

 class Class1
 {
   static std::vector<bool> var1;
   func();
  }


func()
{
 var1.clear();
 int t=0;
 do
 {
   var1.push_back(t++);
 }while(true); //its some condition

Now inside another function main() in another class in another file, i am assigning var1 to another std::vector like:

 std::vector<bool> var2=Class1::var1;

When I am doing this its giving me the error:

undefined reference to Class1::var1

I am not getting what am I doing wrong. Can someone be kind enough to help in rectifying the error?

user1355603
  • 305
  • 2
  • 11
  • Your "another file"... presumably separately compiled? Does it see the Class1 definition you showed us? How? If not, how do you expect it to know of the existence of var1? – Ira Baxter Jun 08 '12 at 02:45

2 Answers2

3

The error you are getting is a linking error not a compilation error.
The linker tells you that it cannot find definition of Class1::var1

You just declared the vector member but did not define it.
Add:

std::vector<bool> Class1::var1;

to only one of your cpp files.


Good Read:
What is the difference between a definition and a declaration?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

Look a little more closely at your class definition. You'll notice it has no access specifiers.. which means all class access defaults to.. private.

Making something static does not imply that it is 'public'.

Richard K.
  • 171
  • 1
  • 3