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 am using static variable. After referring to Unresolved external symbol on static class members, I modified the program with Abc::ct
#include <iostream>
class Abc
{
private:
static unsigned int ct;
public:
void f1()
{
for (int i = 0; i < 5; ++i)
f2();
}
void f2() {
Abc::ct = 0;
if (Abc::ct == 0)
std::cout << "Zero iteration\n";
std::cout << Abc::ct << "\t";
++Abc::ct;
}
};
int main()
{
Abc obj;
obj.f1();
}
but getting error as error LNK2001: unresolved external symbol "private: static unsigned int Abc::ct" in MSVC or undefined reference to Abc::ct in g++. How can I define static variable in class Abc?
You declared your static variable, but you did not define and initialize it. Above main(), but outside of your class, add the following line:
unsigned int Abc::ct = 0;
or, if you are using C++17, you can change your:
static unsigned int ct;
to:
static inline unsigned int ct = 0;
You have to define it:
unsigned int Abc::ct = 0;
Demo
#include <iostream>
class t1
{
public:
~t1();
static t1& fun();
private:
t1()
{
}
};
t1& t1::fun()
{
return t1();
}
int main()
{
t1::fun();
return 0;
}
I am getting unresolved external symbol. please help. the errors are below
Error 2 error LNK2019: unresolved external symbol "public: __thiscall t1::~t1(void)" (??1t1##QAE#XZ) referenced in function "public: static class t1 & __cdecl t1::fun(void)" (?fun#t1##SAAAV1#XZ) D:\LXI\LXIRef\RefDesign_V01.00\Software\Solution\TestWebServer\TestWebServer.obj TestWebServer
Error 3 error LNK1120: 1 unresolved externals D:\LXI\LXIRef\RefDesign_V01.00\Software\Solution\Debug\TestWebServer.exe 1 1 TestWebServer
Give definitions to constructor and destructor.
#include <iostream>
class t1
{
public:
~t1() {} // <<<< defined here
static t1& fun();
private:
t1() {} // << defined here
};
t1& t1::fun()
{
return t1();
}
int main()
{
t1::fun();
return 0;
}
I'd like to pass my 2D Array of class Menza into function..
class Menza
{
public:
string PrintLunch() const {return Lunch;};
unsigned int PrintID() const {return ID;};
double PrintPrice() const {return Price;};
double PrinteValue() const {return eValue;};
string PrintDescription() const {return Description;};
void ChangeLunch(string Change) {Lunch = Change;};
void ChangePrice(double Change) {Price = Change;};
void ChangeID(int Change) {ID = Change;};
void ChangeeValue(double Change) {eValue = Change;};
void ChangeDescription(string Change) {Description = Change;};
private:
string Lunch;
double Price;
unsigned int ID;
string Description;
double eValue;
};
const int Lunches = 5;
void LoadFile(bool FileChoice,Menza (*InputFromFile)[Lunches]);
void CustomerSelection(Menza CustomerSelect[],Menza (*InputFromFile)[Lunches]);
int main()
{
Menza InputFromFile[Lunches][Lunches];
Menza CustomerSelect[Lunches];
bool FileChoice = false;
LoadFile(FileChoice,InputFromFile);
CustomerSelection(CustomerSelect,InputFromFile);
}
Once I compile this, it shows me:
Semestralka.obj : error LNK2019: unresolved external symbol "void __cdecl LoadFile(bool,class Menza (*)[5])" (?LoadFile##YAX_NPAY04VMenza###Z) referenced in function _main
1>E:\My VSB\ZP projekty\Semestralka\Debug\Semestralka.exe : fatal error LNK1120: 1 unresolved externals
Can someone explain me whats wrong in this function call?
Thanks
You don't have definition of LoadFile function, only declaration. Therefore compiler have no way to understand what this function should do.
You must define it or link a library where it is defined (and include a header from this library). (Same is true for CustomerSelection too).
Read more about difference between definition and declaration here: declare_vs_define
I m reading The C++ Programming Language 4e. In the part of Default Argument I dont understand below code. I try to compile but there is an error. Anyway what Bjarne trying to explaing? A default argument is type checked at the time of the function declaration and evaluated at the
time of the call. For example:
class X
{
public:
static int def_arg;
void f(int = def_arg);
// ...
};
int X::def_arg = 7;
void g(X& a)
{
a.f(); // maybe f(7)
a.def_arg = 9;
a.f(); // f(9)
}
Error is :
unresolved external symbol "public: void __thiscall X::f(int)" (?f#X##QAEXH#Z) referenced in function "void __cdecl g(class X &)" (?g##YAXAAVX###Z)
MS c++ 2013
You just declared f but need to define the body of f:
void f(int = def_arg);
For example
void X::f(int)
{
// Do something
}
or
class X
{
// ...
void f(int = def_arg)
{
// Do something
}
};
Live code.
I've written this beauty:
#include <iostream>
struct something {
static const char ref[];
};
const char something::ref[] = "";
template<int N, const char(&t_ref)[N], typename to> struct to_literal {
private:
static to hidden[N];
public:
to_literal()
: ref(hidden) {
for(int i = 0; i < N; i++)
hidden[i] = t_ref[i];
}
const to(&ref)[N];
};
template<int N, const char(&ref)[N], typename to> const to* make_literal() {
return to_literal<N, ref, to>().ref;
}
int main() {
const wchar_t* lit = make_literal<sizeof(something::ref), something::ref, wchar_t>();
}
It somewhat cleanly converts between string literal types. But when I compile it, MSVC says that the make_literal function is an undefined external function- which is clearly untrue as it's defined right there.
Edit: I've managed to reduce the problem down without all of the template gunk.
struct some {
friend int main();
private:
static wchar_t hidden[40];
public:
some()
{
}
};
int main() {
std::cout << some::hidden;
//const wchar_t* lit = make_literal<sizeof(something::ref), something::ref, wchar_t>();
}
main.obj : error LNK2001: unresolved external symbol "private: static wchar_t * some::hidden" (?hidden#some##0PA_WA)
It's just a static array. Does life hate me?
The issue is that is that to_literal::hidden is declared but never defined. Take another look:
struct something {
static const char ref[]; // declaration of something::ref
};
const char something::ref[] = ""; // definition of something::ref
template<int N, const char(&t_ref)[N], typename to> struct to_literal {
private:
static to hidden[N]; // declaration of to_literal::hidden (but there's no
// definition anywhere)
public:
to_literal()
: ref(hidden) {
for(int i = 0; i < N; i++)
hidden[i] = t_ref[i];
}
const to(&ref)[N];
};
To fix this, add a proper definition of to_literal::hidden:
template<int N, const char(&t_ref)[N], typename to>
to to_literal<N, t_ref, to>::hidden[N]; // definition of to_literal::hidden
When you define static members, a declaration does not suffice. You must provide a definition outside the class. I.e. add
wchar_t some::hidden[40];
outside the class, and it'll be defined.
Otherwise, if C++ allowed this, it'd cause the same problem as defining a global variable in a header -- every .cpp file that includes it will come with a duplicate definition, and at link time you'd get a multiply-defined symbol error.
You're declaring but not defining the static member. Add something like...
template<int N, const char(&t_ref)[N], typename to>
to to_literal<N, t_ref, to>::hidden[N];
I tried to check in MSVC for you too, but with VS2005 I get another stupid error...
template<int N, const char(&t_ref)[N], typename to>
to to_literal<N, t_ref, to>::hidden[N];
...compiler complains of...
error C3860: template argument list following class template name must list parameters in the order used in template parameter list
Looks like when they fix one bug, there's another one behind it ;-/.
When I built this with VC 2008, that wasn't the error I got. The error was:
Error 1 error LNK2001: unresolved
external symbol "private: static
wchar_t * to_literal<1,&public: static
char const * const
something::ref,wchar_t>::hidden"
(?hidden#?$to_literal#$00$1?ref#something##2QBDB_W##0PA_WA) main.obj Enabler
Removing static from the to hidden[N]; member resolved the issue.
Are you sure you got the error message correct?