-5

I have a main.cpp file which contains the following:

#include<iostream>
using namespace std;

#include"Command.cpp"

#define EXIT 5

int main(){
    int code;
    do{
        code = getCommand();
        doCommand(code);
    }while(code != EXIT);
}

and in my Command.cpp file, I have some functions:

#include<iostream>
using namespace std;

#include"Service.h"

Service * first = new Service();

int getCommand(){
    cout<<"Choose one of the following commands: "<<endl;
    cout<<"1. Add new service"<<endl;
    cout<<"2. Add new subservice"<<endl;
    cout<<"3. Add parent to a service"<<endl;
    cout<<"4. Delete a service(and it's subservices)"<<endl;
    cout<<"Your Choice: ";
    int c;
    cin>>c;
    return c;
}
void addService(){
    first->add();
}

void addSubService(){
    cout<<"Let's choose the parent first: "<<endl;
    int * one = new int;
    *one = 1;
    first->print(one,0);
    cout<<"0. here."<<endl<<"> ";
    int id;
    cin>>id;
    Service * f = first->find(one,id);

}

void addParentToService(){

}

void doCommand(int c){
    switch(c){
    case 1:
        addService();
        break;
    case 2:
        addSubService();
        break;
    case 3:
        addParentToService();
        break;
    case 4:
        //deleteService();
        break;
    }
}

But when I hit the compile button in Visual Studio, I get the following error:

1>Command.obj : error LNK2005: "void __cdecl addParentToService(void)" (?addParentToService@@YAXXZ) already defined in Source.obj
1>Command.obj : error LNK2005: "void __cdecl addService(void)" (?addService@@YAXXZ) already defined in Source.obj
1>Command.obj : error LNK2005: "void __cdecl addSubService(void)" (?addSubService@@YAXXZ) already defined in Source.obj
...

I believe the problem is in the linking of these files but I don't know what to do...

Masious
  • 431
  • 5
  • 14

2 Answers2

4

You should never include cpp files into other cpp files. This is where using a .h file comes in. You can refer to this SO question on how to do that:

Using multiple .cpp files in c++ program?

Essentially, the problem you are encountering is that you are including your Command.cpp file multiple times. The preprocessor takes a the contents of a file, and directly copies it into the file it is included in, and that means that you might end up having multiple definitions of the same object if your files are not guarded from being re included. (This is also where include guards come into play).

This is an overall large topic to cover, and there are many resources that talk about this.

Community
  • 1
  • 1
dhalik
  • 455
  • 3
  • 11
0

When you want to link functions across different files, you should never include .cpp files within another .cpp file. You only include .hh files, where you define any other functions, which are implemented across other .cpp files.

Example: main.cpp

#include "42.hh"
#include <iostream>

int main() {
  func42();
  return 0;
}

Include file: 42.hh

#ifndef _42_HH_
#define _42_HH_
void func42();
#endif

Function file: 42.cpp

#include <iostream>

void func42() {
  std::cout << "Fourty-two" << std::endl;
}

Compile and run:

~$ g++ 42.cpp main.cpp -o fourty-two
~$ ./fourty-two 
Fourty-two
Adama
  • 720
  • 2
  • 5
  • 23