I'm using Dev-C++ 5.2.0.1
I took an example of how to put a class in another file from a website but it resulted in an error.
In the file class.h I have:
class MyClass
{
public:
void foo();
int bar;
};
In the file class.cpp I have:
#include "class.h"
void MyClass::foo()
{
cout<< "test";
}
In the file main.cpp I have:
#include "class.h"
using namespace std;
int main()
{
MyClass a;
a.foo();
return 0;
}
Here is the error I get:
[Linker error] C:\Users\Matthew\AppData\Local\Temp\cccWe7ee.o:main.cpp:(.text+0x16): undefined reference to `MyClass::foo()'
collect2: ld returned 1 exit status
Is there something I'm doing wrong?
New answer.
Are you compiling and linking all of your files together? In gcc you would do something like:
gcc -o myExe class.cpp main.cpp
I'm not so sure about dev-c++, but I imagine it's not much different.
foo() only have definition. If you want to use a function that doesn't have a implement, Linker will give you this error "undefined reference".
Related
Here is an example C++ code.
header.h
class example {
int a;
int b;
public:
int sum(int i,int j);
};
cpp.cpp
#include<iostream>
#include"header.h"
int example::sum(int i,int j){
a=i;
b=j;
return a+b;
}
int main(){
example e1;
int b=e1.sum(32,34);
std::cout<<b<<std::endl;
return 0;
}
main.cpp
#include<iostream>
#include"header.h"
int main(){
example e1;
int b=e1.sum(32,34);
std::cout<<b<<std::endl;
return 0;
}
When I use the code in powershell g++ cpp.cpp
I get a file a.exe without any problem. When I run the main.cpp it shows the error
d:/program files/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\KISHOR~1\AppData\Local\Temp\ccIMGM4v.o:main.cpp:(.text+0x20): undefined reference to `example::sum(int, int)'
collect2.exe: error: ld returned 1 exit status
If I have to use the class in a file called main.cpp how do I it?
I am working on a bunch of files which has similar usb.cpp and usb.h file. But the cpp fle containing the main program imports only header file.
How to tell g++ that definition of the header file is given in cpp.cpp?
You should compile with the command g++ cpp.cpp main.cpp, and comment out the main function in cpp.cpp.
Given the below class definition in the header file - "class1.h"
#ifndef CLASS1_H
#define CLASS1_H
class class1
{
public:
class1 &fcn();
};
#endif
and the member function fcn is defined in the source file - "class1.cpp"
#include "class1.h"
#include<iostream>
inline class1 &class1::fcn()
{
std::cout << "Welcome to Class1" << std::endl;
return *this;
}
when the following code in "main.cpp" is executed
#include <iostream>
#include "class1.h"
int main()
{
class1 myclass;
myclass.fcn();
}
it produces the following error
C:\...\rough>g++ main.cpp class1.cpp && a
C:\...\Local\Temp\ccJvpsRr.o:main.cpp:(.text+0x15): undefined reference to `class1::fcn()'
collect2.exe: error: ld returned 1 exit status
What went wrong?
The inline keyword is the problem. You are supposed to use that with functions that are defined in headers. In your case, remove it and it should work fine.
I've got a problem when declaring a class in a separate object file and then using it in the main function of another file:
main.cpp:
#include <helloclass.hpp>
using namespace std;
int main() {
Hello hi;
hi.hello();
return 0;
}
helloclass.cpp:
#include <iostream>
using namespace std;
class Hello {
public:
void hello() {
cout << "Hello world\n";
}
Hello() {}
};
helloclass.hpp:
class Hello {
public:
void hello();
Hello();
};
Then I ran the following commands:
g++ -I. -c main.cpp
g++ -c helloclass.cpp
g++ -o main main.o helloclass.o
However, the last command gives the following output:
main.o: In function `main':
main.cpp:(.text+0x1f): undefined reference to `Hello::Hello()'
main.cpp:(.text+0x2b): undefined reference to `Hello::hello()'
collect2: error: ld returned 1 exit status
To me, it seems like I'm missing something pretty obvious. Does anyone know how to fix this?
You should not redefine the class in the separate source file. Instead include the header-file and implement the Hello::hello function (and the Hello::Hello constructor).
I'm having one of those "undefined reference to " errors when compiling a c++ program. I know this is common pitfall, but so far was unable to figure out what I'm doing wrong.
Here's the relevant code. Ex1Two_Sum.h:
#ifndef EX1TWO_SUM_H
#define EX1TWO_SUM_H
#include <vector>
using namespace std;
namespace ddc {
class Ex1Two_Sum
{
public:
void f();
protected:
private:
};
}
#endif
Ex1Two_Sum.cpp:
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;
namespace ddc {
class Ex1Two_Sum {
public:
void f(){
cout << "works" << endl;
}
};
}
And finally, main.cpp:
#include <iostream>
#include "Ex1Two_Sum.h"
using namespace std;
using namespace ddc;
int main()
{
Ex1Two_Sum ex1;
ex1.f();
return 0;
}
I compile as follows:
g++ -std=c++11 -c Ex1Two_Sum.cpp
g++ -std=c++11 -c main.cpp
g++ Ex1Two_Sum.o main.o
yielding the following message:
main.o: In function `main':
main.cpp:(.text+0x2c): undefined reference to `ddc::Ex1Two_Sum::f()'
collect2: error: ld returned 1 exit status
Your source file redefines the whole class, with an inline function definition, when it just needs to provide a non-inline function definition.
#include "Ex1Two_Sum.h"
void ddc::Ex1Two_Sum::f() {
std::cout << "should work\n";
}
Also, please don't put using namespace std; in a header. Not everyone wants the global namespace polluted in potentially surprising ways.
First, which line of the command throws that error?
Second, I think you forgot to include the Ex1Two_Sum.h in the Ex1Two_Sum.cpp
Third you need to change class ....... in Ex1Two_Sum.cpp to:
void Ex1Two_Sum::f(){...}
I have just created this simple class.When I compile, I am getting the following error.
caller.o: In function main':
caller.cpp:(.text+0x15): undefined reference toReader::Reader(int)'
collect2: ld returned 1 exit status
Reader.h
#ifndef READER_H
#define READER_H
class Reader
{
private:
int m_month;
Reader() {}
public:
Reader(int month);
void SetDate(int month);
int GetMonth() {return m_month;}
};
#endif
Reader.cpp
#include "Reader.h"
Reader::Reader(int month);
{
SetDate(month);
}
void Reader::SetDate(int month)
{
m_month=month;
}
main program
#include <iostream>
using namespace std;
#include "Reader.h"
int main()
{ int i;
i=5;
Reader rd(i);
i=rd.GetMonth();
cout<<i;
return 0;
}
There is a ; semicolon that should not be there.
Reader::Reader(int month)//; remove semicolon from this line !!!
{
SetDate(month);
}
You need to compile all your source files into object files, and then link the object files together to produce the program.
This can be done in one step:
gcc Reader.cpp main.cpp
Or in two separate steps:
gcc -c Reader.cpp main.cpp
gcc Reader.o main.o
Of course, you should normally have an IDE, Makefile or buildsystem generator (such as CMake) take care of this for you.