undefined reference to static member variable - c++

I have this class that has a static member. it is also a base class for several other classes in my program. Here's its header file:
#ifndef YARL_OBJECT_HPP
#define YARL_OBJECT_HPP
namespace yarlObject
{
class YarlObject
{
// Member Variables
private:
static int nextID; // keeps track of the next ID number to be used
int ID; // the identifier for a specific object
// Member Functions
public:
YarlObject(): ID(++nextID) {}
virtual ~YarlObject() {}
int getID() const {return ID;}
};
}
#endif
and here's its implementation file.
#include "YarlObject.hpp"
namespace yarlObject
{
int YarlObject::nextID = 0;
}
I'm using g++, and it returns three undefined reference to 'yarlObject::YarlObject::nextID linker errors. If I change the ++nextID phrase in the constructor to just nextID, then I only get one error, and if I change it to 1, then it links correctly. I imagine it's something simple, but what's going on?

Make sure you are linking against the generated .o file. Double-check the makefile.

Related

Right way to implement member functions

I have three files -- main, a header and its 'implementation':
// main.cpp
#include "word_indexer.h"
int main() {
WordIndexer wd;
cout << wd.size() << endl;
return 0;
}
// word_indexer.h
class WordIndexer {
public:
int size() const; // declaring a function
};
// word_indexer.cpp
class WordIndexer {
public:
int size() const {return 0;}
};
Building with g++ -o main main.cpp word_indexer.cpp yields
undefined reference to 'WordIndexer::size() const'
Replacing the implementation with
// updated word_indexer.cpp
class WordIndexer {
public:
int size() const;
};
int WordIndexer::size() const {return 0;}
fixes the problem.
I cannot figure out the difference between these word_indexer.cpp and its updated versions, they seem to be identical.
Why does the first variant have linking problems?
There is no need to repeat the definition of the class in its implementation file. Just have it include the header the class is defined in and then define the member functions in the implementation file like:
// word_indexer.cpp
#include "word_indexer.h"
int WordIndexer::size() const {return 0;}
As for why the first variant did not work: Member functions defined in a class are implicitly inline. Thus, their definitions must be present in every translation unit that uses them. This is why this works if you define the member in the class in the header file, but not if you only have the inline definition in another .cpp file.
Here's how it's usually done for classes.
Header (word_indexer.h):
// declare the class and all its methods, member variables, etc.
class WordIndexer {
public:
int size() const; // declaring a function, but not its implementation
};
Implementation (word_indexer.cpp):
// the implementation basically takes the header "skeleton" and fleshes it out;
// to do that, of course, we need the skeleton first
#include "word_indexer.h"
// we don't need to say "class WordIndexer {}" or anything like that,
// because that's already been done in the header we included
// implement the method
int WordIndexer::size() const {return 0;}
In word_indexer.cpp you should have written something like:
int WordIndexer::size() const {return 0;}
That's the actual definition syntax.
And instead of redeclaring WordIndexer in the translation unit, just #include "word_indexer.h".

static const int member and undefined reference

I use gcc 4.7.3 for ARM platform to compile my code. I have several classes like this:
// types.h
enum Types
{
kType1,
kType2
// ...
};
// d1.h
class D1 : public Base
{
public:
static const int type = kType1;
// ...
};
// d2.h
class D2 : public Base
{
public:
static const int type = kType2;
// ...
};
Somewhere in the sources I use those classes:
MyObject obj;
doSomething<D1>(obj);
doSomething<D2>(obj);
// other.cpp
class Foo
{
template<typename T>
void doSomething(MyObject obj)
{
mm_.insert(std::multimap<int, MyObject>::value_type(T::type, obj));
}
};
And get the next messages (during linking):
undefined reference to `D1::kType`
undefined reference to `D2::kType`
// more messages of this type
OK. If I change do_something function like this:
template<typename T>
void doSomething(MyObject obj)
{
mm_.insert(std::multimap<int, MyObject>::value_type( (int) T::type, obj));
}
it compiles OK. But why? Can't find anything in the standard about it. Does anybody have ideas about what's going on?
Thanks.
P.S.
This fix
// d1.cpp
const int D1::kType;
also works, but this is expected.
P.P.S. the answer would be quite obvious in case of using reference or pointer to T::type, but I don't see anything that requires a ref or ptr. AFAIK std::multimap::value_type take the arguments by value (not ref, nor ptr).
// d1.cpp
const int D1::kType;
// d2.cpp
const int D2::kType;
is the answer,
//d1.h
public:
static const int type = kType1;
Is like a function prototype, it is compiled into every complication object (cpp file) that includes the header, and so because of the ODR rule it doesn't actually reserve the memory space, or else the linker would find lots of instances of the variable in every compilation unit which included the header.
You therefore need one compilation unit (a cpp file) which defines the actual memory space that will be used for the class constant found in the header that is used multiple times.

