I know there are several posts related to this topic, I've read them and can't figure this out. I have created a class and tried to include it in my main.cpp and I'm getting an error.
I followed the example here to try and include a class defined in a .hpp and .cpp file http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/SeparateCompilation.pdf I made 0 changes, even the class name I left as is.
this is my "main.cpp":
#include <iostream>
#include "data_vars_class.hpp"
using namespace std;
int main () {
Num n(35);
cout << n.getNum() << endl;
return 0;
}
this is "data_vars_class.hpp":
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
and this is "data_vars_class.cpp":
#include "data_vars_class.hpp"
Num::Num() : num(0) { }
Num::Num(int n): num(n) {}
int Num::getNum()
{
return num;
}
When I use "Build" in Geany, I get the following error msgs:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\cc4WtpjS.o:main.cpp:(.text+0x1a): undefined reference to `Num::Num(int)'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\cc4WtpjS.o:main.cpp:(.text+0x26): undefined reference to `Num::getNum()'
What am I doing wrong? thanks
EDIT:
Ok thank you everyone for your timely help and advice. Unfortunately I'm still having problems. I will summarize what i have done:
- my three files, "main.cpp", "data_vars_class.hpp", and "data_vars_class.cpp" have not changed whatsoever.
ATTEMPT 1:
I tried making a custom makefile. I created a file "mainmake.txt" with the following content:
mainmake: main.cpp data_vars_class.cpp
gcc -o mainmake main.cpp data_vars_class.cpp -I.
I then tried including this file by opening the Geany "Set Build Commands", and in the independent commands area beside "Make" i tried inserting "mainmake.txt" as the command and %d as the working directory. When I try to "Make" now, it says file not found.
ATTEMPT 2:
As per someones suggestion, i tried to insert, in the "Set Build Commands" are beside "Build"
g++ main.cpp data_vars_class.cpp -o a.out
as the command and %d as the working directory. That gives me an error saying:
data_vars_class.cpp:3:1: error: no declaration matches 'Num::Num()'
so if i go to data_vars_class.cpp and comment out that first line, go back to main.cpp and try the build command again, it says compilation finished successfully. But when I hit the execute button, i would get an error along the lines of
"/.main doesnt exist" or some windows error that apparently means the executable can't be found.
So I went back into the build menu and changed the first "Execute" line at the bottom to main.o, which is the file that has shown up in the working directory, and %d as the working directory. now when i hit "Execute" i get a fatal error from windows, and i have no idea what the execute command was previously so i don't know how to go back.
I'm totally !%@#%@%@'d now
this is what my build menu looks like in this current catastrophe: