-2

I have three files:

A.h

    #ifndef A_H
    #define A_H

    #include <iostream>
    #include <string>

    using namespace std;

    //FUNCTIONS
    void main();
    int menu();
    void inputData();
    void processData();
    void storeData();
    // void viewData();

    #endif

A.cpp

    #include "B.cpp"

void main() {

    int uChoi;
    uChoi = menu();

    if (uChoi = 0) {
        cout << "Hi" << endl;
        // inputData();
    }
    else {
        cout << "Bye" << endl;
        // viewdata();
    }

}

B.cpp

#include "A.h"

int menu() {    
    int c;
    int userChoice;

    bool validChoice = false;
    do {
        cout << "Please make your choice." << endl;
        cin >> userChoice;
        if (userChoice == 0) {
            c = 0;
            return c;
        }
        else if (userChoice == 1) {
            c = 1;
            return c;
        }
        else {
            validChoice = true;
            cout << "That is not a valid choice." << endl;
        }
    }
    while(validChoice = true);
}

I arrive at the error that my function menu(); has been defined twice:

1>B.obj : error LNK2005: "int __cdecl menu(void)" (?menu@@YAHXZ) already defined in A.obj 1>S:\Documents\Visual Studio 2012\Projects\Database\Debug\Database.exe : fatal error LNK1169: one or more multiply defined symbols found ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

With error code:

Error 2 error LNK2005: "int __cdecl menu(void)" (?menu@@YAHXZ) already defined in A.obj S:\Documents\Visual Studio 2012\Projects\Database\Database\B.obj Database

Please any help or errors spotted in my code are appreciated, cheers :)

using IDE Microsoft visual express 2012

Jules Dupont
  • 7,259
  • 7
  • 39
  • 39

1 Answers1

1

remove the include b.cpp from a.cpp, create an include file named b.h where you define the menu() function.

the problem is that in the way you wrote, a.cpp will have a menu() function compiled on it's object file, and also b. then the linker will not know what menu() function you want (even tougth they are equal).

Tomaz Canabrava
  • 2,320
  • 15
  • 20