나는 파일 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 파일 내에 헤더 가드가 있습니까? 어딘가에 그들을 포함 시키려고합니까? – parallelgeek
헤더 파일에만 cpp 파일에 guard를 포함시켜야합니다. 또한 cpp 파일을 포함하지 않고 헤더 파일을 다른 cpp 파일에만 포함시켜야합니다. cpp 파일을 포함하면 링커 오류가 발생할 수 있습니다. –
@parallelgeek 예, main.cpp에이 파일을 포함하지만 long_arithm.cpp의 코드를 테스트 할 때만이 파일 (main.cpp)을 사용합니다. –