C++学习中遇到的错误
作为 C++ 初学者,遇到下面这种困难可能会很经常.
解决方案:
产生这个错误的原因是你有两个方法名字是一样的,在使用时并未注明是哪个命名空间中的方法,解决方案是分别对这个方法使用命名空间,在进行其使用时,应遵循命名空间的使用手册。以上是该错误的解决方案,日常学习时,请注意该错误的产生。
请看一下我对自己项目的解决方案:
这是练习中的 game1.h
#include<iostream>
using namespace std;
void goAtk();
下面这段代码是 game1.cpp
#include "game1.h"
void goAtk()
{
cout << "LOL攻击实现" << endl;
}
以下是 game2.h 的代码
#include<iostream>
using namespace std;
void goAtk();
接着,下面是 game2.cpp
#include "game2.h"
void goAtk()
{
cout << "王者荣耀实现攻击" << endl;
}
最后是 namspace.cpp 的代码
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"
int main()
{
goAtk();
system("pause");
return 0;
}
解决后的每个模块代码分别如下所示:
//game1.h
#include<iostream>
using namespace std;
namespace LOL {
void goAtk();
}
//game1.cpp
#include "game1.h"
void LOL::goAtk()
{
cout << "LOL攻击实现" << endl;
}
//game2.h
#include<iostream>
using namespace std;
namespace KingGlory{
void goAtk();
}
//game2.cpp
#include "game2.h"
void KingGlory::goAtk()
{
cout << "王者荣耀实现攻击" << endl;
}
//namespace.cpp
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"
int main()
{
LOL::goAtk();
KingGlory::goAtk();
system("pause");
return 0;
}