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!