错误:的`GameKey :: getGameKeywords()”C++多个定义[错误]
GameKey.cpp和.h原因错误多个定义,而ExitKey.cpp和.h基本上完全相同的类和头但不会产生错误。
(我知道如何使用空间std整件事)
//Function Declarations
#ifndef GAMEKEY_H
#define GAMEKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class GameKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
GameKey();
// Deconstructor
~GameKey();
// Get keywords
string getGameKeywords();
};
#endif
GameKey.cpp
//Function Definitions
#include "GameKey.h"
// Constructor
GameKey::GameKey()
{
}
// Deconstructor
GameKey::~GameKey()
{
}
// Get keywords
string GameKey::getGameKeywords()
{
ifstream infile;
infile.open("GameKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
ExitKey.h
//Function Declarations
#ifndef EXITKEY_H
#define EXITKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class ExitKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
ExitKey();
// Deconstructor
~ExitKey();
// Get keywords
string getExitKeywords();
};
#endif
ExitKey.cpp
//Function Definitions
#include "ExitKey.h"
// Constructor
ExitKey::ExitKey()
{
}
// Deconstructor
ExitKey::~ExitKey()
{
}
// Get keywords
string ExitKey::getExitKeywords()
{
ifstream infile;
infile.open("ExitKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
感谢您的帮助!
有没有可能是有另一个文件(不是'GameKey.cpp'),它定义'GameKey :: getGameKeywords()'? – Rakete1111
请发布[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。无法在我的本地环境中重现(Windows 7,gcc 4.8.1,编译命令='g ++ ExitKey.cpp GameKey.cpp main.cpp -o main')'main.cpp'的内容是'int main() {}'。 – MikeCAT
@ Rakete1111不幸的是,我不这么认为。 – Sean