【问题标题】:Multiple definition of class method类方法的多重定义
【发布时间】:2012-05-03 08:42:54
【问题描述】:

我有文件 long_arithm.cpp:

#ifndef LONG_ARITHM.CPP
#define LONG_ARITHM.CPP

#include <iostream>
#include <list>

namespace long_arithm {

    typedef signed char schar;
    enum { error_char = 127 };

    class longint {
    public:
        longint() : minusSign(0), array() { }
        longint(int num) { fromInt(num); }
        longint(std::string str) { fromString(str); }
        longint(const longint& other) : minusSign(other.minusSign), array(other.array) { }

        void fromInt(int num);
        void fromString(std::string str);

    protected:
        schar digtochar(schar num);
        schar chartodig(schar ch);

        inline bool isDigit(schar ch) { /* code */ }
        inline bool isSpaceChar(schar ch) { /* code */ }

    private:
        bool minusSign;
        std::list<schar> array;
    };
};


void long_arithm::longint::fromInt(int num) {
    /* code */
}

void long_arithm::longint::fromString(std::string str) {
    /* code */

long_arithm::schar long_arithm::longint::digtochar(schar num) {
    /* code */
}

long_arithm::schar long_arithm::longint::chartodig(schar ch) {
    /* code */
}

#endif

现在我正在尝试构建它,但出现错误(第 1 行和第 2 行 - Eclipce 标题):

Building target: long_arithmetics
Invoking: Cross G++ Linker
g++  -o "long_arithmetics"  ./long_arithm.o ./main.o   
./main.o: In function `long_arithm::longint::fromInt(int)':
/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:153: multiple definition of `long_arithm::longint::fromInt(int)'
./long_arithm.o:/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:153: first defined here
./main.o: In function `long_arithm::longint::fromString(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:168: multiple definition of `long_arithm::longint::fromString(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
./long_arithm.o:/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:168: first defined here
./main.o: In function `long_arithm::longint::chartodig(signed char)':
/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:204: multiple definition of `long_arithm::longint::chartodig(signed char)'
./long_arithm.o:/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:204: first defined here
./main.o: In function `long_arithm::longint::digtochar(signed char)':
/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:188: multiple definition of `long_arithm::longint::digtochar(signed char)'
./long_arithm.o:/home/gxoptg/Документы/My works/Developing/C++/long_arithmetics/Debug/../long_arithm.cpp:188: first defined here

(请注意,行链接(如:188)已损坏,因为我扔掉了很多注释的代码行。)

为什么我有这些错误以及我应该更正什么?据我了解,

void fromInt(int num);

还有一些是“预定义”,我没有看到该方法的任何其他定义。

感谢您的帮助。

【问题讨论】:

  • 为什么在 .cpp 文件中有标题保护?您是否尝试将它们包含在某个地方?
  • 您应该在 cpp 文件中包含保护,仅在头文件中。另外,请检查您是否不包含 cpp 文件,而仅在其他 cpp 文件中包含头文件。包含 cpp 文件可能会导致链接器错误。
  • @parallelgeek 是的,我将此文件包含在 main.cpp 中,但我将此文件 (main.cpp) 仅用于测试 long_arithm.cpp 中的代码
  • @Riateche 我已经删除了包括警卫,但我仍然有那个错误。你真的认为文​​件扩展名会导致错误吗?
  • 这不是关于文件扩展名,而是关于文件内容。头文件应该包含方法的声明,源文件应该包含方法的实现。应该包含头文件,不应该包含源文件。这是使链接器正常工作的最佳方法。

标签: c++ build compilation compiler-errors multiple-definition-error


【解决方案1】:

在类定义之外定义的函数必须移动到源 (.cpp) 文件中,或者您必须在它们前面使用 inline 关键字。否则,该函数的副本将被放置到每个包含头文件的源文件中,并标记为对其他模块可用,当有多个模块时,链接器会报错。

【讨论】:

    【解决方案2】:

    你说你在 main 中包含了long_arithm.cpp。但是您也可以单独编译它,然后尝试将结果与main.o 链接起来。这就是导致重复的原因。

    【讨论】:

      【解决方案3】:

      看起来您没有关闭命名空间定义并在命名空间内使用它的名称来限定函数名称,同时在内部定义它们。在其他文件中包含这个.cpp文件可能会导致在不同的.cpp文件中出现多个定义,从而导致上述问题。

      【讨论】:

      • 不,抱歉,我在复制粘贴时删除了这个括号,但我在源代码中有它。我已经编辑了消息,
      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      相关资源
      最近更新 更多