0

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:

  1. 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:

enter image description here

Geoff L
  • 765
  • 5
  • 22
  • 3
    Looks like you are compiling your main.cpp immediately to an executable instead of compiling it to an object file, so you can link it afterwards with the object file of the other cpp. Alternatively, you can provide both cpp files to your compiler – JVApen Jun 29 '20 at 08:06
  • 4
    You are not compiling "data_vars_class.cpp" otherwise you'd get an error on `Num::Num() : num(0) { }` (since you haven't declared a constructor taking no arguments) – UnholySheep Jun 29 '20 at 08:07
  • 2
    Does this answer your question? [How do you compile/build/execute a C++ project in Geany?](https://stackoverflow.com/questions/4475362/how-do-you-compile-build-execute-a-c-project-in-geany) – brc-dd Jun 29 '20 at 08:11
  • So i just started learning C++ today, forgive my naivety. Before i started trying to include separate class definitions, i would just hit "Build" and then "Execute" in Geany and it worked fine, even when I had a second file that was included that was just some functions. So now i gather i need to make a makefile for this to work correctly? JVApen would really appreciate it if you could explain HOW to do what you are suggesting, same with UnholySheep. If i have to do a makefile, what would it look like (exactly). how would i implement it? Thanks for your help – Geoff L Jun 29 '20 at 08:23
  • @brc-dd, thanks for the info, upon reading that i'm guessing i need to specify a makefile, but i have no idea how to write, where to put, or how to tell Geany how to use it – Geoff L Jun 29 '20 at 08:25
  • I think that it automatically uses the makefile to "make" the project (shift+f9). There are numerous examples on the internet that may help you write it. It is placed in your project folder, i.e. the one where your main.cpp is kept. A simple tutorial: https://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ – brc-dd Jun 29 '20 at 08:29
  • @GeoffL A simpler approach is to go to Build > Set Build Commands > C++ Commands > Build and set the command to `g++ -Wall -o main.exe main.cpp data_vars_class.cpp -I`. See this for more info : https://wiki.geany.org/howtos/configurebuildmenu – brc-dd Jun 29 '20 at 08:42
  • Hey thanks for that. so In Geany in the "C++ commands" area, beside "Build", I inserted: g++ -Wall -o main.exe main.cpp data_vars_class.cpp -I and for the working directory i put %d. its givening me an error though "Missing path after '-I' ? – Geoff L Jun 29 '20 at 08:50

1 Answers1

2

if you gcc or clang or anything compiler, you should compile data_vars_class.cpp translate unit and link the object file with that of main.cpp, because all of your non-static member function definition are in data_vars_class.cpp.

g++ main.cpp data_vars_class.cpp -o a.out

In your modified part, the reason why you get a link error is if declare a constructor like Num(int n), this will forbid compiler to generate default constructor, hence you have not declare Num() in your class definition.

you could define your class like the following:

class Num
{
    private:
        int num;
    public:
        Num();
        Num(int n);
        int getNum();
}; 
Initial D
  • 81
  • 5
  • i tried this and failed miserably, and now i can't go back because I don't even know what the original execute command was in geany, please see edit above – Geoff L Jun 29 '20 at 09:08
  • wow i changed the execute line to a.out instead of main.o (in retrospect not sure where main.o came from) and now when i hit execute, it works! thanks – Geoff L Jun 29 '20 at 09:24