2016-08-18 5 views
-1

エラー:GameKey :: getGameKeywords()」C++複数の定義[エラー]

GameKey.cppと.H原因エラー `の複数の定義、基本的に全く同じクラスとヘッダ.HありExitKey.cppながらエラーは発生しません。

(私は名前空間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; 
    } 

助けてくれてありがとう!

+0

それはあなたが別のファイル 'GameKey :: getGameKeywordsを定義します(ない' GameKey.cpp'を)() '持っていることは可能ですか? – Rakete1111

+1

[最小限で完全であり、検証可能な例](http://stackoverflow.com/help/mcve)を投稿してください。 'main.cpp'の内容は' int main() 'です。これは、ローカルの環境(Windows 7、gcc 4.8.1、コンパイルコマンド=' g ++ ExitKey.cpp GameKey.cpp main.cpp -o main') {} ' – MikeCAT

+0

@ Rakete1111残念ながら私はそうは思わない。 – Sean

答えて

1

私は、あなたはおそらく他の場所

+0

main.cppには.cppではなく、GameKey.hが含まれています – Sean

0

コンパイルに使用するコマンドが掲載されていないとして、私は確かではないよGameKey.cppの代わりGameKey.hが含まれると思います。

コンパイルコマンドでファイル名が繰り返される可能性がありますが、このエラーが発生する可能性があります。例えば

: -

g++ ExitKey.cpp GameKey.cpp GameKey.cpp main.cpp -o main 
+0

私はコーディングに新しいので、コンパイルに使用するコマンドについては何も知らないです。 MinGWコンパイラを使用するDev C++を使用しています。コンパイルコマンドにアクセスするにはどうしたらいいですか? – Sean

+0

私はWindows環境でDev C++を使用していると推測しています。その場合は、メインファイルに "GameKey.h"ではなく "GameKey.cpp"が含まれている可能性があります。 –

+0

コンパイルをクリックしてDev C++ウィンドウで実行すると、MinGWコンパイラで私の答えで述べたようなコマンドを実行します。このコマンドは、UNIXシェルでC++コードをコンパイルするために使用されます。 希望します。がんばろう! –