This question already has answers here:
What does it mean to have an undefined reference to a static member?
(2 answers)
Closed 9 years ago.
I have a .cpp file that looks something like this:
//other code
namespace {
class C1;
class C2;
class C2{
public: static int counter;
//member functions here
};
class C1{
//other code
C2::counter = 10;
};
}
When I run 'make' I get the following error:
relocation R_386_GOTOFF against undefined symbol '(anonymous namespace)::C2::counter' can not be used when making a shared object...
Am I missing something simple here? Shouldn't the static int be available for class C1 to change it? Also, I am developing this as a part of the Clang library's. Also, I can share the Makefile if that helps.
You have missed out on providing the definition of you static variable. This definition must occur outside the class and only one definition is allowed. Usual way to do this is to provide the definition in the implementation file.
Because you are directly using it, without providing any definition for it, you are getting the error.
Related
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
How to initialize the reference member variable of a class?
(2 answers)
Closed 2 years ago.
can a struct member be a reference to another class?
I have this struct in header file:
struct stop_object {
timetable& tt;
std::string stop_name;
std::set<std::string> platforms;
};
and this class in header file:
using stop_container = std::vector<stop_object>;
class timetable {
public:
stop_container timetable_stops;
platform_container timetable_platforms;
platform_route_container timetable_platform_routes;
route_departure_container timetable_route_departures;
trip_container timetable_trips;
trip_departure_container timetable_trip_departures;
};
I can not build the program. It says: missing type specifier. for timetable& tt;
How can I achieve that I will have the reference to the parent object in which the struct is?
Thanks for help!
This question already has answers here:
Undefined reference to static class member
(9 answers)
How to initialize private static members in C++?
(18 answers)
Closed 6 years ago.
I have a file A.hpp as such:
class A
{
private:
static std::string s;
public:
void modify_string();
};
I am implementing this in a file A.cpp as such:
#include "A.hpp"
void A::modify_string()
{
s = "something"; // Error here.
}
My main class:
int main()
{
A a;
a.modify_string();
}
I understand static variables are shared by all the class instances. I also went through this SO post where it says how to access the static member. Public static member of class . Could you please let me know where my concept is missing at?
Edit:
I am getting this error:
error: undefined reference to A::s
When you define:
void modify_string() {
s = "something"; // Error here.
}
You are creating a new function, not defining the member function modify_string of the class A. You need to do:
void A::modify_string() {
To inform the compiler that you are defining the member function modify_string for class A.
You also need a ; after your class definition.
Finally, the variable s is static so it needs to be defined seperatly somewhere so the linker can find a reference to it. So add:
std::string A::s = "default";
This was clearly described in the link you provided for your question.
Here is a working example: http://ideone.com/iQ6Kux
You need to reserve storage for s in exactly one compilation unit.
Do that by writing
std::string A::s;
In exactly one source file.
Your definition void modify_string() {...} in A.cpp is not defining the member function of the class, it's defining a separate global function with the same name. You probably meant
void A::modify_string()
{
s = "something";
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
Why is it giving me a linking error? I think it's OK to access static members with this->x. Logically it sounds OK. I guess an instance pointer can access what a class owns as per OOPS concepts.
You need to also define the static member variable. For example:
// in .h
class some_class {
static int v; // it's just a declaration
};
// in .cpp
int some_class::v; // here's the defination
Put declarations in your Foo.h file:
class Foo {
static int v;
};
Put defenitions in your Foo.cpp file:
int Foo::v;
Here are some explanations: Why the static data members have to be defined outside the class separately in C++, Defining static members in C++
I find it helpful to treat linking errors differently. If you do something against the rules of the language -- the compilation error will be issued, so if you get a linking error it must be not something that you've done against the rules of the language directly, rather than you've omitted something. And usually the error message tells you what exactly you have omitted.
This question already has answers here:
Undefined reference to static variable [duplicate]
(2 answers)
Closed 9 years ago.
I am beginner to C++ and have a doubt about static member variables and member functions.
I have implemented a class as follows -
class Foo
{
private:
static int myVariable;
public:
static void setMyVariable()
{
myVariable = 100;
}
static void resetMyVariable()
{
myVariable = 0;
}
};
There are following considerations when I wrote a code like that -
I want only one instance of class Foo. Thats why I made all member variables and functions as static.
I don't want the outside code to touch myVariable
I have put this class in a header file and included in my main file. When I do this, I get an error undefined reference to Foo::myVariable
I want to know if I can write a code which can satisfy above requirements?
Thanks !
You need to define static class variables somewhere:
e.g. in your main C++ file,
int Foo::myVariable;
Note that technically, by making everything static, you may have no instances of Foo.
This question already has answers here:
Undefined reference to static class member
(9 answers)
C++ - Initialize and modify a static class member
(4 answers)
Closed 9 years ago.
I have problem with pthread_mutex_t. When I try to create static field pthread_mutex_t, then initialize it in static function and finally use it within some class methods I get many errors like:
main.o: In function `LogWriter::initialize(pthread_mutex_t*)':
main.cpp:(.text._ZN9LogWriter10initializeEP15pthread_mutex_t[LogWriter::initialize(pthread_mutex_t*)]+0x7): undefined reference to `LogWriter::mutex'
Simpplified class code:
class LogWriter{
static pthread_mutex_t mutex;
static void initialize(pthread_mutex_t *mut){
LogWriter::mutex = PTHREAD_MUTEX_INITIALIZER;
//if(pthread_mutex_init(&(LogWriter::mutex), NULL) != 0){
//init failed
//}
}
public:
static LogWriter getInstance(string module_name){
LogWriter instance(module_name);
return instance;
}
LogWriter& operator<<(string a);
};
My quesiton is: why ? I know that if I define it as normal (non-static) field I won't have any problems. Also searched google but I couldn't find any materials that are linked with this.
Also creating pointer to static pthread_mutex and initializing in in main function ends like this.
In some source file in your code, you need to add:
static LogWriter::pthread_mutex_t mutex;
The compiler won't "place" your variable in any particular source file, you have to do that for it. The class declaration just tells the compiler "I'll have a static variable somewhere" - but since, at least in theory, variables ordering and placement can make a difference [you may for example have different object files product "data" that goes into different sections of memory in some embedded system], the compiler won't be able to just throw it in any place it likes - that could be somewhere you don't want it.