I have a data type and I can instantiate a variable of that type. like this:
FetchAddr faddr(VirtualMemoryAddress( 0x0a ));
The definition of FetchAdr is:
struct FetchAddr {
VirtualMemoryAddress theAddress;
FetchAddr(VirtualMemoryAddress anAddress)
: theAddress(anAddress)
{ }
};
Now I have a class that faddr is a private (or public) variable
class FLEXUS_COMPONENT(BPred) {
static FetchAddr faddr;
public:
FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
: base( FLEXUS_PASS_CONSTRUCTOR_ARGS )
{
faddr = VirtualMemoryAddress( 0x0a );
}
...
}
Assume the macros are defined properly.
The code compiles and links without any problem. However when I start the program, it says:
"undefined symbol: _ZN6nBPred14BPredComponent8faddr"
it says there no symbol for faddr.
any idea about that?
When you declare a static member you also have to define it somewhere, like in a .cpp file. And also remember to link to this file.
Problem number 2 –
FetchAddrdoesn’t have a default constructor.If you need to have
faddras a static member of the class, you also need to give it a value when it is defined, like:That creates an
faddrthat is shared by allFLEXUS_COMPONENT(BPred)objects.If you rather have it that each object has its own copy of the
faddrvariable, you can make it non-static and initialize it in the constructor: