Problems using public:static char

I just can't figure out what I'm doing wrong.I get a link error with this code:

star.h
1
2
3
4
5
6
7
const int ENTRY_SZ = 256;

class Star
{
public:
	static char file1Path_[ENTRY_SZ];
};

Main.cpp
1
2
3
4
5
6
7
8
#include <string>
#include "star.h"
const int STR_SZ = 256;//Maximum word length
using namespace std; 
void main()
{
strcpy(Star::file1Path_,"hello");
}


Error message

Main.obj : error LNK2001: unresolved external symbol "public: static char * Star::file1Path_" (?file1Path_@Star@@2PADA)
1>D:\Cis 162 project 3\Debug\Cis 162 project 3.exe : fatal error LNK1120: 1 unresolved externals


Thanks for any help
Static member variables need to be defined somewhere outside of the class, preferably in the corresponding .cpp file.

Try putting char Star::file1Path_[ENTRY_SZ]; in star.cpp

IIRC, having it in the .h file will work, but it can cause multiple definition errors if you include the header in more than one file. It will also work if you throw it into main.cpp, but that's probably not the best place for it.
Last edited on
You rock. Thank you!
Topic archived. No new replies allowed.