-1

I don't know how to add functions of a class outside its scope to it, use them in another class and then compile it.

MyMain.cpp

#include"MyClass.cpp"
int main(){
  MyClass myClass;
  myClass.run();
}

MyClass.cpp

#ifndef MYCLASS_CPP
#define MYCLASS_CPP
#include<iostream>

class MyClass {
  private:
    void usage();
  public:
    void run();
};

void MyClass::usage(){
  std::cout << "usage called" << std::endl;
}

void MyClass::run(){
  usage();
}
#endif

I try to compile it with:

g++ MyMain.cpp MyClass.cpp -o main

With that I get the following error message:

/usr/bin/ld: /tmp/ccN7GfOD.o: in function `MyClass::usage()':
MyClass.cpp:(.text+0x0): multiple definition of `MyClass::usage()'; /tmp/ccLhxS6v.o:MyMain.cpp:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccN7GfOD.o: in function `MyClass::run()':
MyClass.cpp:(.text+0x38): multiple definition of `MyClass::run()'; /tmp/ccLhxS6v.o:MyMain.cpp:(.text+0x38): first defined here
collect2: error: ld returned 1 exit status

If I have understood the concept correctly, the function headers within the class serve only as placeholders. The actual functionality is then "overwritten" by the external functions, which also contain a body. And why does the error message say, that the function is already defined in the MyMain.cpp?

I have also seen that there are many similar questions here, but unfortunately I could not expand my understanding of the basic problem to solve it. Is it possible that I am using the command to build the class with C++ incorrectly or that I can save the #include "MyClass.cpp"?

Kind regards

schande
  • 576
  • 12
  • 27
  • Begin with splitting the source file into an actual *header* file (e.g. `MyClass.h`) which contain only the class itself, and then the source file which defines (implements) the functions of the class. – Some programmer dude Sep 29 '22 at 10:44
  • What happens now is that the class and its functions are defined (implemented) in *both* `MyMain.cpp` and `MyClass.cpp`. When you build with both source files you get the errors because things like functions are only allowed to be defined (implemented) *once*. – Some programmer dude Sep 29 '22 at 10:46
  • Now is also a good time to learn about the concept of [*translation units*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). A *translation unit* is basically a single source file, with all included header files. The compiler only deals with a single translation unit, and has no idea about other possible translation units of your project. It's the *linker* which takes the different translation units (in the form of *object files*) and then combines them into the final executable program. – Some programmer dude Sep 29 '22 at 10:47

1 Answers1

2

Several things wrong. here's the steps to put it right

  1. Rename MyClass.cpp to MyClass.h.

  2. Create a new empty file MyClass.cpp

  3. Move the function definitions MyClass::usage() { .. } and MyClass::run() { .. } from MyClass.h to MyClass.cpp. You should probably also move #include <iostream> but this is not essential.

  4. Add #include "MyClass.h" to MyClass.cpp

  5. Change #include "MyClass.cpp" to #include "MyClass.h" in MyMain.cpp

Then build as you are doing now. That part is correct.

Essentially the technique is to separate your code into declarations and definitions. The declarations go into header files, which are included in the cpp files. The cpp files contain the definitions and are what you compile.

john
  • 85,011
  • 4
  • 57
  • 81