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
+1 Scott Walker · May 7, 2015
This is not an issue with code or compiler, this is an issue with Code::Blocks.

To fix, simply save your project and exit Code::Blocks. Then re-open Code::Blocks and open the project.

I know this was probably too late for this post but for future reference :-)
0 Cardinal Coog · January 13, 2015
C Student: it works from command line. I coded a new set of files substituting "Color" with "Cheese".

Compiled with:

g++ main.cpp Cheese.h Cheese.cpp -o myProgram

Executed with:

./myProgram

Output:

My favorite cheese is cheddar.

Pretty cheesy, huh?;)

Not sure what happened with Code Blocks, but I guess I might have inadvertently changed something. Will reinstall later.

Thanks to everyone for your help!:)
0 Vikash Kumar Bijarnia · January 13, 2015
Try in a new project..totally new project ..and create the new class like this..i had the same problem..but after unchecking those two things ..my code worked.
/images/forum/upload/2015-01-13/4e4ed657123947715ce1d221dbe7cd3c.png
0 Cardinal Coog · January 13, 2015
I tried replying to your suggestion earlier, but it never appeared. Sorry, if post appears twice. Anyway...

Nice try. But, unchecking "add paths to project and use relative path" did not solve the problem.

I also have "virtual destructor' and "has destructor" unchecked.

Again, good thought.
0 Cardinal Coog · January 13, 2015
Thanks for the try. That's a very wise suggestion.  But, sadly, no that did not solve the problem.

I even recreated all new files and made sure the "add paths to project and use relative path" options were unchecked. I also have "virtual destructor" and "has destructor" unchecked.

Again, nice try...
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.
0 Vikash Kumar Bijarnia · January 13, 2015
Bro just create again the class in new project and uncheck 1.add paths to project 2.use relative path..and try again to run the code and let me know.
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.
+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.
+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
  • 2

C++

107,178 followers
About

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

Links
Moderators
Bucky Roberts Administrator