"Undefined symbols" error in C++ - c++

I have Point class that has X, Y and Name as data members. I overloaded
T operator-(const Point<T> &);
This calculates the distance between two points and returns a value
template < typename T>
T Point<T>::operator-(const Point<T> &rhs)
{
cout << "\nThe distance between " << getName() << " and "
<< rhs.getName() << " = ";
return sqrt(pow(rhs.getX() - getX(), 2) + pow(rhs.getY() - getY(), 2));;
}
The main function
int main () {
Point<double> P1(3.0, 4.1, "Point 1");
Point<double> P2(6.4, 2.9, "Point 2");
cout << P2 - P1;
return EXIT_SUCCESS;
}
But the problem is that this program doesn't compile, and I receive this error:
Undefined symbols:
"Point<double>::operator-(Point<double>&)", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Any help is appreciated...

You can't compile non-specialized templates. You have to put the definition code in headers.

You need to put your Point template class in a .hpp file and include that whenever using a Point.

You have to include templates in each file that uses them, otherwise the compiler cannot generate the code for your specific type.
There is also a precedence between operators, which isn't changed when overloading them. Your code will be treated as
(cout << P2) - P1;
Try this instead
cout << (P2 - P1);

Related

Linker error when compiling STL container vector in Solaris Platform

I am trying to use C++ STL container vector on a Solaris platform but I am unable to compile it successfully. I am getting the following error:
Undefined first referenced symbol in file
void std::vector<int>::__insert_aux(int*,const int&) iqdir_r_o/st_database.o
ld: fatal: symbol referencing errors.
No output written to libiq16.so.1
Error:Link failed with exit code 512.
Here is the piece of code I have added:
void st_database::cpp_stl_vector_test(){
std::cout << "Entering Vector Test\n";
std::cout << "--------------------\n";
std::vector<int> vec;
vec.push_back(10);
vec.push_back(20);
std::cout << "Exiting Vector Test\n";
std::cout << "-------------------\n";
}
I have also included the vector header file.

Undefined Symbols For Architecture x86 [duplicate]

This question already has answers here:
Undefined reference to a static member
(5 answers)
Closed 6 years ago.
I am coding in c++ and I am attempting to learn about static variables.
When I wrote my practice code, I got this error message:
Undefined symbols for architecture x86_64:
"pizza::firstLetterFavPizza", referenced from:
pizza::favPizzaFirstLetterChan(char) in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Please help! I don't know what's wrong. The source code is here:
#include <iostream>
class pizza
{
public:
static char firstLetterFavPizza;
char favPizzaFirstLetterChan (char letter = firstLetterFavPizza)
{
pizza::firstLetterFavPizza = letter;
return pizza::firstLetterFavPizza;
}
};
int main()
{
pizza *a = new pizza();
pizza *b = new pizza();
std::cout << a->favPizzaFirstLetterChan('c') << std::endl;
delete a;
std::cout << b->favPizzaFirstLetterChan('b') << std::endl;
delete b;
return 0;
};
You have declared static data member, but not defined it. Add a definition to your code somewhere (in the global namespace):
char pizza::firstLetterFavPizza;

