C ++多重定义[错误]

时间:2016-08-18 01:30:08

标签: c++ function header definition

错误:`GameKey :: getGameKeywords()'

的多重定义

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;
    }

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我想你可能在其他地方包含GameKey.cpp而不是GameKey.h

答案 1 :(得分:0)

我不确定,因为没有发布用于编译的命令。

一种可能性是在编译命令中重复文件名也可能导致此错误。

例如: -

 var table = $('#example').DataTable({
     "iDisplayLength": 50,
     "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],           
     "order": [[ 0, "asc" ]],        
     "scrollX": true,
     "scrollCollapse": true,
     "fixedColumns": {
         "leftColumns": 2,
     },
     "sScrollXInner": "150%",
     "fixedHeader": true,         
     "drawCallback": function (settings) {
         var api = this.api();
         var visibleRows = api.rows({ page: 'current' }).data();
     },          
 });