Undefined reference???

I don't understand this error on gcc, mingw.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// this stuff is in a separate file.
namespace prog
{
    enum progState 
    {
        goodState,
        badState,
        otherState,
    };
} // namespace

// class header file
namespace prog
{
    class Program
    {
       private:
         static progState ProgramState;
       public:
          Program();
         ~Program();
     
         void InitProgram();
         void ProgramGo();

    };
}// namespace


// the cpp file...
namespace prog
{
    Program::Program()
    {
       // set the state to good
       ProgramState = goodState;
       // do some init that might modify it.
       // all references to ProgamState are good in the constructor.
    }
    
    Program::~Program()
    {
       // clean up...
    }

    void Program::InitProgram()
    {
        if(ProgramState == goodState)
        {
            // this is where it gets messed up...
            // ProgramState is good to the compiler with nothing within this block
            // add any code to it like
            std::cout << "InitProgram" << std::endl;
            // and the compiler spits back that ProgramState is undefined reference
            // in any function of said class will result with the same error
        }
    }
    
    void Program::ProgramGo()
    {

        if(ProgramState == goodState)
        {
            // this is where it gets messed up...
            // ProgramState is good to the compiler with nothing within this block
            // add any code to it like
            std::cout << "ProgramGo" << std::endl;
            // and the compiler spits back that ProgramState is undefined reference
            // in any function of said class will result with the same error
        }
     }
} // namespace



This is driving me nuts when one way it compiles fine and then I modify the code with CodeLite I get the error. I don't know what's going on here. I have never seen this problem before. I think my logic is pretty sound here.

Can someone point me in a direction?
Try qualifying the static member with the class name.

I.E.:

if (Program::ProgramState == goodState)
Just tried that, it gave me the same results. Thanks for the suggestion.
The Solution I found on another web site would add a prototype, or forward casting which ever is preferred wording, in the cpp files namespace. I don't understand why this is needed. I may try to compile this code on another compiler
Static class members have to be both declared and defined. The problem is that ProgramState is declared but not defined.
Last edited on
Topic archived. No new replies allowed.