Creating classes in other files

Hello, I am totally new to c++ and programming in general, and am running into issues when trying to create classes in other files. All I want my code to do is print a message, "Howdy", when an object is made. I am using the following codes with CodeBlocks on Mac OS X 10.10.3:

main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "Milo.h"

using namespace std;

int main()
{
    Milo ob;
    return 0;
}


Milo.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef MILO_H
#define MILO_H


class Milo
{
    public:
        Milo();

};

#endif // MILO_H 


Milo.cpp
1
2
3
4
5
6
7
8
9
#include "Milo.h"
#include <iostream>

using namespace std;

Milo::Milo()
{
    cout << "Howdy" << endl;
}


However, whenever I build/run main.cpp, instead of printing "Howdy", CodeBlocks runs whatever main.cpp contained the last time it was built/ran. For example, in this case it printed "Hello World!" instead of "Howdy" since I initially built/ran main.cpp when it contained the generic "Hello world" code.

The build log shows:

-------------- Build: Debug in newClass (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -I -c /Users/Ek/Desktop/newClass/main.cpp -o obj/Debug/main.o
Undefined symbols for architecture x86_64:
"Milo::Milo()", referenced from:
_main in main-7c0b9c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))


-------------- Run: Debug in newClass (compiler: GNU GCC Compiler)---------------

Checking for existence: /Users/Ek/Desktop/newClass/bin/Debug/newClass
Executing: osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script "/Users/Ek/Downloads/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Ek/Desktop/newClass/bin/Debug/newClass "' -e 'end tell' (in /Users/Ek/Desktop/newClass/.)
Process terminated with status 0 (0 minute(s), 0 second(s))


If instead I do not initially run the "Hello world" code, I get a pop up box when I run/build main.cpp with the following message:

"It seems this project has not been built yet. Do you want to build it now?"

If I say yes, nothing happens, and I receive the following message in the build log:


-------------- Build: Debug in testd (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -I -c /Users/Ek/Desktop/testd/main.cpp -o obj/Debug/main.o
Undefined symbols for architecture x86_64:
"Milo::Milo()", referenced from:
_main in main-4e0535.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


I do not encounter this issue if I create the class within main.cpp. Any help would be greatly appreciated. Please let me know if I should provide any additional information.
Last edited on
Hmmm...it prints fine on my Codeblocks (13.12 | 4.7.1) though.

Do you want to check if there are any boxes un-checked for your .cpp files? I think it is your compiler settings.
Thanks, would these boxes be under settings < compiler? I reset the compiler settings, but the same problem persists.

Also, I'm running Codeblocks 13.12 as well.
Right click your .cpp files (the list on the left) and click properties.
Both the "Compile file" and "Link file" boxes are checked, as well as the "Debug" and "Release" boxes under the "Belongs in targets:" heading. The "File is read-only " box is unchecked.
Topic archived. No new replies allowed.