undefined reference to constructor c++ - c++

I already searched and found a solution for this problem but i find this a little bit strange. Anyway my problem is this:
Personal.h
class Personal
{
public:
Personal();
int money;
~Personal();
}
Personal.cpp
#include "Personal.h"
Personal::Personal()
{
money = 1800;
}
Personal::~Personal(){};
Now i want to compile in main
main.cpp
#include "Personal.h"
#include <iostream>
#include <vector>
int main()
{
std::vector<Personal> test(100);
}
When I write: g++ -Wall main.cpp -o main it gives me :
undefine reference to Personal::Personal()
undefine reference to Personal::~Personal()
The solution:
g++ -Wall Personal.cpp main.cpp -o main
Why do i need compile the Personal.cpp too?
Or the other main version is to include instead of "Personal.h", "Personal.cpp"
main.cpp
#include "Personal.cpp"
#include <iostream>
#include <vector>
Then the normal g++ -Wall main.cpp -o main works
Can someone help me?

Why do i need compile the Personal.cpp too?
Because you use functions that are defined in that file. In particular, you use the functions Personal::Personal and Personal::~Personal.
Can someone help me?
Make sure that all functions (that are odr-used) are defined in exactly one (or in all files, in case of inline functions) of the source files that you compile and link together.

Related

Having one .cpp file communicate with a class.cpp and a class.h file

I don't know the correct syntax for Dice.h and Dice.cpp to talk to my final game.cpp file.
I am getting a compile error that is returning a undefined reference to Dice::Dice()
Here are my 3 file headers
Game.cpp
#include "Dice.h"
#include <iostream>
using namespace std;
int main()
{ int sum;
Dice dice1;
Dice dice2;
dice1.roll();
dice2.roll();
sum = dice1.getFace() + dice2.getFace();
cout << sum;
return 0;
}
Dice.h
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <ctime>
// definition of class Dice
class Dice //... cont
Dice.cpp
#include "Dice.h"
using namespace std;
Dice::Dice() //... cont
I get the error when i compile by typing g++ -Wall -o game game.cpp
Is this the correct way to compile multiple files?
Just because you have the includes in the files doesn't mean they will be compiled together.
Try g++ -Wall -o game Dice.cpp Game.cpp
...I could be slightly wrong on the command but that should work
Side note you should really look into Makefiles. Makes everything so much easier.
The other answer is close, but slightly off. Enter
++ -Wall -o game game.cpp Dice.cpp
Make sure that game is lower case, and I think the file containing main() needs to go first.

Compiler doesn't see my main function ( Undefined reference to main)

I have 2 cpp and 3 header files in my project. When I compile them in VS it works smoothly and I get no error message. But when I try to compile it on SSH network by this line:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
It says:
In function `_start':
(.text+0x20): undefined reference to `main'
Do not say "do not forget to write main function" because it is already there and my project works on VS. What to do then?
Here is related part from my codes. Program.cpp until main:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include "lineoperations.h"
using namespace std;
line bankline;
bool operate(char);
void search(char[]);
void add(char[]);
void removee(char[]);
void transaction();
void printline();
int main(){
bankline.create();
bool end = false;
while (!end) {
end = bankline.decideFunction();
}
bankline.close();
return EXIT_SUCCESS;
}
It goes on but it is not necessary to paste them I guess. If you need to see other cpp file or header files I'll paste them as well.
The command:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
tells g++ to compile and link the files:
lineoperations.cpp customer.h transaction.h lineoperations.h
and output an executable program called program.cpp.
This fails with the linkage error you have observed because
main is defined in program.cpp, which you are not compiling or linking.
Try this instead:
g++ -o program program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
or if you are on Windows:
g++ -o program.exe program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
And BTW, there is no need to list the header files on the commandline. They are included by
the source files, I presume.

How to use header files?

