3

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

Dennis
  • 3,683
  • 1
  • 21
  • 43
Prerak
  • 57
  • 1
  • 7

1 Answers1

4

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.

Dennis
  • 3,683
  • 1
  • 21
  • 43
  • I have done the changes as suggested but still I am getting the same error – Prerak Jun 18 '14 at 14:35
  • Have you looked at http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix?? Do you have `Marksheet.h` on your `include path`? – Dennis Jun 18 '14 at 14:43