how to use a static char [duplicate] - c++

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
}

Related

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.

Static word in c++ showing error [duplicate]

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 5 years ago.
Linker error: tried to make basic cpp program but there is a linker error saying:unresolved symbol "private: static int complex::count". When i removed the static words, its working fine.
Please find Program sample on this
you must explicitly define your static variable outside of the class. For example like this:
class complex {
....
int static count;
....
};
// initialization
int complex::count = 0;

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

undefined reference error due to use of static variables [duplicate]

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.