2012-09-07 2 views
2

함수를 통해 상수 클래스를 전달하는 데 문제가 있습니다.이 인수로 const CName을 전달하면 한정자가 삭제됩니다.

컴파일러가 nameTwo.WriteFullName()을 호출하면 바로 한정자를 포기하는 오류가 발생합니다. 클래스가 상수라는 것을 알고 있지만 어떻게 처리해야하는지 알 수 없습니다.

main.cpp:(.text+0x51): undefined reference to `CName::CName()' 
main.cpp:(.text+0x7c): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const' 
main.cpp:(.text+0xbb): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const' 
main.cpp:(.text+0xf7): undefined reference to `CName::WriteFullName(std::basic_ostream<char, std::char_traits<char> >&) const' 
main.cpp:(.text+0x133): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)' 
main.cpp:(.text+0x157): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)' 
main.cpp:(.text+0x1f4): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)' 
main.cpp:(.text+0x22b): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)' 
main.cpp:(.text+0x25f): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)' 
main.cpp:(.text+0x320): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CName const&)' 
main.cpp:(.text+0x347): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, CName&)' 
+1

작고 관련없는 팁 : 당신이하는 것처럼'자동'을 사용하지 마십시오. 예전의 C++ 03 표준에서는 실제로 아무 것도하지 않으며 새로운 C++ 11 표준에서는 변수의 유 형을 추론하는 데 사용됩니다. –

+0

아, 덕분에, 그 옛날 습관이 내 선생님을 우리에게 주입 시켰습니다. 자동은 프로그램의 끝에 자동으로 작성하고 삭제하는 데 사용됩니까? –

+0

함수'const'를 만들기 위해서는 AFTER 함수 이름과 시그너처를 제공해야합니다 :'void WriteFullName (ostream & outstream = cout) const {/ * your code * /}' – Rost

답변

0

당신은 무엇을해야하는 링커 오류입니다

void const WriteFullName(ostream& outstream = cout) 
{ 
    outstream << m_first << ' ' << m_last; 
} 

나는이 오류가 CONST 함수 헤더의 뒤쪽에 넣어 :

기능은 이렇게하여 작성한 헤더 파일에, 즉 모든 소스 파일과 링크하지 않습니다. 난 CName 클래스에 대한 코드가 별도의 소스 파일에있는 것 같아요, 당신은 그것을 구축/링크 잊어 버렸습니다.

편집 : 컴파일 및 링크 여러 파일

하나 개 이상의 소스 파일에서 프로그램을 만들려면 다음과 같이 명령 행에 모든 소스 파일을 통과해야 :

$ g++ main.cpp cname.cpp -o myprogram 

위의 명령은 소스 파일을 모두으로 사용하고 -o 옵션은 컴파일러에게 프로그램의 이름을 지정합니다.

은 또한 다음 링크을 함께 개별적으로 각 소스 파일을 컴파일 할 수 있습니다 :

$ g++ main.cpp -c -o main.o 
$ g++ cname.cpp -c -o cname.o 
$ g++ main.o cname.o -o myprogram 

-c 옵션은 나중에 프로그램을 연결하는 데 사용할 수있는 개체 파일을 생성하는 컴파일러를 알려줍니다. 마지막 명령은 실제로 모든 소스를 컴파일하지 않지만 두 개의 오브젝트 파일을 사용하여 프로그램을 작성합니다.

+0

동일한 파일에 퍼티를 사용하고 있습니다. 구현을 위해 main.cpp cname.h 및 cname.cpp을 사용하고 있는데 전체 헤더와 구현을 게시해야합니까? –

+0

@GenoDiaz 필요하지 않아야합니다. 모든 파일을 컴파일하고 링크하는 데 사용하는 명령입니다. –

+0

불행히도 함께 구현하면 구현 파일이 더 많은 오류를 발생시킵니다.내 멤버 함수에서 범위 정의 오류와 같은 다른 멤버에 액세스하려고합니다. –

관련 문제