C++ "Classes in separate files"

+3 Cardinal Coog · January 12, 2015
I keep getting the error: "undefined reference to burrito::burrito()"

Here is the main file.

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

using namespace std;

int main()
{
    burrito bo;

    return 0;
}

Here is the Header file:
#ifndef BURRITO_H
#define BURRITO_H


class burrito
{
    public:
        burrito();

};

#endif // BURRITO_H

Here is the Class file:

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


using namespace std;

burrito::burrito()
{
    cout << "I'm getting hungry." << endl;
}



Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Alf Nohr · January 12, 2015
hmm, it's working for me. What compiler are you using?

Undefined reference error, usually  appears  because the .o file "which gets created from the .cpp file"  do not exist, and your compiler is not able to link them.
0 Jay Bennett · January 12, 2015
There is nothing wrong with your code (as far as I can see), and it works. I tested your exact code in both Code::Blocks (version "svn build rev 10035") and Visual Studio 2013. Not sure why you are getting that error, maybe try creating a new project and try the code again?
+1 Cardinal Coog · January 12, 2015
I'm using C:B 13.12 Build: Dec 27, 2013

I tried re-writing the code and now I'm getting "undefined reference to 'winMain@16'.
In a few minutes I'll try it in my Linux box and let you know what happens. Would still like to know though :(
+1 Cardinal Coog · January 12, 2015
UGH... Same error in Linux. Using C:B 13.12 Build: Jan 27 2014 - wx2.8.12
(Linux, unicode) - 32 bit

"undefined reference to burrito::burrito()":'(

BTW, the compiler is set to keep the files in the same directory.
0 Alam Khattak · January 12, 2015
/images/forum/upload/2015-01-12/27876e35d36be80bbf459878a20aeabb.jpg/images/forum/upload/2015-01-12/43cab9c798f8006211fe2a3a117eb309.jpg/images/forum/upload/2015-01-12/5f717ef013a5010cf98a86268e111b05.jpg/images/forum/upload/2015-01-12/b46f54562f6e3756e0e049f9493cb51e.jpg
0 Alam Khattak · January 12, 2015
i have also this version 13.12 and it works a shown above
+1 Cardinal Coog · January 12, 2015
That's the same exact code and same compiler. I'm completely lost; unless, there is some strange setting somewhere within C:B that got changed somehow. I'll wait a while longer to see if anyone else has some idea. If not, I guess I'll have to uninstall and reinstall C:B. Weird....:ermm:
+1 Adam Beck · January 12, 2015
I had this issue too with codeblocks.

It worked in visual studio though.

I had to delete my project and remake the classes, but check something like "use relative path" or something like that.
0 Cardinal Coog · January 12, 2015
I deleted/renamed all associated files on the Linux box. I rewrote the main, header, and the function file.
All three new files (Colors.cpp, Color.h, and Color.cpp) are in the same directory.
Using Code::Blocks 13.12
I'm still getting the same error.

In function 'main':
Colors.cpp undefined reference to 'Color::Color()'
=== Build failed.....

Here's the new code:

//Colors.cpp

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

using namespace std;

int main()
{

    Color object1;

    return 0;

}

//Color.h

#ifndef COLOR_H
#define COLOR_H


class Color
{
    public:
        Color();

};

#endif // COLOR_H


//Color.cpp

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

using namespace std;

Color::Color()
{
   cout << "My favorite color is camouflage." << endl;
}



I've never seen anything like this before. All of my other files compile and execute without any problem.
Unfortunately, I don't have visual studio to try that. But, even if I did I still want to know what it won't work in C:B.
0 Vikash Kumar Bijarnia · January 13, 2015
When creating a class in file policy ....uncheck add paths to project and use relative path..and your problem will be solved.
  • 1
  • 2

C++

107,316 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator