0

So I am trying to get practice in with using namespaces and creating header files and linking multiple files together in visual studio. I am getting the errors: "'function int foo::doSomething(int,int)' already has a body' and "'function int goo::doSomething(int,int)' already has a body'. So I have 5 files in total. A main.cpp, foo.cpp, goo.cpp, foo.h, and goo.h. I purposefully have to function with the same parameters and name in order to get used to using namespaces. I also purposefully split them up into multiple files because I want to get comfortable linking files and user custom headers.

My main.cpp is as follows:

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

#include "foo.h"

int main() {

    std::cout << foo::doSomething(4, 3) << '\n'; 
    return 0;


}

my goo.cpp is

:
#include <iostream>
#include "goo.h"
#include "foo.h"

int main() {

    std::cout << foo::doSomething(4, 3) << '\n'; 
    return 0;


}

my foo.cpp is:

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

namespace foo // define a namespace named foo
{
    // This doSomething() belongs to namespace foo
    int doSomething(int x, int y)
    {
        return x + y;
    }
}

my goo.h is:

#pragma once
#include <iostream>
#include "goo.cpp"

namespace goo {
int doSomething(int x, int y);
}

my foo.h is :

#pragma once
#include "foo.cpp"
#include <iostream>

namespace foo {
    int doSomething(int x, int y);

}

All help is truly appreciated. I have the most trouble with linking and working with multiple files and would like to just finally get it down so I do not have to troubleshoot so much in the future. Thanks!

o_gulsan
  • 9
  • 2
  • 3
    Do not `#include` cpp files. – jkb Oct 26 '21 at 18:04
  • Does this answer your question? [Including .cpp files](https://stackoverflow.com/questions/19547091/including-cpp-files) – fabian Oct 26 '21 at 18:06
  • i appreciate you so much – o_gulsan Oct 26 '21 at 19:04
  • Does it help you? [Using multiple .cpp files in c++ program?](https://stackoverflow.com/questions/6995572/using-multiple-cpp-files-in-c-program). As for compiling & linking, please find some tutorial on projects in the context of your favourite IDE. Some introduction to `make` (mostly for Linux users) will also be helpful. `cmake` seems to be good further step. For absolute Linux beginners, `gcc *.cpp` is all one needs. – zkoza Oct 26 '21 at 21:20

0 Answers0