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

Related

const causes a linker error for external file [duplicate]

This question already has answers here:
Why does const imply internal linkage in C++, when it doesn't in C?
(7 answers)
Closed 1 year ago.
I have this cpp file which contains 2 arrays:
const float arr[123]={/*..*/};
const float ave[213]={/*..*/};
And in my main.cpp I have:
int main()
{
extern float arr[123];
float temp[123];
for(unsigned i=0; i<123;i++)
temp[i]=arr[i];
return 0;
}
When I build my project I got an error saying that there’s no definition for arr.
It’s a linker error.
What could be the problem?
Your const variable has internal linkage by default.
You need to declare it as extern as well to counteract that.

Undefined symbol error when use inline methods in C++ [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 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.

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.

How to reference static member variables from a struct within another class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 8 years ago.
I'm trying to define some static constant strings in C++ and reference them from different files.
Here's how I have the information set up this far:
structClass.h
namespace test {
typedef struct customstructure{
static const std::string stringA;
} customstructure;
}
structClass.cpp
namespace test {
static const std::string customstructure::stringA = "This is String A";
}
Now I'm wondering how I would call this in a third file?
execute.cpp
void printStringA(){
printf("stringA is: %s", test::customstructure::stringA.c_str());
}
gives me a compile error that says:
undefined reference to 'test::customstructure::stringA'
In this code:
namespace test {
static const std::string customstructure::stringA = "This is String A";
}
remove the word static. In fact it is an error to have this, your compiler should give a more useful error message (although I suppose 'undefined reference' meets the requirements of "a diagnostic").
Standard reference: [class.static.data]#5 says that static data members have external linkage, however using the keyword static in the definition would specify internal linkage.

how to use a static char [duplicate]

This question already has answers here:
Linker error when using static members
(2 answers)
Closed 10 years ago.
I am currently working with a wxWidgets project where I have to copy a wxString to a static c string that can hold the value for the life time of the program. Essentially my headerfile and source file look like this:
*****************PortDialog.h*****************
...
static char *portName;
-----------------------end
and the source file is;
***************PortDialog.cpp*****************
.
.
.
wxString str = "COM1";
strcpy(portName, (const char*)str.mbc_str());
---------------------end
However I run into the following linking error.
error LNK2001: unresolved external symbol "public: static char * portDialog::eportName" (?portName#portDialog##2PADA)
Can somebody explain to me what is the mistake I am making here? Is it correct to use static char * for the said purpose?
static variables declared in header should be initialized in cpp file see http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr038.htm
Besides the explanations you have received regarding the linker error, you should also be aware that you cannot call strcpy with the destination as a char * which has not been allocated memory to hold the source string. It would compile (and link) but could do just about anything during run-time.
static Variables should be declared in the cpp file
char* PortDialog::PortName = NULL;
Like u do to access methods(functions) of a class using Scope Resolution Operator
void PortDialog::SomeFunction()
{
//Code Goes here
}