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.
Related
This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 2 years ago.
Having 2 simple files like that:
Main.c:
#include "Initialization.cpp"
int main() {
return 0;
}
and Initialization.cpp:
int main2() {
return 0;
}
I'm getting en error:
..."int __cdecl main2(void)" (?main2##YAHXZ) already defined in Initialization.obj...
What's peculiar when i complied the program the first time everything was ok. After recompilation this error starts appearing.
PS. I'm using Visual Studio c++ 2019
The preprocessor copies everything in the include file into Main.c which will look
int main2() {
return 0;
}
int main() {
return 0;
}
Both Initialization.o and Main.o now have definition for
main2(). Thus, you break the one definition rule and invoke undefined behavior.
This question already has answers here:
Why does "extern const int n;" not work as expected?
(5 answers)
Closed 4 years ago.
Consider the following a.cpp and b.cpp files:
ebra#him:/tmp$ cat a.cpp
const int i = 5;
ebra#him:/tmp$ cat b.cpp
int main()
{
extern int i;
return i;
}
ebra#him:/tmp$ g++ *.cpp
/tmp/ccqBWi4e.o: In function `main':
b.cpp:(.text+0x6): undefined reference to `i'
collect2: error: ld returned 1 exit status
The question is how I can use the i variable that is declared in a.cpp file inside b.cpp?
Note that
I added the keyword const inside b.cpp too, but nothing changed.
I have the same problem with static and static const variables too!
In C++, when you declare a variable to be const at namespace scope it automatically has internal linkage. Adding static will also yield in internal linkage with or without const
Therefore they are not available outside the translation unit, hence the linker error.
Put it in a header file, that way they both have the same value (different variables, same name, same value).
I think you can also override to make it external linkage, but this will get you nothing: no behaviour changes; No efficiency improvement, for int.
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:
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;
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
}