Initializing static field [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I don't understand why the following code cannot be compiled:
// program.h
class Sensor;
class Program {
private:
static Sensor* sensor;
public:
void SetSensor(Sensor *s) { sensor = s; }
};
I get this compiler error:
cc3No0Or.ltrans0.ltrans.o*: In function Program::SetSensor(Sensor*)
program.h:##: undefined reference to Program sensor

You only have a declaration for the static member, you need also the definition...
Add
Sensor* Sensor::sensor;
in a .cpp file and it will work.

Related

Why can't static member methods access its non-static method in C++? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I had to make a callback interface for a new module while I work, so I made a static method in a class.
One thing I still don't understand is why I can't call a non-static member method in a static member like this :
class CAdapterUser
{
public:
CAdapterUser() {}
virtual ~CAdapterUser() {}
void Test();
void Test2();
protected:
CAdapter m_Adapter;
unsigned char buffer[16];
static void TestFunc(void* apContext);
};
void
CAdapterUser::TestFunc( void* apContext )
{
// CAdapterUser* pUser = (CAdapterUser*)apContext;
CAdapterUser* pUser = reinterpret_cast<CAdapterUser*>(apContext);
pUser->Test2(); // Compile error : LNK2019
pUser->buffer[0] = 1; // Even though I can access protected member variable?
}
Could someone answer my question?
LNK2019 is unresolved symbol, probably you simply forgot to implement Test2().

Calling scope resolution-ed CPP function from Objective C++ [duplicate]

This question already has answers here:
Inheritance - Symbol undefined Objective-C++
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I have a cpp function defined in a dynamic library like this
namespace A
{
namespace B
{
class C
{
public:
static funcA();
};
}
}
Now I am calling the above function from obcpp.mm file in the following manner:
void sample()
{
A::B::C::funcA();
}
This gives me error like Undefined Symbol A::B::C::funcA
Would highly appreciate any help regarding this.

Static word in c++ showing error [duplicate]

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 5 years ago.
Linker error: tried to make basic cpp program but there is a linker error saying:unresolved symbol "private: static int complex::count". When i removed the static words, its working fine.
Please find Program sample on this
you must explicitly define your static variable outside of the class. For example like this:
class complex {
....
int static count;
....
};
// initialization
int complex::count = 0;

using static member variable of a class inside a method of this same class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
if I have a static member variable of a class A declared in the file Af.h
and I want to use this variable inside a method "met" of this same class inside the file Af.cpp, how do I proceed?
here is my files
Af.h
class A
{
public:
static std::vector <int> vec;
void met();
//....
};
Af.cpp
//...
void A::met()
{
// I will use here some int variable i
vec.push_back(i);
//...
}
Unfortunately,this code provides the following compiling error:
undefined reference to A::vec
You need to define it in Af.cpp:
std::vector<int> A::vec;

C++ header and class file - undefined reference [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
.h:
class ArithmeticCoding
{
public:
ArithmeticCoding();
static void test(QString text);
static QMap<QChar,int> letters_freq;
}
.cpp:
QMap<QChar, int> letters_freq;
ArithmeticCoding::ArithmeticCoding()
{
}
void ArithmeticCoding::test(QString text)
{
for(int i=0; i<text.length(); i++) letters_freq[text.at(i)]++;
}
I am getting
arithmeticcoding.cpp:-1: error: undefined reference to
`ArithmeticCoding::letters_freq'
Why?
Add this to exactly one of your CPPs
QMap<QChar,int> ArithmeticCoding::letters_freq;