Undefined reference to Class::Function error

I've been following TheNewBoston's C++ tutorials and it went just fine until I got to placing classes in different files. I've followed the video to the letter but every time I try to build and run I get the error "undefined reference to Burrito::Burrito". The code is as follows.

main.cpp:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Burrito.h"
using namespace std;

int main()
{
    Burrito bo;
    return 0;
}


Burrito.h:

1
2
3
4
5
6
7
8
9
10
11
#ifndef BURRITO_H
#define BURRITO_H
#include <iostream>

class Burrito
{
    public:
        Burrito();
};

#endif // BURRITO_H 


Burrito.cpp

1
2
3
4
5
6
7
#include "Burrito.h"
using namespace std;

Burrito::Burrito()
{
    cout << "Please work. Pretty please. With a cherry on top.";
}


I just don't understand. I know from the YouTube comments that I'm not the only one who has this problem yet it worked just fine for Bucky. My compiler is Code::Blocks, but then again so is Bucky's which means that the compiler probably isn't the problem. I'd be very grateful to anyone who can figure out why the hell this is happening. Thanks in advance.
If you haven't already, try adding Burrito.cpp to the project you are compiling.
Well it worked, but doesn't having to include the .cpp file kind of defeat the whole point of header files? Thanks for the quick reply, anyway.
Note that EssGeEich doesn't mean you should include the .cpp file by using the #include directive.
Oh. What did he mean?
That if you use an IDE make sure that all .cpp files are in the same project.

EDIT:
Althalus99 wrote:
My compiler is Code::Blocks
Your IDE is Code::Blocks. Your compiler is probably GCC/MinGW.
Last edited on
Did you create a project with main.cpp, Burrito.h, and Burrito.cpp?

Or did you just hit compile on main.cpp, hoping that the linker would be able to "magically" detect that the definition of Burrito::Burrito is in Burrito.cpp, not in main.cpp or Burrito.h?

When you start splitting your program into multiple .cpp files, you're going to need to create a project for that.
(Well, I guess you could always compile it manually over the command line, but regardless....)
Sorry, got the whole IDE/compiler thing mixed up. I'm a beginner at this, as you've probably guessed. How would I tell if they're in the same project? Thanks again for the replies.
@long double main:

I did everything exactly as Bucky did in this video:
http://www.youtube.com/watch?v=NTip15BHVZc
The video doesn't show him creating the project file.
In fact, he already had it set up before he started recording that video.

If you just started out with a blank new file (no project) and then followed his instructions, you would certainly have your three files, but when you hit compile, it'll only compile main.cpp and you'll get that error.

So create a new project first (File > New > Project > Console Project) and then go from there.
Ah. You see, in the very first video he does talk about creating a new project so I've been doing that since the start. If all I needed for the files to be in the same project was to make a new Console Application then yes, my files are all in the same project.
Ah, okay.

Just to make sure, on the left side of the screen (the "Management" pane), under "Projects", you have all three files in your project?
It should look something like
Workspace
  <project name>
    Sources
      Burrito.cpp
      main.cpp
    Headers
      Burrito.h

Oh, I'm a complete and utter moron beyond human comprehension. You see, this isn't my first time watching these tutorials. Absolutely ages ago, I started watching them because I wanted to get into programming. I got to about the 40th video and then, for some reason or another, I stopped. I can't remember why now, various things must have just got in the way. Then a few months back I decided to get back into programming and I immediately came to Bucky's videos because they're very well done. But you see, because I'd forgotten quite a bit I decided to watch back through the tutorials until I got to where I was as a sort of refresher course. I created a new console application and started watching through them again, doing the various things as I went. Only, I saved and stopped for the day before getting back to this, which proved disastrous. You see, when you go to File > Open and open up main.cpp it doesn't open it up as a project, it just opens it up as a file. Only I didn't realize that. So when I tried to do this tutorial it didn't work because I didn't actually have a project to put the classes in, just a reused main. Inevitably, this error happened and I just couldn't find a solution, so I gave up for the time being. Then yesterday I decided to try and get back into it again the same thing happened because I was still using an unconnected main. Thank you so much for this, I understand now and I've finally got it to work. However, I have one last question: how do I open up a project and continue working on it? I can't start a new project all over again every time I don't finish something in one day.
When you create a project, it should have saved a .cbp file with the same name as the project.
Just open that up and you should be good to go. :)
It works! Thank you so much! It frickin' works! Ha ha! Oh my God, I spent so long trying to find an answer on the internet with absolutely no joy and then within my first few hours of being on this site I get an answer! Once again, thank you so very much! Ah, my! Now I can continue programming! Thank you!
Topic archived. No new replies allowed.