Why wont this very simple program with classes and files compile?

Why wont this very simple program with classes and files compile? This code is from a YouTube tutorial. I'm using Code:Blocks 16.01. The code has three files. I compiled all the files with Code:blocks, and selected for them all to be stored in the same file. I can't figure out what I'm doing wrong. Any help appreciated. Thanks.

I click Build and run, and it fails to compile. I get these build messages.

=== Build file: "no target" in "no project" (compiler: unknown) ===
C:\Users\Fish\Documents\C++\FishFish.o:FishFish.cpp
undefined reference to `FISH::FISH()'
error: ld returned 1 exit status
=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//In File: FishFish.cpp. This is the one I click compile and run on. 

#include <iostream>
#include "FISH.h"
using namespace std;

int main ()
{
    FISH bo;
}

// In File: FISH.h

#ifndef FISH_H
#define FISH_H

class FISH
{
    public:
        FISH();
    private:
};

#endif // FISH_H

//In File:  Fish.cpp

#include "FISH.h"
#include <iostream>
using namespace std;

FISH::FISH()
{
   cout << "Fishy fish fish around!"<<endl;
}
You presumably haven't set up your project properly. Both FishFish.cpp and Fish.cpp need to be separately compiled into object files and then those object files need to be "linked" together (with other code from the standard library, etc.) to make the executable.

Your error message is a linker error and it is basically saying that you haven't compiled Fish.cpp and included the object file in the linker command, which should pretty much be automatically done for you if you set up your project properly.

Tpb, thanks for the reply. I think I basically understand the problem now although I have no idea how to fix it yet. I look into some of the things you've said and see if I can figure it out. I copied what the guy on YouTube did in creating the files so I'm not sure why mine failed, and his worked. I guess I need to spend some time trying to learn Code::blocks before my attempt to learn C++.
Maybe watch the video again, especially the first part. The first thing he should've done is create the project and add the source files to it.
I went back and watched things again including how he created projects in the first place (not just how he created the class). I did everything over again, ran it, and this time it worked. Thanks so much for all your help.
Last edited on
Topic archived. No new replies allowed.