Problem in declaring static variable in the class

Hi all,

I want to declare static vector of user defined datatype to keep the values during the lifetime of the project.
My code is like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
computeChiSquare.h

#include <vector>
using namespace std;

struct resultBackup
{
	int var12, varConditional;
        bool test;
	double chiValue, pValue;
};

class computeChiSquare: public chiSequare
{

	
	
public:
	computeChiSquare( ...............);

         bool chi_square_test(int, int, std::vector <int>);
    
    static std::map<string, resultBackup> testValuesCashe;
protected:
	

};




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
computeChiSquare.cpp

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

using namespace std;

computeChiSquare( )
{
}


bool computeChiSquare::chi_square_test(int a, int b, std::vector<int> vars)
{
....

        std::map<string, resultBackup> computeChiSquare::testValuesCashe;
	
        ...
....
...
}


When i only declare static variable in header file there is no error. the program keeps running.

But when i use this variable in the cpp file then it always generate linking error.


1>Linking...
1>ComputeChiSquare.obj : error LNK2001: unresolved external symbol "public: static class std::vector<struct resultBackup,class std::allocator<struct resultBackup> > computeChiSquare::testValuesCash" (?testValuesCash@computeChiSquare@@2V?$vector@UresultBackup@@V?$allocator@UresultBackup@@@std@@@std@@A)
1>C:\ProBT_SLP_2.1\trunk_ProBT21\work\yasin\testMMHC\testMMHC\Debug\testMMHC.exe : fatal error LNK1120: 1 unresolved externals




Thanks in advance for helping.

Last edited on
You need to instantiate the static variable.

In addition to what you have there, also put this in your cpp file (not your header):

 
std::vector< resultBackup > computeChiSquare::testValuesCashe;

Thank you for your reply, can you give me little bit more detail.

1
2
std::vector< resultBackup > computeChiSquare::testValuesCashe;


also you have not put static key word.
You know how you have a function prototype, and then a separate function body?

Like... consider the following program:

1
2
3
4
5
6
void SomeFunction();

int main()
{
  SomeFunction();
}


That will compile OK, but because SomeFunction has no body, you'll get a linker error.


Static member vars are the same way. The declaration in the header is the "prototype", but you need to give them a "body", otherwise you get the linker error you're getting.

1
2
3
4
5
// in your header
class MyClass
{
  static int foo;  // the "prototype"
};
1
2
3
4
// in your cpp file
#include "myclass.h"

int MyClass::foo; // the "body".  Note:  no static keyword here 
But i am not declaring static function, i want to declare static variable.
 
std::vector< resultBackup > computeChiSquare::testValuesCashe;
sorry
1
2
static std::vector< resultBackup > testValuesCashe;

hello amanyasin,

like Disch said. your linker needs a 'body' for your declaration of 'testValuesCashe'.

So put that line std::vector< resultBackup > computeChiSquare::testValuesCashe; in your cpp and the linker will be happy.
Can't explain it any better than I already did:

1
2
3
4
5
6
7
8
9
10
11
12
// in your header
class MyClass
{
  static int foo;  // the "prototype"
};

// or in your case...

class computeChiSquare: public chiSequare
{
    static std::vector< resultBackup > testValuesCashe;
};
1
2
3
4
5
6
7
8
9
// in your cpp file
#include "myclass.h"

int MyClass::foo; // the "body".  Note:  no static keyword here 


// or in your case...

std::vector< resultBackup > computeChiSquare::testValuesCashe;



The "prototype" (ie: static int foo;) doesn't actually create the variable. It just tells the compiler that the variable exists somewhere. To use that variable, you need to instantiate it... or "give it a body". That is done by putting int MyClass::foo; in a cpp file.
Last edited on
Sorry

when i put like this it gives an error of redefinition of variable.
Disch is right.

Are you sure there is a static in the .hpp file but no static in the .cpp file?
Please post your corrected code so we can look at it.
Thank you, I changed the code in my first code and here is my errors.


error C2655: 'computeChiSquare::testValuesCashe' : definition or redeclaration illegal in current scope

error C2086: 'std::map<_Kty,_Ty> computeChiSquare::testValuesCashe' : redefinition
1> with
1> [
1> _Kty=std::string,
1> _Ty=resultBackup
1> ]
1> c:\.......\computechisquare.h(46) : see declaration of 'testValuesCashe'

Line 17 in your .cpp file is in the wrong place:

std::map<string, resultBackup> computeChiSquare::testValuesCashe;

When you put it there, you are attempting to allocate an automatic variable on the stack which disappears when you hit the closing brace on Line 22.

Instead, you need to put this line outside of any functions, right in the .cpp file itself.
For example, on empty Line 7.
thank you now it works
How did you do that, kfmfe04?

You had the line numbers and everything, but he never posted the source. o_O
Hehe, he just modified the code in the OP...
oh. Hahahah

Didn't notice
Topic archived. No new replies allowed.