类方法的多重定义
Multiple definition of class method
我有文件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) 文件,或者必须在它们前面使用 inline
关键字。否则,函数的副本将放入包含标头的每个源文件中,并标记为可供其他模块使用,并且链接器会在有多个模块时进行投诉。
你说你在main中包含long_arithm.cpp
。但是您也可以单独编译它,然后尝试将结果与 main.o
.这就是导致重复的原因。
看起来您没有关闭命名空间定义,而是在命名空间内使用其名称来限定函数名称,同时在内部定义它们。将此.cpp文件包含在其他文件中可能会导致不同的.cpp文件中出现多个定义,从而导致上述问题。
相关文章:
- 使用用户定义的参数调用future/async并调用类方法
- 单独定义模板化嵌套类方法的正确语法
- 通过函数指针定义类范围之外的方法
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 编译器无法识别在类声明之外定义的方法
- 定义类模板构造函数的两种方法之间的区别
- C++是否有定义的方法来传递指向类的成员对象的成员函数的指针
- 如何最好地让用户重新定义类方法
- 查询数据库以在 C++ 中创建自定义类集合的最佳方法
- 如何定义和设置指向模板类方法的功能指针
- 对超类方法的未定义引用
- 如何在源文件中定义类并将其声明在标题文件中(而不必使用`class :: method'语法定义类方法)
- 在另一个文件中定义类方法
- Win32应用程序,在调用Windows内容之前调用自定义类方法
- 如何避免在模板专门化中重新定义类方法
- c++:如何定义类方法作为线程的启动例程(使用pthread库)
- 在类体之外定义类方法时出错
- 在构建共享库时多重定义类方法
- 蓝图中的自定义类方法
- c++中的用户定义类方法