This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
In C++ I am not able to link source code file and its header files. I am keeping both files in same folder/directory. Also I am using another class which imports header file and it is the startng point of the application but when I am compiling I am getting following error message:
C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o Marksheet_Test.cpp:(.text+0x74):
undefined reference to `Marksheet::Marksheet(std::string,
std::string)'
C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o Marksheet_Test.cpp:(.text+0xa9):
undefined reference to `Marksheet::dispmessage()'
e:\education\dev-cpp\mingw32\mingw32\bin\ld.exe C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o:
bad reloc address 0x13 in section
`.text$_ZN9MarksheetD1Ev[__ZN9MarksheetD1Ev]'
e:\education\dev-cpp\mingw32\mingw32\bin\ld.exe final link failed:
Invalid operation
E:\Education\C++ programming\collect2.exe [Error] ld returned 1 exit
status
Here Marksheet is a cpp file of which header I am making and Marksheet_Test is starting point of the application.
Can somebody help me solving this problem?
Code is as follows:
This is code for Marksheet_Test
#include "Marksheet.h"
using namespace std;
int main()
{
Marksheet obj1("Pransanjeet Majumder","IT 114 Objject Oriented programming");
obj1.dispmessage();
}
Following code is of Marksheet.cpp
#include<iostream>
#include "Marksheet.h"
using namespace std;
class Marksheet{
Marksheet::Marksheet(string cname,string instname){
setCoursename(cname);
setinstname(instname);
}
void Marksheet::setCoursename(string cname)
{
coursename=cname;
}
void Marksheet::setinstname(string insname){
instname=insname;
}
string Marksheet::getCoursename()
{
return coursename;
}
string Marksheet::getinstname()
{
return instname;
}
void Marksheet::dispmessage()
{
cout<<"Welcome to the "<<coursename<<"\n";
cout<<"This course is offered by Prof."<<instname<<endl;
}
};
Following code is of Marksheet.h header file
#include<string>
using namespace std;
class Marksheet
{
public:
Marksheet(string,string);
void setCoursename(string);
string getCoursename();
void dispmessage();
void setinstname(string);
string getinstname();
private:
string coursename;
string instname;
};
I am using DEVC++ compiler for compiling the code
You have a class Marksheet around your implementations that is unnecessary.
Change Marksheet.cpp to:
#include<iostream>
#include "Marksheet.h"
using namespace std;
Marksheet::Marksheet(string cname,string instname) {
setCoursename(cname);
setinstname(instname);
}
void Marksheet::setCoursename(string cname) {
coursename=cname;
}
void Marksheet::setinstname(string insname) {
instname=insname;
}
string Marksheet::getCoursename() {
return coursename;
}
string Marksheet::getinstname() {
return instname;
}
void Marksheet::dispmessage() {
cout<<"Welcome to the "<<coursename<<"\n";
cout<<"This course is offered by Prof."<<instname<<endl;
}
Note that there is no class in the definition file.
What you were doing was declaring a new class called Marksheet and then attempted to define it's own members without declaring them. Also your should not put you using declarations in the header files as then any class that includes them will also have to use the same declaration. This can lead to hard to find conflicts at compile time.
Related
This question already has answers here:
Separating class code into a header and cpp file
(8 answers)
Closed 1 year ago.
Just had a little problem that I haven't been able to figure out yet.
I was using a similar program structure for a different project, but the problem boils down to this. I have two cpp files, which are:
Trading_dte.cpp :
#include <iostream>
using namespace std;
class Dte
{
public:
int addition(int a, int b)
{
return a + b;
}
};
dummy.cpp :
#include <iostream>
#include "Trading_dte.hpp"
Dte obj;
int check()
{
std::cout<<obj.addition(6,9);
}
I created a header file called Trading_dte.hpp :
# pragma once
#include <iostream>
class Dte
{
public:
int addition(int a, int b);
};
Now when I try compiling using the command :
g++ Trading_dte.cpp dummy.cpp
I get the error :
/usr/bin/ld: /tmp/ccCcM8R6.o: in function `check':
dummy.cpp:(.text+0x1a): undefined reference to `Dte::addition(int, int)'
collect2: error: ld returned 1 exit status
I'm sure it's something small, but I just can't figure what.
Thanks a lot in advance!
your cpp file need to be written differently
#include "Trading_dte.hpp"
#include <iostream>
int Dte::addition(int a, int b)
{
return a + b;
}
You've created two separate Dte classes, one visible to main and another visible only in Trading_dte.cpp. The one visible to main, defined in Trading_dte.hpp has a declaration of the addition member function but no definition.
Probably the easiest thing to do is to drop Trading_dte.cpp and put the implementation into the class definition in Trading_dte.hpp.
Trading_dte.hpp:
# pragma once
class Dte
{
public:
int addition(int a, int b)
{
return a + b;
}
};
Note that I also removed the #include <iostream> line. You don't need it in the header file because you don't use it in the class.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
It looks basic and it’s probably due to a beginner mistake but I can’t figure out why...
When compiling I get an error like below from int main():
“undefined reference to 'Hello::World::PaintService::PaintService()"
paint.cpp
using namespace Hello;
int main(int argc, char **argv) {
World::PaintService service;
service.start_painting(argc[1]);
}
PaintService and start_painting are defined like below:
paint_service.h
namespace Hello {
namespace World {
class PaintService {
public:
PaintService();
void start_painting(...);
}; } }
paint_service.cpp
namespace Hello {
namespace World {
void start_painting(....) {
... //paint
}
} }
It seems simple to call that start method like service.start_paint() in another class after calling PaintService service, but something is wrong. I have tried lots of variations yet couldn’t figure out :-/ Could someone point out what I’m doing wrong?
Thanks!
To declare a method, you need to include the name of the class.
namespace Hello {
namespace World {
void PaintService::start_painting(....) { ... }
void PaintService::PaintService() { ... }
} // namespace World
} // namespace Hello
https://repl.it/repls/PunySaneSpools#main.cpp
This question already has answers here:
Separating class code into a header and cpp file
(8 answers)
Closed 5 years ago.
I have that common LNK2019 error and can not figure out what is wrong.
Here is my Solution Explorer:
Here is my Rectangle.cpp:
class Rectangle
{
public:
int getArea()
{
return this->width*this->height;
}
int width;
int height;
};
Here is my Rectangle.h:
#pragma once
class Rectangle
{
public:
int getArea();
int width;
int height;
};
Here is my Functions.cpp:
#include <iostream>
#include "Rectangle.h";
using namespace std;
int main()
{
Rectangle r3{ 2, 3 };
cout << r3.getArea();
return 0;
}
I am very new to C++ and it seems I did everything correctly, but still I am getting an error.
Rectangle.cpp should be like this:
#include "Rectangle.h"
int Rectangle::getArea() {
return this->width*this->height;
}
You shouldn't redefine the class definition in the source file, since you already have it in the header file!
When you want to write the body of a class function, you need to write it this way :
#include "Rectangle.h"
int Rectangle::getArea()
{
return this->width*this->height;
}
You do not need to redefine your class into the cpp file.
You define everything inside the header (.h, .hpp), you include it inside the cpp file (#include "Rectangle.h") but you must not redeclare everything in the header file.
By the way, since you are writing a method, you can access member variables directly by width and you do not need to use this->width.
However, I recommand you to use a convention when you are writing your own classes.
My convention is to prefix member variable by a m. (In your case it gives you mWidth or mHeight).
There is other people that have other conventions like m_variable or variable_.
This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 8 years ago.
While linking this code:
#include <map>
using std::map;
#include <string>
using std::string;
class C {
public:
static void dump() {
for (const auto& e : data) {
string(e.first);
}
}
private:
static map<string,map<string,string>> data;
};
int main() {
C::dump();
}
... I get this error:
/tmp/cc4W2iNa.o: In function `C::dump()':
test.cpp:(.text._ZN1C4dumpEv[_ZN1C4dumpEv]+0x9): undefined reference to `C::data'
collect2: error: ld returned 1 exit status
... from g++ (GCC) 4.9.1.
Am I doing anything wrong?
You've declared C::data, but not defined it. Add a definition outside the class:
map<string,map<string,string>> C::data;
In a larger program, which more than one source file, this must go in just one source file to satisfy the One Definition Rule; while the class definition (including the declaration of data) might go in a header to be available wherever it's needed.
My code will be similar to the below code:
class DMLGroup
{
public:
DMLGroup();
~DMLGroup();
void SetName(string name);
private:
string mName;
};
void DMLGroup::SetName(string nm){
mName.assign( nm );
}
int main()
{
string api="API_DML";
DMLGroup * dmlGr = new DMLGroup();
dmlGr->SetName(api.c_str()); //Getting link error with this statement
}
I could able to compile the above code but failed to link using MS visual studio 9 on windows 7 32-bit OS.
Note: I am able to compile, link and run successfully on suselinux-x8664 platform. The problem is only with windows
Please tell me how to resolve this?
The following code compiles & links fine:
#include "stdafx.h"
#include <string>
class DMLGroup
{
public:
DMLGroup() {}
~DMLGroup(){}
void SetName(std::string name);
private:
std::string mName;
};
void DMLGroup::SetName(std::string nm){
mName.assign( nm );
}
int main()
{
std::string api="API_DML";
DMLGroup * dmlGr = new DMLGroup();
dmlGr->SetName(api.c_str()); //Getting link error with this statement
}
What I changed:
#included stdafx.h because it's VS standard - you can turn it off
#includes because it was missing
decorated every use of string with std:: - because most probably you'll use your class definition in a header file and there you simply don't use "using namespace"
supplied default implementation for constructor & destructor
That's all. Check what from the above part is missing or supply an example that exposes the problem & supply the linker error message.