我有文件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);
和其他人是'预定义',我没有看到任何其他定义的方法。
感谢您的帮助。
答案 0 :(得分:6)
必须将在类定义之外定义的函数移动到源(.cpp)文件,或者必须在它们前面使用inline
关键字。否则,该函数的副本将放入包含标题的每个源文件中,并标记为可用于其他模块,并且链接器会在多个文件时出现。
答案 1 :(得分:3)
你说你在main中包含了long_arithm.cpp
。但您也可以单独编译它,然后尝试将结果与main.o
相关联。这就是造成重复的原因。
答案 2 :(得分:1)
看起来你没有关闭命名空间定义,并在命名空间内使用它的名称来限定函数名,同时在里面定义它们。在其他文件中包含此.cpp文件可能会导致在不同的.cpp文件中出现多个定义,从而导致上述问题。