C++ - What's wrong with this separate compilation code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this code that I'm trying to debug. It looks okay to me. But I get an error I don't understand.Here's my code
//struct.h
#ifndef STRUCT_H
#define STRUCT_H
#include <iostream>
using namespace std;
struct Person {
Person();
Person(int a, string n);
Person(const Person &p);
Person &operator=(const Person &p);
~Person();
int age;
string name;
};
#endif
//struct.cc
#include "struct.h"
Person::Person(): age(0), name("noname") {
cout << "Creating default Person" << endl;
}
Person::Person(int a, string n): age(a), name(n) {
cout << "Creating: " << name << "," << age << endl;
}
Person::Person(const Person &p) {
name = p.name;
age = p.age;
}
Person& Person::operator=(const Person &p) {
Person person;
person.name = p.name;
return person;
}
Person::~Person() {
cout << "Destroying: " << name << "," << age << endl;
}
//structMain.cc
#include "struct.h"
#include <iostream>
using namespace std;
int main() {
Person one(21, "Zuhaib");
cout << "I am " << one.name << ". I am " << one.age << " years old" << endl;
Person two;
cout << "I am " << two.name << ". I am " << two.age << " years old" << endl;
two = one;
cout << "I am " << two.name << ". I am " << two.age << " years old" << endl;
}
I compile with
g++ -c struct.cc
g++ -c structMain.cc
g++ -o struct.o structMain.o
I then get the following error
structMain.o: In function `main':
structMain.cc:(.text+0x3b): undefined reference to `Person::Person(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
structMain.cc:(.text+0xb5): undefined reference to `Person::Person()'
structMain.cc:(.text+0x11e): undefined reference to `Person::operator=(Person const&)'
structMain.cc:(.text+0x180): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x18c): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x1b8): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x1e3): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x1f4): undefined reference to `Person::~Person()'
collect2: ld returned 1 exit status
I think I included all the right files. I double checked the declarations and definitions. I'm just not sure why these errors are coming up. It looks fine to me.
Also, in the main function, what happens at the line
two = one;
I wonder this because, I've overloaded the operator=, but I've also defined the copy constructor which also executes when "=" is encountered. So in the above case, does operator= execute or the copy constructor.
Any help would be appreciated.
Thanks
When you use:
g++ -o struct.o structMain.o
g++ tries to produce an executable by using the code from structMain.o and put the resulting executable in struct.o. Since structMain.o does not have all the object code to create the executable, you get the linker errors.
You need to use:
g++ -o MyProgram struct.o structMain.o
Here, g++ tries to produce an executable by using the code from struct.o and structMain.o and put the resulting the executable in MyProgram. Assuming you have all the required object code in those two files, that should work.
g++ -o struct.o structMain.o
This tells g++ to take the object code in structMain.o, turn it into an executable, and name that executable struct.o.
As your compiler does exactly what you tell it to, you have just overwritten the object code in struct.o, which happened to hold the definition of your Person class, so yes, the references to that class by structMain.o remain unresolved.
What you want is:
g++ struct.o structMain.o
...or give an explicit output name that is not also the name of one of your input files...
g++ -o someGuessedExecutableName struct.o structMain.o
A common convention is to name a source file after the class it defines, i.e. name it Person.cc and Person.o instead of struct.cc / struct.o... which would have made that error more visible.

Cygwin + Gdb Simple_Program

I wrote this simple program in cygwin:
#include <iostream>
int main()
{
int a;
float b;
std::cout << "Podaj liczbe calkowita: ";
std::cin >> a;
std::cout << "Podaj liczbe rzeczywista: ";
std::cin >> b;
std::cout << "Liczba a = " << a << std::endl;
std::cout << "Liczba b = " << b << std::endl;
return 0;
}
When I try to debug it with gdb, (compiling with -g option), and I get this as my output:
g++ -o -g test test.cpp
test: In function `mainCRTStartup':
/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: multiple definition of `mainCRTStartup'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/crt0.o:/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: first defined here
test: In function `mainCRTStartup':
/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: multiple definition of `WinMainCRTStartup'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/crt0.o:/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: first defined here
test:cygming-crtbegin.c:(.text+0x60): multiple definition of `__gcc_register_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x10): first defined here
test:cygming-crtbegin.c:(.text+0xd0): multiple definition of `__gcc_deregister_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x80): first defined here
test:crt0.c:(.data+0x0): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.data+0x0): first defined here
test:crt0.c:(.text+0x50): multiple definition of `.weak._Jv_RegisterClasses.__dso_handle'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x0): first defined here
test:crt0.c:(.idata+0x1d0): multiple definition of `__imp__ZSt3cin'
test:crt0.c:(.idata+0x1d0): first defined here
test:crt0.c:(.idata+0x1d8): multiple definition of `__imp__ZSt4cout'
test:crt0.c:(.idata+0x1d8): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.text+0x0): multiple definition of `main'
test:test.cpp:(.text+0xe0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): multiple definition of `.refptr._ZSt4cout'
test:test.cpp:(.rdata+0xb0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt3cin[.refptr._ZSt3cin]+0x0): multiple definition of `.refptr._ZSt3cin'
test:cygming-crtbegin.c:(.rdata+0xa0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): multiple definition of `.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_'
test:test.cpp:(.rdata+0xc0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): multiple definition of `.refptr.__dso_handle'
test:test.cpp:(.rdata+0xd0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZNSt8ios_base4InitD1Ev[.refptr._ZNSt8ios_base4InitD1Ev]+0x0): multiple definition of `.refptr._ZNSt8ios_base4InitD1Ev'
test:cygming-crtbegin.c:(.rdata+0x90): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt3cin[.refptr._ZSt3cin]+0x0): multiple definition of `__fu0__ZSt3cin'
test:cygming-crtbegin.c:(.rdata+0xa0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): multiple definition of `__fu1__ZSt4cout'
test:test.cpp:(.rdata+0xb0): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x35): undefined reference to `_Jv_RegisterClasses'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `_Jv_RegisterClasses'
collect2: error: ld returned 1 exit status
./test.exe
Any ideas on why I am getting this?

