0

I am new to c++ programming. I do not have any idea of oop in c++. But I only know the basics like loops, if else structures and switch-case statements. I have decided to write Tic-Tac-Toe in c++. Its too large without Object Oriented Programming. But still I decided to do that. After fixing a bunch of errors, I am getting this error: "Multiple definitions of the same function" and I have no idea how to fix this. I am using four files altogether (in code::blocks). The main file:

//main.cpp
//Tic-Tac-Toe game

#include <iostream>
#include "Untitled1.cpp"
#include "Untitled2.cpp"
#include "Untitled3.cpp"


using namespace std;


int main()
{
    cout << "HII\nWelcome to Tic-Tac-Toe!!!\nEnter 1 to play with yourself\nEnter 2 to play with someone who's with you\nEnter 3 to play with the computer ";
    string num;
    krrish:
    cin >> num;
    //   <EXCEPTION HANDLING>
    while ((num != "1") && (num != "2") && (num != "3"))
    {
        cout << "I guess you didn't understand!!\n\nEnter 1 to play with yourself\nEnter 2 to play with someone who's with you\nEnter 3 to play with the computer ";
        goto krrish;
    }
    //   </EXCEPTION HANDLING>
    if (num == "1")
    {
        playwithyourself();
    }
    else if (num == "2")
    {
        playwithanotherone();
    }
    else if (num == "3")
    {
        playwithcomputer();
    }

    return 0;
}  

In this file I have used three other files containing the functions declared above. Untitled1.cpp:

#include <iostream>

using namespace std;


int playwithyourself()
{
/////4828 LINES OF CODE INSIDE; TOOK ME WEEKS TO WRITE THIS; ITS LONG COZ I DUNNO OOP
}

Untitled2.cpp


#include <iostream>

using namespace std;

int playwithanotherone()
{

//Left empty haha, not written yet


}

Untitled3.cpp


#include <iostream>

using namespace std;

int playwithcomputer()
{


//Left empty haha, not written yet

}

When I am compiling main.cpp its showing this error for the three functions:"multiple definition of '(function name)'" And its also showing this error: "1d returned 1 exit status" Seriously I am messed up.

  • Be sure to use guard in the class and function definitions. Use #ifndef, #define and #endif . – user1436187 Nov 06 '20 at 05:13
  • Does this answer your question? [Multple c++ files causes "multiple definition" error?](https://stackoverflow.com/questions/17646959/multple-c-files-causes-multiple-definition-error) – Geno C Nov 06 '20 at 05:14
  • Does this answer your question? [How to include a cpp file in two different cpp files that are linked?](https://stackoverflow.com/questions/47879734/how-to-include-a-cpp-file-in-two-different-cpp-files-that-are-linked) – JaMiT Nov 06 '20 at 05:29
  • It seems you compile all four cpp files and link them to one executable file.Can you show me what is your build command – Kbdman Nov 06 '20 at 08:52

1 Answers1

0

First Function:

#ifndef TEST_PLAYWITHYOURSELF_H
#define TEST_PLAYWITHYOURSELF_H
#include <iostream>

using namespace std;


int playwithyourself()
{
/////4828 LINES OF CODE INSIDE; TOOK ME WEEKS TO WRITE THIS; ITS LONG COZ I DUNNO OOP
}
#endif //TEST_PLAYWITHYOURSELF_H

Second function:

#ifndef TEST_PLAYWITHANOTHERONE_H
#define TEST_PLAYWITHANOTHERONE_H
#include <iostream>

using namespace std;

int playwithanotherone()
{

//Left empty haha, not written yet


}

#endif //TEST_PLAYWITHANOTHERONE_H

Third function:

#ifndef TEST_PLAYWITHCOMPUTER_H
#define TEST_PLAYWITHCOMPUTER_H
#include <iostream>

using namespace std;

int playwithcomputer()
{


//Left empty haha, not written yet

}
#endif //TEST_PLAYWITHCOMPUTER_H

Use the above code, it will work fine!

user1436187
  • 3,252
  • 3
  • 26
  • 59