0

As the title says. I have this exact problem with my bigger project, but I decided to check for the same issue on this little test model. It turns out the issue is still present and I've tried everything I could find out there but it's still the same. I've tried the article

The IDE I used is Code::Blocks which manages all compiling and linking (I made sure that the files were added in the project and set for both compiling and linking) and the compiler is gcc.

This is the exact error I get:

main.cpp|9| undefined reference to `Calc::add2(int, int)'

And these are all the files:

main.cpp:

#include <iostream>
#include "add.h"

using namespace std;

int main()
{
    Calc a;
    cout<<"2 + 2 = "<<a.add(2,2);
    return 0;
}

add.h:

#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED

struct Calc
{
    int add(int a, int b);
};

#endif // ADD_H_INCLUDED

add.cpp:

struct Calc
{
    int add(int a, int b)
    {
        return a + b;
    }
};
The Q
  • 16
  • 4
  • 1
    How are you compiling and linking your code? – Brian61354270 Nov 19 '21 at 22:24
  • The content of add.h is the same as of main.cpp. – 273K Nov 19 '21 at 22:32
  • Compiling and Linking is done by IDE, because I don't have much experience. The compiler is gcc. About the add.h being a duplicate... I fixed it, my bad. And the 'undefined reference' article didn't really help – The Q Nov 19 '21 at 22:47
  • Well, you're definitely not compiling and linking `add.cpp`, since if you were, you'd be getting a "multiple definitions of `Calc`" error instead of an undefined reference error. Double check your build tool configuration, and either change the content of `add.cpp` to `Calc::add(int a, int b) { ... }` or make the defintion of `Calc::add` inline by moving it to the header file as written. – Brian61354270 Nov 19 '21 at 22:54

0 Answers0