What does 'Error LNK2019: unresolved external symbol' mean?

I'm trying to run a program but it won't compile, I get errors. I have changed things, but doesn't seem to work. The code is this:
#include <iostream>
#include <string>
#include "StackLS.h"
using namespace std;
int main()
{
int answer;
char symbol;
char n, N;
StackLS stack;
bool balenced = true;
do {
cout << " ********** MENU ********** " << endl;
cout << " 1. Basic Brackets () " << endl;
cout << " 2. Standard Brackets ()[]{} " << endl;
cout << " 3. User-Defined brackets " << endl;
cout << " Please enter your choice: " << endl;
switch (answer){
case 1:
cout << "Current Setting: () " << endl;
cout << "Enter your expression followed by a ; : " << endl;
cin >> symbol;
do {
if (symbol = '(')
stack.push( '(' );
else
if (symbol = ')' )
{
if (stack.isEmpty())
balenced = false;
else {
symbol = stack.top();
stack.pop();
}
if (balenced)
cout << "Expression is well-formed" << endl;
else
cout << "Expression is not well-formed" << endl;
}
}
while (symbol != ';' && balenced);
stack.pop();
}
}
while (answer != 'n' || 'N');
} // end main
I haven't finished the program. I wanted to make sure that what I have so far will compile before I move on to the next case. Now I will post the errors I am getting. They are:
1>e:\c++ language 2\well-formed expression checker solution\well-formed expression checker project\main.cpp(11): warning C4101: 'n' : unreferenced local variable
1>e:\c++ language 2\well-formed expression checker solution\well-formed expression checker project\main.cpp(11): warning C4101: 'N' : unreferenced local variable
1>e:\c++ language 2\well-formed expression checker solution\well-formed expression checker project\main.cpp(22): warning C4700: uninitialized local variable 'answer' used
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall StackLS::top(void)const " (?top#StackLS##QBEHXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall StackLS::push(int const &)" (?push#StackLS##QAEXABH#Z) referenced in function _main
1>E:\C++ language 2\Well-Form ed Expression Checker Solution\Debug\Well-Formed Expression Checker Project.exe : fatal error LNK1120: 2 unresolved externals
Thanks for the help.
The warnings are just that -- warnings. They don't stop your program from compiling, but you should look at them and try to fix them.
Your program actually compiles OK; the errors are keeping it from linking. That means that after your code has been compiled into machine code, and it's being built into a *.exe file,it turns out that some pieces are missing. It looks as if the StackLS.h file comes with either a C++ source file, or a *.lib or *.dll file; whatever you have, those need to be included when the executable is built, to supply those missing pieces.
It has compiled, you got some Warning about unused Variables.
Linking has failed and so what you are missing is the file where StackLS is implemented.
Just including the header does not makes them "implemented".
So you need something like
StackLS.cpp or the like
You don't have posted that.
The problem is (probably) not with your code, but with the way in which you are invoking your compiler/linker.
You need to compile the source file where int StackLS::top() const and void StackLS::push(int const &) are defined, and give the result to your linker that when linking your executable.
You program used a library called StackLS. This could either be precompiled library or some source code.
You add references to this library using the #include "StackLS.h", to allow the compiler to compile your code. This creates a compiled version of your code.
The next stage is linking your compiled code with the compiled StackLS library. This is the job of the Linker. These days the same program (compiler) usually makes all the calls needed to do this step too (though you can link your self), though technically it's a different step to compilation.
If StackLS is your code, then you must compile that too, or if it's a precompiled library, you need to tell the linker where to find it.
In your make file, you need to add a reference to the StackLS source code or library (it's usually a .dll or .lib type file in this case).
Remove the lines:
char n, N;
StackLS stack;
and see how you go.