1

나는 파일 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); 

및 기타는 '사전 정의'이며, 그 방법에 대한 다른 정의는 볼 수 없습니다.

도움 주셔서 감사합니다.

+5

왜 .cpp 파일 내에 헤더 가드가 있습니까? 어딘가에 그들을 포함 시키려고합니까? – parallelgeek

+0

헤더 파일에만 cpp 파일에 guard를 포함시켜야합니다. 또한 cpp 파일을 포함하지 않고 헤더 파일을 다른 cpp 파일에만 포함시켜야합니다. cpp 파일을 포함하면 링커 오류가 발생할 수 있습니다. –

+0

@parallelgeek 예, main.cpp에이 파일을 포함하지만 long_arithm.cpp의 코드를 테스트 할 때만이 파일 (main.cpp)을 사용합니다. –

답변

6

클래스 정의 외부에서 정의 된 함수는 원본 파일 (.cpp)로 이동하거나 파일 앞에 inline 키워드를 사용해야합니다. 그렇지 않으면 함수의 복사본이 헤더를 포함하고 다른 모듈에서 사용할 수있는 것으로 표시된 각 소스 파일에 배치되며 링커는 둘 이상의 파일이있을 때 불평합니다.

1

네임 스페이스 정의를 닫지 않은 것처럼 보입니다. 네임 스페이스 정의를 닫지 않았고 네임 스페이스 내부에서 이름을 정의하는 동안 함수 이름의 정규화에 사용 된 것처럼 보입니다. 이 .cpp 파일을 다른 파일에 포함 시키면 앞에서 언급 한 문제를 일으키는 여러 .cpp 파일에서 여러 정의가 발생할 수 있습니다.

+0

아니요, 죄송합니다.이 브라켓을 복사 할 때 삭제되었지만 소스 코드가 있습니다. 나는 메시지를 편집했다. –

3

메인에 long_arithm.cpp이 포함되어 있다고 가정 해보십시오. 그러나 별도로 컴파일 한 다음 결과를 main.o과 연결해보십시오. 그것이 중복의 원인입니다.