2
C:\Users\PC\Desktop\random\main.o:main.cpp:(.text+0x76)||undefined reference to `Tclass::FFunction()'|

I made my own class which is external from the main program and this is the error i get. Here is the code of my program.

The main program(.cpp)

#include<iostream>
#include "Tclass.h"
#include "Tclass.cpp"

using namespace std;

int main(){
    Tclass object;
    object.FFunction();
    return 0;
}

The header file. (.h)

#ifndef TCLASS_H
#define TCLASS_H


class Tclass
{
    public:
        Tclass();
        void FFunction();
};

#endif // TCLASS_H

The c++ style sheet(i think that's what it is called) (.cpp)

#include "Tclass.h"
#include<iostream>
using namespace std;

Tclass::Tclass()
{
    cout << "An object for this class has been created \n";
}

void FFunction(){
    cout << "The function has been created \n";
}

I am using code::block as my IDE. I also created the class with any destructors

5
  • Seems like you are missing the link for that object. Is TCone in a .a, .o, or in .h/.cpp format? Commented Nov 12, 2013 at 21:52
  • it is an .cpp format i believe. You are talking about the functions then it is in a .cpp format, if you are talking about the class, then its in a .h format. Commented Nov 12, 2013 at 21:52
  • 1
    What IDE are you using? It looks like it is not linking Cone.o into the final executable. Are all the .cpp source files included in your project? It would seem that only Main.cpp is a build target. Commented Nov 12, 2013 at 22:11
  • If the library is in .h / .cpp format then just add their .cpp files into your compiler call... ex: g++ Cone.cpp Main.cpp Commented Nov 12, 2013 at 23:12
  • Im using a CODE::BLOCKER ide. I will be updating the code with a much shorter code so you can see the issue better. Commented Nov 13, 2013 at 8:51

1 Answer 1

1

in your .cpp file:

void Tclass::FFunction(){
    cout << "The function has been created \n";
}

instead of:

void FFunction(){
    cout << "The function has been created \n";
}

also, there's no need to include Tclass.cpp in your main.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried it without the Tclass.cpp but it wouldn't run at all. But when i left the Tclass.cpp in the main it ran fine after i added the changes you gave me. Thanks ^^ The error i got when i removed the Tclass.cpp: C:\Users\PC\Desktop\random\main.o:main.cpp:(.text+0x16)||undefined reference to Tclass::Tclass()'|``C:\Users\PC\Desktop\random\main.o:main.cpp:(.text+0x22)||undefined reference to Tclass::FFunction()'| ||=== Build finished: 2 errors, 0 warnings ===|`
@user2985134 that shouldn't happen.
Will this ever hinder the code? If so, ill try to re install the IDE.
will what hinder the code? not including cpp files? it most certainly shouldn't - it's the linker's job to find the function definitions. take a look at this (it's for C, but also applies to C++) stackoverflow.com/questions/5904530/…
So the IDE is at fault? I remember seeing someone else saying something about adding the include"Tclass" to make it run who is also using code::blocks. But then again i seen other who run it without it who are using the same IDE. I also just went a little ballsy and it seems i didn't even need to include the header reference. I do not understand any more. It seems like the header isn't linking at all. Do you reckon this would be a good question to create another thread for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.