【问题标题】:C++ Multiple Definition [Error]C++ 多重定义 [错误]
【发布时间】:2016-08-18 01:30:08
【问题描述】:

错误:`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;
    }

感谢您的帮助!

【问题讨论】:

  • 您是否有另一个文件(不是GameKey.cpp)定义了GameKey::getGameKeywords()
  • 请发帖Minimal, Complete, and Verifiable example。无法在我的本地环境中重现(Windows 7,gcc 4.8.1,编译命令=g++ ExitKey.cpp GameKey.cpp main.cpp -o mainmain.cpp 的内容是int main(){}
  • @Rakete1111 不幸的是我不这么认为。
  • @MikeCAT TDM-GCC 4.9.2 64 位版本
  • @Sean 不要仅仅认为不是这样就跳过检查。

标签: c++ function header definition


【解决方案1】:

我认为您可能在其他地方包含 GameKey.cpp 而不是 GameKey.h

【讨论】:

  • main.cpp 包括 GameKey.h,而不是 .cpp
【解决方案2】:

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

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

例如:-

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

【讨论】:

  • 我是编码新手,我对用于编译的命令一无所知。我正在使用使用 MinGW 编译器的 Dev C++。如何访问编译命令?
  • 我猜你是在 Windows 环境中使用 Dev C++。在这种情况下,我认为您可能在主文件中包含“GameKey.cpp”而不是“GameKey.h”。
  • 当您在 Dev C++ 窗口中单击编译并运行时,您会执行一个命令,就像我在使用 MinGW 编译器的回答中提到的那样。该命令用于在 unix shell 中编译 c++ 代码。希望能帮助到你。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 2017-02-17
  • 2011-05-02
  • 2014-03-25
相关资源
最近更新 更多