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.
#include <iostream>
class ObjectInfo{
private:
static float Rotation;
public:
//sets object rotation value
void SetR(float a){ static float Rotation = a; }
//print roation value (I think this is where the problem is located)
void PrintR(){ std::cout << Rotation;}
};
int main()
{
ObjectInfo Wall;
//set float var
float Rotation;
//Get user set rotation
std::cin >> Rotation;
//set wall rotation
Wall.SetR(Rotation);
//print wall rotation value
Wall.PrintR();
std::cin >> Rotation;
}
Error 1 error LNK2001: unresolved external symbol "private: static float ObjectInfo::Rotation" (?Rotation#ObjectInfo##0MA)
Error 2 error LNK1120: 1 unresolved externals
This is a protype i made and i have no clue how to resolve the error.
Does anynyone know what could cause this error?
I get the same error if i try returning the value and then couting that value.
Does anyone know an alteriate solution to retreaving the value from the class?
You need to allocate storage for your static member, need
float ObjectInfo::Rotation;
outside your class 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)
Unresolved external symbol on static class members
(6 answers)
Closed 2 years ago.
Error message in question:
LNK2001 unresolved external symbol "public: static class std::vector<struct MakeKey::KeyStruct,class std::allocator<struct MakeKey::KeyStruct> > MakeKey::KeyArray" (?KeyArray#MakeKey##2V?$vector#UKeyStruct#MakeKey##V?$allocator#UKeyStruct#MakeKey###std###std##A)
Hello i'm kind of new to this whole coding thing and apologize if the error is stupid but I am getting a unresolved external symbol error on both of my public static vectors. I have read through multiple posts on stack overflow and can't seem to figure out what I am doing wrong. Anyway here is the minimal code that produces the same errors. Yes it is important that the vectors remain static because multiple other errors appear if it is non-static.
class MakeKey
{
public:
typedef struct KeyStruct {
sf::Image Img;
sf::Texture Tex;
sf::Sprite Sprite;
}NewKey;
static vector <MakeKey::NewKey> KeyArray;
static vector <sf::RenderWindow*> WindowArray;
static void StepWindows()
{
for (int i{ 0 }; i > MakeKey::KeyArray.size(); i++)
{
WindowArray[i]->clear(sf::Color::Transparent);
WindowArray[i]->draw(KeyArray[i].Sprite);
WindowArray[i]->display();
}
}
};
And my main
int main()
{
MakeKey::StepWindows();
}
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 8 years ago.
I just started with C++ for some OpenGL applications and wanted to pack some monster functions into a util class so that my code remains clean. This is what I did:
awesomeClass.h :
#pragma once
class AwesomeClass
{
public:
static void do_something_awesome();
};
awesomeClass.cpp :
#include "awesomeClass.h"
void do_something_awesome(){
//...
}
main.cpp :
#include "awesomeClass.h"
int main(int argc, char** argv)
{
AwesomeClass::versuchen();
return 0;
}
Output:
Error 3 error LNK1120: 1 unresolved externals \Visual Studio 2013\Projects\TestEnvironmment\Debug\TestEnvironmment.exe TestEnvironmment
Error 2 error LNK2019: unresolved external symbol "public: static void __cdecl AwesomeClass::do_something_awesome(void)" (?do_something_awesome#AwesomeClass##SAXXZ) referenced in function _SDL_main \Visual Studio 2013\Projects\TestEnvironmment\TestEnvironmment\main.obj TestEnvironmment
What is wrong with that code? I mean it works when I paste everything in one file.
You should write
void AwesomeClass::do_something_awesome(){... }
Instead of
void do_something_awesome(){... }
Otherwise the function you implement does not belong to the class.
You should write this if you want write do_something_awesome in separate cpp file.
void AwesomeClass::do_something_awesome(){
//...
}
Or you can use:
#pragma once
class AwesomeClass
{
public:
static void do_something_awesome(){
//code here
}
};
Greetings.
I have searched for a solution, but I think this problem is personal code specific, hence my posting here.
I'll get straight to the point.
In my main I have two objects.
Computer *computer = new Computer();
Player *player = new Player();
In the computer class, in the header I have the following:
private:
Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;
Then in Computer.cpp:
Computer::Computer(char token, bool hasTurn)
{
m_token = token;
m_hasTurn = hasTurn;
strategy = new Strategy();
}
int Computer::getMove(const char (&board)[9])
{
twoInRow = strategy->checkTwoInRow(board);
counter = strategy->counter(board);
makeTwo = strategy->makeTwo(board);
if(twoInRow != 0)
{
return twoInRow - 1;
} else if(counter != 0) {
return counter - 1;
} else if(makeTwo != 0) {
return makeTwo - 1;
} else {
return 0;
}
}
At this point I think the problem arises.
The methods called from the class Strategy all require knowledge of the board thus:
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
The problems im getting, unabling them to compile:
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::makeTwo(char const (&)[9])" (?makeTwo#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 2 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::counter(char const (&)[9])" (?counter#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 3 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::checkTwoInRow(char const (&)[9])" (?checkTwoInRow#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
As a c++ noob, I have absolutely no clue why or how this problem is caused. I think it has to do something with either the instantiation of Strategy in the computer class, or with the parameter given from computer to the strategy in the method calls.
Can anyone explain WHY this error is occuring, I don't quite understand the error at all.
And also how to solve/prevent this?
EDIT*
I just got a request to share the Strategy class:
Strategy.h:
#pragma once
class Strategy
{
public:
Strategy(void);
~Strategy(void);
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
};
The class defined these methods, I won't post them because they are quite long.
This is a linking error and it has nothing to do with instantiations or parameters.
You haven't provided the linker with the definitions for those functions. If you defined them in Strategy.cpp you need to compile that and add it as an argument to the linker. How you do that depends entirely on what tools you're using to build your program.
If you're using Visual Studio (or any other IDE) it should take care of it automatically once you've added Strategy.cpp to your project.
Or did you perhaps define them like this:
int checkTwoInRow(const char (&board)[9])
{
// Do something with board the wrong way
}
instead of like this:
int Strategy::checkTwoInRow(const char (&board)[9])
{
// Do something with board the right way
}
The first one doesn't define a Strategy member function, it defines a global function.
The error is simply stating that you have declared, but not defined, the member functions Strategy::makeTwo, Strategy::counter, and Strategy::checkTwoInRow. Are you sure that you implemented them (in a source file that's actually being compiled) and that you didn't accidentally define them as free functions?
Okay, as mentioned, I am having two unresolved external errors. There are no intellisense errors and such, just this two linker errors. Here is the code.
include "stdafx.h"
include < iostream >
using namespace std;
class circle; //forward declaration
class square
{
public:
square create_square(circle user_circle);
};
class circle
{
public:
friend square square::create_square(circle user_circle)
};
square square::create_square(circle user_circle)
{
square user_square(user_circle.get_circumference());
return user_square;
}
// function call
user_square = user_square.create_square(user_circle);
This isn't the whole program, but the error seems to be pointing to this batch of code
Error 1 error LNK2019: unresolved external symbol
"public: __thiscall circle::~circle(void)" (??1circle##QAE#XZ) referenced in function
"public: class square __thiscall square::create_square(class circle)"
(?create_square#square##QAE?AV1#Vcircle###Z)
C:\Users\John\documents\visual studio 2010\Projects\PROG5\PROG5\PROG5.obj
Error 2 error LNK2019: unresolved external symbol
"public: __thiscall square::~square(void)" (??1square##QAE#XZ) referenced in function
"public: class square __thiscall square::create_square(class circle)"
(?create_square#square##QAE?AV1#Vcircle###Z)
C:\Users\John\documents\visual studio 2010\Projects\PROG5\PROG5\PROG5.obj
I'm really stumped, and I don't want to just randomly change the code because my logic should be correct. A circle object gets passed to the create_square function, the function gets the diameter of the circle and makes that the perimeter of the square. As for the pointless exercise, this is for a class. Hopefully someone can help, thank you.
You declared a circle destructor, and a square destructor - but you didn't implement them.
Additionally, you should prefer const references over pass by value:
square create_square(circle const & user_circle);
You get the error at this point because you're passing a circle by value - which means a temporary will be created and destructed - and because you return a square by value, which means a square will be constructed and destructed.
square square::create_square(circle user_circle)
{
square user_square(user_circle.get_circumference());
return user_square;
} // Destruction point of user_circle
// function call
user_square = user_square.create_square(user_circle);
The problem is user_circle is passed by value to square::create_square. So, the passed parameter is copied to the receiving argument. Up on the return of the function, object needs to be destructed but since in the class declaration, you have provided just the destructor declaration but not definition which the linker is complaining about.