c++classreferencecodeblockslinker-warning

Class undefined reference


Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

This should be a simple problem but I'm still having issues. I've looked at similar questions but they seem a bit more complex then what I'm looking for.

I'm using Code::Blocks 10.05.

I'm getting a undefined reference to my class files. I know this is a linker error but I'm not sure how to fix it. I'm afarid I don't know very much about linking files.

s\Desktop\the cool container\Test 1\Class2.o:Class2.cpp|| undefined reference to `Burrito::Burrito()'|
||=== Build finished: 1 errors, 0 warnings ===|

All my files are in the same folder. My object does nothing, I'm just trying this out and I should not get errors from creating an object from another class.

Yeah, this is from TheNewBoston tutorial... He was able to get this work, so I should be able to get it working.

If I include #include "Burrito.cpp" in the Class2.cpp then it works, but he did not include that.


------------Class2.cpp-----------------

#include <iostream>
#include "Burrito.h"
using namespace std;
int main(){

  Burrito Bo;
    return 0;
}

---------------Burrito.h----------------

#ifndef BURRITO_H
#define BURRITO_H


class Burrito
{
    public:
        Burrito();

};

#endif // BURRITO_H

---------------Burrito.cpp-----------------

#include "Burrito.h"
#include<iostream>

using namespace std;
Burrito::Burrito()
{

}

Solution

  • If I include #include "Burrito.cpp" in the Class2.cpp then it works, but he did not include that.

    That is rarely (I would say never) the solution. Don't #include what is supposed to be a source file in some other source file. The solution is to compile all the files that you need compiled and let the linker deal with linkage problems.

    You didn't compile Burrito.cpp, so of course you are having linkage problems. The solution is easy: Compile Burrito.cpp and add Burrito.o to the set of files you supply to the linker. Alternatively, you could supply both Class2.cpp and Burrito.cpp to the compiler, creating an executable rather than object files as output.