-1

Error: multiple definition of `GameKey::getGameKeywords()'

GameKey.cpp and .h cause error, while ExitKey.cpp and .h are essentially the exact same class and header but do not produce an error.

(I know the whole thing about using namespace 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;
    }

Thanks for any help!

Sean
  • 1
  • 2
  • Is it possible that you have another file (not `GameKey.cpp`) that defines `GameKey::getGameKeywords()`? – Rakete1111 Aug 18 '16 at 01:33
  • 1
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Couldn't reproduce in my local environment (Windows 7, gcc 4.8.1, compile command = `g++ ExitKey.cpp GameKey.cpp main.cpp -o main`) The contents of `main.cpp` is `int main(){}`. – MikeCAT Aug 18 '16 at 01:33
  • @Rakete1111 Unfortunately I don't think so. – Sean Aug 18 '16 at 01:58
  • @MikeCAT TDM-GCC 4.9.2 64-bit Release – Sean Aug 18 '16 at 02:11
  • @Sean Do not skip checking by just thinking that is not the case. – MikeCAT Aug 18 '16 at 02:15

2 Answers2

1

I think you probably include GameKey.cpp instead of GameKey.h elsewhere

ggrr
  • 7,737
  • 5
  • 31
  • 53
0

I am not certain as the command used for compilation is not posted.

One possibility is repeating the file names in your compilation command could also lead to this error.

for example :-

g++ ExitKey.cpp GameKey.cpp GameKey.cpp main.cpp -o main
  • I am new to coding, I do not know anything about commands used for compilation. I'm using Dev C++ which uses a MinGW compiler. How do I access the compilation command? – Sean Aug 18 '16 at 02:28
  • I am guessing that you are using Dev C++ in Windows environment. In that case I think you might have included "GameKey.cpp" instead of "GameKey.h" in your main file. – Ghanshyam Bhutra Aug 18 '16 at 04:35
  • When you click on compile and run in the Dev C++ window you execute a command like the one I mentioned in my answer with the MinGW compiler. The command is used to compile c++ codes in unix shells. Hope it helps. good luck! – Ghanshyam Bhutra Aug 18 '16 at 04:39