This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does it mean to have an undefined reference to a static member?
I have a static class as follows:
.h file
class c1 {
public:
static int disconnect();
private:
static bool isConnected;
};
.cpp file
#include c1.h
int c1::disconnect()
{
c1::isConnected = false;
return 0;
}
However when I compile, there is an error
undefined reference to `c1::m_isConnected'
Please help!
You have to provide an actual object instance for the static class members. Add the following to your .cpp file:
bool c1::isConnected;
To initialize it to a specific value, add an initializer:
bool c1::isConnected = false;
(By the way, classes in C++ cannot be static. Classes are just types. Only class members can be static.)
isConnected is a (non-static) member variable and you can't use it without an instance that it's a part of. While static variables exist independently of any object, non-static member variables only exist as part of an instance of that class.
You need to either make isConnected static or accept an instance of c1 on which to set isConnected to false. You probably want the former.
What you have in header file is declaration. However, you also need a definition of the variable. The correct code will look like this:
class c1 {
public:
static int disconnect();
private:
static bool isConnected;
};
bool c1::isConnected = false;
int c1::disconnect()
{
isConnected = false;
return 0;
}
Related
This question already has answers here:
problem sorting using member function as comparator
(9 answers)
Closed 2 years ago.
Is there any way to declare the comp function here under the scope of the class declared like this.
class Solution {
public:
bool comp(vector<int> x, vector<int> y){
return x[0] < y[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),comp);
return {{}};
}
};
This code snippet gives error :
reference to non static member must be called
The compiler would say that a non-static member reference must be relative to a specific object. This hint states that you must need to create an object before accessing the class members if the members ain't declared as static.
There are two options here:
Either Declare the class member function as static and then directly access.
OTOH, create an object (instance) of the class and then access the class member function.
The first's solution would look something like:
class Solution {
public:
// Declared as 'static', so you don't need to create any instances
static bool comp(vector<int> x, vector<int> y) {
// ...
}
};
// ...
Solution::comp(...);
And the second solution would be (perhaps you didn't want this one):
class Solution {
public:
bool comp(...) {
// ...
}
};
// ...
Solution s;
s.comp(...);
This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 2 years ago.
I have declared a static variable in a file say "example.hxx" and am calling this variable in the cxx file "example.cxx" in this way
example.hxx file :-
class example{
private:
static int p;
public:
void func();
};
CXX file
#include <example.hxx>
class example{
void func(){
std::cout<<p;
}
}
I get an error of undefined reference to p.
Why does this happen and how to solve this ?
I have seen some answers in relation to this question but none deals with separate hxx and cxx files, instead they answer wrt main function. Would be great if someone clears my doubt !
You have only declared p. You need to define it as well, like this, outside the class:
// .hpp file
class example{
private:
static int p; // declaration
};
// .cpp file
int example::p = 42; // definition
Alternatively, you could do:
class example{
private:
inline static int p = 42; // inline definition
};
Your syntax is incorrect. First, missing ; when defining example in the .hxx file. Second, you have multiple definitions of the class example. Instead, you should define func as follows:
void example::func(){
....
}
Third, you have multiple definitions of func. Replace void func(){}; with void func();.
After that's done, you're also missing a definition of p. Use
class example{
private:
static inline int p = some_value;
...
Or add a definition in the .cxx file.
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:
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:
How to initialize private static members in C++?
(18 answers)
Closed 9 years ago.
So I have the following c++ class
class MyClass:
public:
static void InitObject();
private:
static MyObject *myObject;
};
And then in the .cpp file I do
void MyClass::InitObject
{
myObject = new MyObject();
}
However, I get a compiler error saying that "myObject" was referenced from InitObject() and then it says Linker command failed with exit code 1.
Why doesn't this work? How do I fix it?
Since static data members of classes in C++ need memory space that is separate from the instance memory, they need to be defined, in addition to being declared in the class.
You do that in a separate translation unit (in your CPP file) like this:
MyObject *MyClass::myObject;
This definition tells the compiler to allocate space for myObject in the static memory area. Without this definition, the code is going to compile, but the linker will report an error, because it is responsible for ensuring that all referenced static objects have memory allocated to them.
Extend your .cpp file with the following definition for myObject:
MyObject* MyObject::myObject = NULL;
NOTE:
For your particular case you might be better off saying:
class MyClass:
{
public:
static MyClass& instance();
private:
MyClass() {}
};
and in .cpp file:
MyClass& MyClass::instance()
{
static MyClass myInstance;
return myInstance;
}
I'd prefer this over using new MyClass(). Any access to the instance() method will guarantee your instance is initialized exactly once, and you'll get a valid reference to it.
'Space' is completely allocated on the stack then, as well for the reference as for the instance itself.
You never allocated space for MyObject::myObject. Put this in the CPP file:
MyObject* MyObject::myObject;