Undefined symbol error when use inline methods in C++ [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
When I make method as inline, compilation fails with this error:
Undefined symbol: 'int CPositionRequestor::State(void) const (?State#CPositionRequestor##QBEHXZ)'
Header file:
class CPositionRequestor : public CActive
{
// ...
private:
TInt iState;
public:
inline TInt State() const;
}
CPP file:
inline TInt CPositionRequestor::State() const
{
return iState;
}

An inline function needs it's definition to be available in the file that is calling that function.
So if You define it in one cpp file and try to call it in a second cpp file, then it will not be found.
What You need to do is move this definition into the h file. (just cut & paste it after the class definition).
Or as #einpoklum has noticed, if You don't need it, remove the inline from all the definitions.

Related

Initializing static field [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 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.

Undefined reference char* [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 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.

Embarcadero C++ Builder - Public Function [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 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");
}

C++ linkage error for inline member function [duplicate]

This question already has answers here:
Why are C++ inline functions in the header?
(8 answers)
Closed 9 years ago.
I have the following code:
IFile.h
class IFile
{
public:
IFile();
~IFile(void);
inline bool IsValidFileType() const;
};
IFile.cpp
IFile::IFile()
{
//IsValidFileType();
}
IFile::~IFile(void)
{
}
inline bool IFile::IsValidFileType() const
{
return true;
}
main.cpp
int main(int argc, char* argv[])
{
IFile* pFile = new IFile();
pFile->IsValidFileType();
return 0;
}
When compiling the code I get the following error:
error LNK2019: unresolved external symbol "public: bool __thiscall IFile::IsValidFileType(void)const " (?IsValidFileType#IFile##QBE_NXZ) referenced in function _main
If I change wither "inline" or "const" qualiferes for the function, or call it inside the constructor, the program will complile.
Can you please explain this behaviour?
How can the compiler inline a function whose code it cannot see while it is compiling? When compiling main.cpp, the compiler is being asked to do just this.
An inline function's code gets compiled into each translation unit that references it (that's the idea, after all). Meaning, you need to include the code in the header file.
The inline keyword promises to the compiler that it will be able to see the definition in each translation unit (*.cpp file) in which it is used. You break this promise, since main.cpp can't see the definition although it includes IFile.h.
Usually functions with the inline keyword should be defined in a header file, not a source file.
Since the function is inline, you have to define it in the header file, not in the cpp file.

is it possible to have a static field in C++? [duplicate]

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