Homework - Compile error

Hey guys I am working on a homework assignment for my programming languages class, and I am stuck because of a compile error. My assignment is implementing a reverse polish calculator. My design goal was to create an interface called commands that will have a pure virtual function called execute(). I subclass commands with classes such as Add, Subtract and so fourth. When the user inputs '+', I will use that as the key for the Add command in the map, so I can easily grab the commands based on the user input. However, in all of my subclasses, the compiler gives the error "undefined reference to stack". I forgot to mention that I have a Stack as a protected member in the Command class. Here is the line where I get the error: https://github.com/haglerchristopher/RPN-Calculator/blob/master/src/Print.cpp#L22
and this is the command class: https://github.com/haglerchristopher/RPN-Calculator/blob/master/src/Command.h If you are able to help please don't give me any answers to anything. I would just like a nudge in the right direction to get past the compile error because I do not want to cheat and possible receive a zero grade. Thanks for all of the help.
Last edited on
bump
Define the static member in Command.cpp

1
2
3
4
5
6
7
#include "Command.h"

void Command::insert(RationalNumber number) {
    stack.push(number);
}

Stack<RationalNumber> Command::stack; // ***  
Thanks for the help. Why is it that you have to define it in the .cpp file and not in the header? I come from Java, so it is fairly new to me.

Edit: I found this source:http://stackoverflow.com/questions/17342703/how-and-where-to-define-class-variable-when-using-header-files-in-c but when should you ever declare class variables in the header? If you wanted class variables to be private, would they still be private by not being defined under the private section in the header file and being defined in the class definition? Sorry for all of the questions.
Last edited on
> Why is it that you have to define it in the .cpp file

C++ makes a distinction between a declaration and a definition.

The static data member is declared inside the class definition. It has external linkage, and must be defined (at namespace scope), once (and only once) in the entire program.


> when should you ever declare class variables in the header?

Member variables are always declared within the class definition.
Static member variables (not associated with specific objects) are defined at namespace scope.

Static members are subject to member access control (private, protected, public).
Last edited on
Topic archived. No new replies allowed.