I wanted to learn using header files. and I got an error. here is my code:
printmyname.h:
void printMyName();
printmyname.cpp:
#include "printmyname.h"
void printMyName() {
cout << "omer";
}
try.cpp (main file):
#include <iostream>
#include "printmyname.h"
using namespace std;
int main() {
printMyName();
return 0;
}
Here is the error:
undefined reference to `printMyName()`
What's is the problem?
Undefine reference has nothing to do with your header file in this case. It means the linker cannot find the implementation of printMyName which is in printmyname.cpp. If you are using g++, you should try:
g++ try.cpp printmyname.cpp -o yourBinaryName
If you are using a makefile, you should add dependency(printmyname.cpp) correctly for try.cpp.
Edit:
As #zmo suggest in his comment:
you can also do it through a two times compilation (more suitable with Makefiles):
g++ -c printmyname.cpp
g++ try.cpp printmyname.o -o yourBinaryName
If you are using Windows, you need to add the printmyname.cpp to your project too.
Consider adding an include guard to your header
#ifndef PRINTMYNAME_INCLUDED
#define PRINTMYNAME_INCLUDED
void printMyName();
#endif
You will also need to move the #include <iostream> and using namespace std; from the try.cpp to the printmyname.cpp file.
You need to add code/definition in printMyName.cpp inside printMyName.h only.
void printMyName();
{
cout << "omer";
}

Error: undefined reference to `censorship()' [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?
I have main.cpp:
#include "censorship_dec.h"
using namespace std;
int main () {
censorship();
return 0;
}
this is my censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void censorship();
this is my censorship_mng.cpp:
#include "censorship_dec.h"
using namespace std;
void censorship()
{
cout << "bla bla bla" << endl;
}
I tried to run these files in SSH (Linux), so I wrote: make main, but I got:
g++ main.cpp -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
please help!
You have to specify the file where censorship is defined.
g++ main.cpp censorship_mng.cpp -o main
You must add censorship_mng.cpp in your compilation command:
g++ main.cpp censorship_mng.cpp -o main
Another solution (if you really don't want change your compile command) is making void censorship(); to a inline function and move it from .cpp to .h.
censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}
And remove void censorship() from censorship_mng.cpp file.
once your project starts using several source-files to be compiled into a single binary, manual compilations become tedious.
this is usually the time when you start using a build-system, such as a Makefile
a very simple Makefile that uses default build-rules could look like
default: main
# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm
# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o
# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)

undefined reference to a class ERROR

I am working in c++ /ubuntu.
I have:
libr.hpp
#ifndef LIBR
#define LIBR
#include <string>
using namespace std;
class name
{
public:
name();
~name();
std::string my_name;
std::string method (std::string s);
};
#endif
and
libr.cpp
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
name::name()
{
}
std::string name::method(std::string s)
{
return ("YOUR NAME IS: "+s);
}
From these two I've created a libr.a.
In test.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
int main()
{
name *n = new name();
n->my_name="jack";
cout<<n->method(n->my_name)<<endl;
return 0;
}
I compile with g++ and libr.a. I have an error: "name::name() undefined reference", why?
I would like to mention that I've added in qt creator at qmake the .a. When I compile, I have the error. How can I solve it?
This is a linker error, not a compiler error. It means that you have called but you have not defined the constructor. Your allocation name *n = new name(); calls the constructor.
Since you defined the constructor in your libr.cpp, what this means is that this compilation unit is not making its way into your executable. You mentioned that you are compiling with libr.a. When you compile your libr.cpp the result is a .o file, not a .a file.
You are not linking libr.o into your executable.
What are the steps you're using to compile your "project"?
I performed the following steps and managed to build it with warnings/errors.
g++ -Wall -c libr.cpp
ar -cvq libr.a libr.o
g++ -Wall -o libr main.cpp libr.a
One last thing, if I change the order off the last command, like
g++ -Wall -o libr libr.a main.cpp
I get the following error:
Undefined first referenced
symbol in file
name::name() /tmp/cc4Ro1ZM.o
name::method(std::basic_string<char, std::char_traits<char>, std::allocator<char
> >)/tmp/cc4Ro1ZM.o
ld: fatal: Symbol referencing errors. No output written to libr
collect2: ld returned 1 exit status
in fact , you needn't define the destructor yourself because the default destructor will be used when the class calling is over.
and in the VS2008,it's all right!