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?
Unresolved external symbol C++
I know that you can have static locals and static globals, but is it possible to have static fields? I ask because when I declare a static field (a static variable declared inside a class) I get "unresolved externals" compiler error messages.
Yes, it is possible. What you have to do is define the static member. Typically this is done in the corresponding .cpp file:
//=== C.h
class C {
static int i; // declaration
}
//=== C.cpp
#include <C.h>
int C::i = 0; // definition
Related
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.
1) D:\imp\msgList\fun1.cpp
CMLMessage::MLMessageStatus CMLMessage::getInformationBlocks(const TBase::TLocale& fallbackLocale )
{
CDatabaseHelper::setfallBackLocale(fallbackLocale); // setter function
}
2) D:\imp\commonfolder\fun2.cpp
class CDatabaseHelper
{
Public:
static void setfallBackLocale(TBase::TLocale fallbackLocale)
{
mfallbackLocale = fallbackLocale;
}
Private:
static TBase::TLocale mfallbackLocale; // class member
}
Compiler giving Error:
error LNK2001: unresolved external symbol "private: static struct TBase::TLocale NTrafficInformation::CDatabaseHelper::mfallbackLocale" (?mfallbackLocale#CDatabaseHelper#NTrafficInformation##0UTLocale#TBase##A)
Hi experts Do you have any suggestion for this?
In fun2.cpp, you need to initialize that static member with something like:
TBase::TLocale CDatabaseHelper::mfallbackLocale = TBase::TLocale{"C"};
where the right-hand side is any valid expression producing a TLocale. This line should go after the class definition.
See the cppreference page on "static members" for alternative ways to declare/define that static member.
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.
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.
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;
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;