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?
I have a game program and I am getting VERY frustrated. Everything was running fine, and I decided to clean up my program by making separate files for each set of functions. The code is very long with multiple files, but heres the basic idea:
Im on Windows XP using the Code::Blocks IDE
In my entity.h Ive declared all of my functions and variables for that class. In my entity.cpp Ive included it, as well as in all my other files. But Im still getting a huge list of errors that tell me I have an undefined reference to all of the methods in entity.h as well as all my other header files. For example, I have a function call print() to make it easier to print out things, and thats the first method I call from the entity.h file. I get this error:
Heres the code for print():
void print(string f) {
cout<<f<<endl;
}
How Im calling it:
void Player::win(){
entity e;
e.print("You have defeated the orc");
}
The error:
In function 'ZN6Player3winEv': undefined reference to 'entity::print(std::string)'
And yes, I do have an object of entity.
Its also happening for every single other function in the entity class and file.
void print(string f) {
cout<<f<<endl;
}
should be
void entity::print(string f) {
cout<<f<<endl;
}
void print(string f) {
cout<<f<<endl;
}
is a global function
if you want to call
e.print("You have defeated the orc");
then you need an implementation for
void entity::print(string f)
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 4 years ago.
I have the following part of code in testtournamentmember.cpp
TournamentMember player1("Ian", "Smith", "1998-12-03");
TournamentMember player2 = TournamentMember(player1);
player1.setFirstName("Andrew");
with class TournamentMember.h defined as:
private:
char firstName[31];
char lastName[31];
char dateBirth[11];
static std::string location;
//static int numberMembers;
//static int difficulty;
public:
TournamentMember();
TournamentMember(char[], char[], char[]);
TournamentMember(const TournamentMember&);
~TournamentMember();
inline void setFirstName(char*);
and with TournamentMember.cpp with:
inline void TournamentMember::setFirstName(char* _firstName){
strcpy(firstName, _firstName);
}
(I have all the other functions defined, but I didn't attached them). When I want to run the code, I receive undefined reference to 'TournamentMember::setFirtstName(char*). I do not understand what is wrong with my code, because I define the fuction setFirstName as char* in the class and also in the program.
Your definition of setFirstName in TournamentMember.cpp is marked inline. That means the definition exists only in TournamentMember.cpp; that definition is not accessible from other source files, such as testtournamentmember.cpp.
By the way, you should be getting this error when you go to compile the code, not run it. You might be using a process that compiles and runs in one step, but you should still be aware of the distinction.
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 got two forms (form_1 and form_2).
I made a function inside body of form_1.
void ExampleFunction() {
ShowMessage("I'm example function inside form_1");
}
and I want to call it from form_2.
I tryed to add this function to header file of form_1.
public: // User declarations
__fastcall Tform_2(TComponent* Owner);
void ExampleFunction();
};
but when I want to call it from form_2, like :
form_2->ExampleFunction();
Builder gives me a error : "[ilink32 Error] Error: Unresolved external"
So how can I do it properly to make it work ?
You are contradicting yourself. You said you want to implement the function in Form_1 and call it from Form_2, but you are trying to implement it in Form_2 and call it from outside of Form_2.
In any case, you declared the function as a member of the Tform_2 class, so you need to qualify the function's body as such:
void Tform_2::ExampleFunction() {
ShowMessage("I'm example function inside form_1");
}
I face a very strange link problem with VC 2010. Now I am developing a C++ library, and in order to make debug much easier, for some functions the library provides two function interfaces. For example,
class Object
{
public:
int fun(std::vector<int> &auxiliary_variable_for_debug_purpose);
int fun();
}
It is also possible to reorganize this class in this way:
class Object
{
public:
#ifdef DEBUG_INDICATOR
int fun(std::vector<int> &auxiliary_variable_for_debug_purpose);
#else
int fun();
#endif
}
By doing so I except to give a clear interface to the user.
The problem I face now is both int fun(std::vector<int> &auxiliary_variable_for_debug_purpose); and int fun(); will invoke another function called void help_function(), which is declared and defined in separated files.
file.h
void help_function()
and
file.cpp
void help_function()
{
// do something
}
As you can see void help_function() is the same regardless whether DEBUG_INDICATOR is defined or not. If I defined DEBUG_INDICATOR, I can compile the class with int fun() function without any problem. However, when I undefined DEBUG_INDICATOR, the error LNK2001 error happens, suggesting unresolved external symbol void help_function(). I have tried every possible means to figure it out, but failed. Any ideas will be appreciated.
EDIT
The library I have built is a dynamic library. Regardless whether DEBUG_INDICATOR is defined, the library can be built, and the link error only happens when the library is invoked.
Since you've not posted the exact error message you are getting, this MSDN link might help you.
Tip: Be specific while asking your question if you wish to receive accurate answers.
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?
I recently started working on an interpreter in C++, but I got annoyed that vectors or arrays could not be passed to external class methods no matter what I tried and so I deleted everything I had worked on. As it turns out, I can't pass even an int to another class. I decided to give C++ another chance before resorting to C or Java, but the compiler still doesn't work as I would expect. Maybe I'm forgetting something simple about C++, as I haven't used it in a while, but this seems simple enough. My problem is: I can't pass arguments to methods in other classes when they're not defined in the same file. Here's what I'm trying to do:
Main: main.cpp
#include "myclass.h"
int main() {
MyClass test;
int n = test.add(25, 30);
return n;
}
Header: myclass.h
class MyClass {
public:
int add(int a, int b);
};
Class implementation: myclass.cpp
#include "myclass.h"
int MyClass::add(int a, int b) {
return a + b;
}
Compiling this with g++ main.cpp yields
/tmp/ccAZr6EY.o: In function main':
main.cpp:(.text+0x1a): undefined reference toMyClass::add(int, int)'
collect2: error: ld returned 1 exit status
What the heck am I doing wrong? Also, the compiler yells at me for the same thing even if my functions aren't parameterized, so it must be a problem with the header.
Any help is much appreciated - thanks!
You need to compile both files
g++ main.cpp myclass.cpp
If you only compile main.cpp, the compiler finds the declaration of MyClass::add in your header but the linker later fails to find an implementation of MyClass::add to jump to.
This question already has answers here:
static variable link error [duplicate]
(2 answers)
Closed 8 years ago.
I asked a question earlier today about singletons, and I'm having some difficulties understanding some errors I encountered. I have the following code:
Timing.h
class Timing {
public:
static Timing *GetInstance();
private:
Timing();
static Timing *_singleInstance;
};
Timing.cpp
#include "Timing.h"
static Timing *Timing::GetInstance() { //the first error
if (!_singleInstance) {
_singleInstance = new Timing(); //the second error
}
return _singleInstance;
}
There are two errors in this code which I can't figure out.
The method GetInstance() is declared in the header as static. Why in the cpp file do I have to omit the word static? It gives the error: "cannot declare member function ‘static Timing* Timing::GetInstance()’ to have static linkage". The correct way to write it is:
Timing *Timing::GetInstance() { ... }
Why can't I write _singleInstance = new Timing();? It gives the error: "undefined reference to Timing::_singleInstance". I solved this error by defining _singleInstance as a global var in the cpp file.
1: static means "local linkage" when used for a function declaration/definition outside a class-declaration.
Local linkage means that the particular function can only be referenced from code inside this particular file, and that doesn't make much sense with a method in a class.
2: Since your class declaration can be included multiple times, the actual storage for the static member should be defined in the cpp-file:
#include "Timing.h"
Timing* Timing::_singleInstance;
Timing *Timing::GetInstance() { //the first error
if (!_singleInstance) {
_singleInstance = new Timing(); //the second error
}
return _singleInstance;
}
Referencing to question 2: You need to specify the static variable at the top of your cpp-file:
Timing* Timing::_singleInstance = NULL;
static within a class means something completely different than static outside of it. Yeah, not the greatest design decision of C++, but, we have to live with it.
I imagine the whining comes from the linker, and it's because you have declared that variable but never defined it, making it an undefined references. Just add in your .cpp file a line like:
Timing* Timing::_singleInstance;
yes, you have to omit the static in the .cpp file
You'll have to 'reserve memory' for _singleInstance somewhere, e.g. by writing the following in the .cpp file:
Timing *Timing::_singleInstance = NULL;
(outside the definition of the member functions)
In the definition, you need to omit the static keyword. Its because that's teh syntax of C++. Nothing big.
Once you fix error number 1, error number 2 will be fixed automatically.