Does the friend function have to be in the same file?

I am actually testing a file and I have a situation, where I need to access some of the protected members of the class from main.cpp. I tried to add, main() as friend, didn't work out and learned that it wont work, so I moved everything in the main() to a test () and made the test() as friend. still it shows the error.
Example would be
//--File.hpp
namespace Files {
class File {
public:
File(long word_):word(word_) {}
protected:
long word;
private:
friend int test();
};
}//ns:Files
//--List_File.hpp
namespace Files {
class List_File :public File {
public:
List_File() : File(sizeof(int) + sizeof(long)) {}
private:
friend int test();
};
}//ns:Files
//--main.cpp
using namespace Files;
int test() {
File *pd = new List_File();
assert(pd->word == 12); //LINE 34
return 0;
}
int main() {
test();
return 0;
}
//it says error on Line 34: Base::value is protected. Please advice.
g++ -O -Wall -Wno-unused -o a.out File.cpp List_File.cpp Data_File.cpp
Free_List_File.cpp main.cpp
File.hpp: In function ‘int test()’:
File.hpp:30:7: error: ‘long int Files::File::word’ is protected
main.cpp:34:16: error: within this context
make: *** [a.out] Error 1
No, it definitely doesn't have to be in the same file, but it obviously has to "know" what the class is, i.e.: the header that has the class definition should be included in the file where the function is implemented. Your code should be fine, as commented.
after you added some context
Your test function is not in the Files namespace. If you want it to be in the global context, you should treat it as "::test" within the namespace, otherwise the compiler might expect the "Files::test" to be the friend function, and not the "::test" as in your case. Can't find the formal standard reference, but I'm pretty sure that's the case. Note that you're performing a forward declaration here, so there's no default fall-back to the upper level of scope for name resolution.
Maybe you missing inheritance declaration in Derived class?
class Derived : public Base {
I've tested your code (with inheritance declaration included) and it produced no error
littedev is right!!
Updated the code according to the comments by littedev..
//--File.hpp
namespace Files {
class File {
public:
File(long word_):word(word_) {}
protected:
long word;
private:
friend int test();
};
}//ns:Files
//--List_File.hpp
namespace Files {
class List_File :public File {
public:
List_File() : File(sizeof(int) + sizeof(long)) {} \
private:
friend int test();
};
}//ns:Files
//--main.cpp
namespace Files{
int test() {
File *pd = new List_File();
assert(pd->word == 12); //LINE 34
return 0;
}
int main() {
Files::test();
return 0;
}
}
I would guess that you are trying to access a protected variable outside of the scope of the Files class definition. I would recommend a function that returns the variable word when it is called and use that instead of trying to access a protected variable outside of a class definition. I could be wrong in that I am not really sure what is the scope of a protected variable (whether it is limited only to class declarations or whether it can be accessed outside of the class definition) but I am pretty sure that I am right because protected variables are like private variables. They are only accessible within the class scope. Correct me if I am wrong.
EDIT: Oh I am sorry I didn't realize what you were doing. littleadv is right, your function declaration isn't within the files namespace.

error initializing an object of the same class within class definition - C++

I am trying to implement the following class. However, when I try to instantiate an object of the class within its definition and pass "0" as value to initialize the object, i get an error:
"a type specifier is expected".
Can anyone explain how can i remove this error?
class MessageType
{
public:
static const MessageType msgEvent(0);
private:
MessageType();
virtual ~MessageType();
MessageType(int);
};
You need to initialize(define) it outside the class definition in a cpp file.
MessageType const MessageType::msgEvent;
However, Your intent in doing so is not very clear. Are you trying to implement a Singleton Pattern, probably this sample implementation might help, I leave it to you to decide, whether you really need a singleton, inspite of its disadvantages:
//MessageType.h
#include <boost/noncopyable.hpp>
class MessageType: private boost::noncopyable
{
public:
static MessageType* instance();
private:
MessageType();
~MessageType();
static bool g_initialised;
// static initialisation
static MessageType g_instance;
// dynamic initialisation
};
// MessageType.cpp
#include "MessageType.hpp"
#include <ostream>
#include <iostream>
#include <cstring>
bool MessageType::g_initialised;
// static initialisation
MessageType MessageType::g_instance;
// dynamic initialisation
MessageType::MessageType()
{
g_initialised = true;
}
MessageType::~MessageType()
{
g_initialised = false;
}
MessageType* MessageType::instance()
{
return g_initialised ? &g_instance : 0;
}
You can only initialize static member variables in the definition if they are of int type.
class MessageType
{
public:
static int sCount = 0; // That is fine.
static std::string sLogName; // That is fine.
static std::string sLogName("log.txt"); // Fail!
};
There's no way around this rule. If you want to initialize a static member variable, then you have to do it in the cpp:
std::string MessageType::sLogName("log.txt"); // fine, in the cpp.
This same rule applies directly to your MessageType instance, and has nothing to do with the fact that the class is of it's own type.

multiple definition for static member?

Failed to link up following two files, when I remove the "static" keyword, then it is okay. Tested with g++.
Check with readelf for the object file, the static member seems is exported as a global object symbol... I think it should be a local object ...?
static1.cpp
class StaticClass
{
public:
void setMemberA(int m) { a = m; }
int getMemberA() const { return a; }
private:
static int a;
};
int StaticClass::a = 0;
void first()
{
StaticClass statc1;
static1.setMemberA(2);
}
static2.cpp
class StaticClass
{
public:
void setMemberA(int m) { a = m; }
int getMemberA() const { return a; }
private:
static int a;
};
int StaticClass::a = 0;
void second()
{
StaticClass statc1;
static1.setMemberA(2);
}
With error info:
/tmp/ccIdHsDm.o:(.bss+0x0): multiple
definition of `StaticClass::a'
It seems like you're trying to have local classes in each source file, with the same name. In C++ you can encapsulate local classes in an anonymous namespace:
namespace {
class StaticClass
{
public:
void setMemberA(int m) { a = m; }
int getMemberA() const { return a; }
private:
static int a;
};
int StaticClass::a = 0;
} // close namespace
void first()
{
StaticClass statc1;
static1.setMemberA(2);
}
The following is a definition of the static data member. It has to occur only in one file that's compiled and then linked.
int StaticClass::a = 0;
If you have multiple such definitions, it is as if you had multiple functions called first. They will clash and the linker will complain.
I think you are mistaking static members with static applied to namespace scope variables. At the namespace level, static gives the variable or reference internal linkage. But at the class scope level (when applied to a member), it will become a static member - one that is bound to the class instead of each object separately. That then has nothing to do with the C meaning of "static" anymore.
The statement
int StaticClass::a = 0;
actually allocates storage for the variable, this is why it should be only written once.
As for your remark, you are probably confusing two different uses of the static keyword. In C static used to mean "use internal linkage", this means that the name of a variable or function would not be seen outside the translation unit where it was defined.
In classes, static is used to define class members, that is variables or methods that don't refer to a specific instance of the class. In this case the storage for a static variable must be allocated somewhere (as it is not part of any instance), but only in one place of course.
StaticClass needs a place to store a, a static variable that will be shared by all class instances - there are two lines that do so, one in each file. Removing one will fix the linker error.