C++;-&引用;“功能的多重定义”;我们如何修复它? 我对C++编程很陌生。我对C++中的OOP没有任何了解。但我只知道循环、if-else结构和switch-case语句等基本知识。我决定用C++编写Tic Tac Toe。如果没有面向对象编程,它太大了。但我还是决定这么做。在修复了一系列错误之后,我得到了这个错误:“同一个函数的多个定义”,我不知道如何修复这个错误。我总共使用了四个文件(在代码::块中)。 主文件: //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; } //main.cpp //井字游戏 #包括 #包括“Untitled1.cpp” #包括“Untitled2.cpp” #包括“Untitled3.cpp” 使用名称空间std; int main() { cout>num; // 而((num!=“1”)&&(num!=“2”)&&(num!=“3”)) { cout

C++;-&引用;“功能的多重定义”;我们如何修复它? 我对C++编程很陌生。我对C++中的OOP没有任何了解。但我只知道循环、if-else结构和switch-case语句等基本知识。我决定用C++编写Tic Tac Toe。如果没有面向对象编程,它太大了。但我还是决定这么做。在修复了一系列错误之后,我得到了这个错误:“同一个函数的多个定义”,我不知道如何修复这个错误。我总共使用了四个文件(在代码::块中)。 主文件: //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; } //main.cpp //井字游戏 #包括 #包括“Untitled1.cpp” #包括“Untitled2.cpp” #包括“Untitled3.cpp” 使用名称空间std; int main() { cout>num; // 而((num!=“1”)&&(num!=“2”)&&(num!=“3”)) { cout,c++,function,compiler-errors,codeblocks,C++,Function,Compiler Errors,Codeblocks,第一个函数: #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_PL

第一个函数:

#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
#如果NDEF测试(你自己玩)
#定义自己的测试
#包括
使用名称空间std;
intplaywithyourself()
{
/////里面有4828行代码;我花了几周的时间写这篇文章;它很长,因为我不知道OOP
}
#endif//TEST_playing_H
第二个功能:

#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
#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
#如果NDEF测试(使用另一个测试)
#用另一个定义测试
#包括
使用名称空间std;
int与其他人一起玩()
{
//留下的是空的哈哈,还没写
}
#endif//使用另一个进行测试
第三项职能:

#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
#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
#如果NDEF测试(使用计算机播放)
#使用计算机定义测试和播放
#包括
使用名称空间std;
int playwithcomputer()
{
//留下的是空的哈哈,还没写
}
#endif//TEST_play with computer_H

使用上面的代码,它会工作得很好!

确保在类和函数定义中使用guard。使用#ifndef、#define和#endif。这能回答您的问题吗?这能回答您的问题吗?您似乎编译了所有四个cpp文件并将它们链接到一个可执行文件。您能告诉我您的构建命令是什么吗?请将错误放在这里。我使用gcc 9.3.0版编译了上述代码。
#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