C++ class static member initialisation [duplicate] - c++

This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 9 years ago.
I have the following code:
class employee {
public:
static int last_id;
...
};
int main() {
employee::last_id=0;
}
When i try to run it it gives the following error:
Undefined symbols for architecture x86_64:
"employee::last_id", referenced from:
_main in chap7-F3IpS1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]

int employee::last_id=0;
int main() {
[...]
}

You only declared the static data member but not defined it. Write before main in the global namespace
int employee ::last_id;
It will be initialized by zero though your explicitly can specify the initializer.

Related

Undefined reference to the static variable of structure in cpp [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
static variable link error [duplicate]
(2 answers)
Closed 3 years ago.
I am not able to use structures for static variable and function. Could anyone explain why exactly is this behavior?
#include <iostream>
using namespace std;
class Foo
{
private:
typedef struct
{
int bee_1;
}bee;
static bee test;
public:
static void Inc(){ test.bee_1++;}
static int getBee_1(){return test.bee_1;}
};
int main(){
Foo::Inc();
Foo::Inc();
Foo::Inc();
Foo::Inc();
cout << Foo::getBee_1();
return 0;
}
I get the following error when I use this code:
clang++-7 -pthread -o main main.cpp
/tmp/main-521333.o: In function `Foo::Inc()':
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x24): undefined reference to `Foo::test'
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x2e): undefined reference to `Foo::test'
/tmp/main-521333.o: In function `Foo::getBee_1()':
main.cpp:(.text._ZN3Foo8getBee_1Ev[_ZN3Foo8getBee_1Ev]+0x7): undefined reference to `Foo::test'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1

Undefined Symbols For Architecture x86 [duplicate]

This question already has answers here:
Undefined reference to a static member
(5 answers)
Closed 6 years ago.
I am coding in c++ and I am attempting to learn about static variables.
When I wrote my practice code, I got this error message:
Undefined symbols for architecture x86_64:
"pizza::firstLetterFavPizza", referenced from:
pizza::favPizzaFirstLetterChan(char) in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Please help! I don't know what's wrong. The source code is here:
#include <iostream>
class pizza
{
public:
static char firstLetterFavPizza;
char favPizzaFirstLetterChan (char letter = firstLetterFavPizza)
{
pizza::firstLetterFavPizza = letter;
return pizza::firstLetterFavPizza;
}
};
int main()
{
pizza *a = new pizza();
pizza *b = new pizza();
std::cout << a->favPizzaFirstLetterChan('c') << std::endl;
delete a;
std::cout << b->favPizzaFirstLetterChan('b') << std::endl;
delete b;
return 0;
};
You have declared static data member, but not defined it. Add a definition to your code somewhere (in the global namespace):
char pizza::firstLetterFavPizza;

linker cannot find a C++ static member [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.
When i write simple C++ code in X-code, it shows Linker Error.
Undefined symbols for architecture x86_64:
"Emp::id", referenced from:
Emp::Emp() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#include <iostream>
using namespace std;
class Emp
{
public:
static int id;
int sal;
Emp()
{
Emp::id =10; // When i comment this line its working fine.
};
};
int main(int argc, const char * argv[])
{
Emp Ram;
cout << Ram.sal ;
return 0;
}
You have declared id as a static variable. You then set it in every constructor call, which is probably not what you want to do.
For a 'fix', you can add the following line above main:
int Emp::id = 0;
However, you may not want that to be static. For more information on static class variables, see this page

Unable to initialize static map in class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
static variable link error [duplicate]
(2 answers)
boost::function static member variable
(1 answer)
Closed 9 years ago.
I am trying to initialise a static map of
map<string, int> in my program as follows:
testApp.h
class testApp(){
public:
void setup();
void update();
void renew();
static map<string, int> _someMap;
};
testApp.cpp
testApp::setup(){
_someMap["something"] = 1;
_someMap["something2"] = 2;
cout<<_someMap["something"]<<"\n";
}
I don't want to use boost for this short use of map and add source dependency for my code. I am not on C++11 and I don't have the constructor here in the program since the class is some framework's class. I am on Xcode and on doing the above in .cpp, I get the following error:
Undefined symbols for architecture i386:
"testApp::mapppp", referenced from:
testApp::setup() in testApp.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
-- >Additionally, let's say my map is private, for which I tried doing this in my class:
...
private:
static someVariable;
static void someFunction();
.cpp
testApp::setup(){
someFunction();
}
Error:
Undefined symbols for architecture i386:
"testApp::_someMap", referenced from:
testApp::someFunction() in testApp.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You've declared the variable in the class definition, but it looks like you haven't defined it. Every static variable needs to be defined in exactly one translation unit. So add a definition to your source file:
map<string, int> testMap::_someMap;
If you like (and if you can't use a C++11 initialiser), you could avoid having to call the setup function by initialising the map from the result of a function instead:
map<string, int> make_map() {
map<string, int> map;
map["something"] = 1;
map["something2"] = 2;
return map;
}
map<string, int> testMap::_someMap = make_map();

C++ templates not defined in header / linker error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
I just came across something that I failed to understand.
I had a problem at the linking stage in the following case.
//header file
class A
{
template<class T>
std::weak_ptr<T> GetSomethingFromSomeWhere(const char* Id);
};
//cpp file
template<class T>
std::weak_ptr<T> A:GetSomethingFromSomeWhere(const char* id)
{
//A method with the right stuff inside and the right return statement
...
}
//Another class
class B
{
};
//main.cpp
int main ()
{
A a;
auto pB = a.GetSomethingFromSomeWhere<B>( "id" );
}
This didn't compile, during linking I have something of this kind :
Undefined symbols for architecture x86_64:
"std::__1::weak_ptr A::GetComponentFromName(char const*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I fixed it by defining the template method directly in the header file.
Should I always define template method in the header ? Why ?
I am on OSX and use clang++ with XCode if that can be of any help
Thanks
Template definition needs to be visible to the code using it. Otherwise linker errors will be generated.
There are different workarounds for situations like that:
Read This