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

n00b
  • 35
  • 6
  • Seems like you are missing the link for that object. Is TCone in a .a, .o, or in .h/.cpp format? – Chad Befus Nov 12 '13 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. – n00b Nov 12 '13 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. – paddy Nov 12 '13 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 – Chad Befus Nov 12 '13 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. – n00b Nov 13 '13 at 08:51

1 Answers1

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.

stellarossa
  • 1,730
  • 1
  • 18
  • 29
  • 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 ===|` – n00b Nov 13 '13 at 13:30
  • @user2985134 that shouldn't happen. – stellarossa Nov 13 '13 at 13:50
  • Will this ever hinder the code? If so, ill try to re install the IDE. – n00b Nov 13 '13 at 16:43
  • 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++) http://stackoverflow.com/questions/5904530/understanding-header-and-source-files-in-c – stellarossa Nov 13 '13 at 17:40
  • 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? – n00b Nov 13 '13 at 21:46