레퍼런스 쪽 질문입니다.
글쓴이: nayana / 작성시간: 목, 2004/04/22 - 4:51오후
소스는 다음과 같습니다.
#include <iostream>
using namespace std;
int max( int a, int b )
{
return ( a > b ) ? a : b;
}
int main( void )
{
int a = 123, b = 456;
int ( &f )( int, int ) = max;
cout << max( a, b ) << ' ' << f( a, b ) << endl;
cout << &max << ' ' << &f << endl;
return 0;
}g++ 컴파일을 해보니 수많은 에러가 떨어집니다.
그래서 vc++ 에서 컴파일 해보니...무난히 컴파일이 되었습니다.
그런데...max 의 주소와 f 주소가 같아야하는게 아닌가요...
레퍼런스 f에 max함수는 동일한 영역을 공유하는걸로 알고 있는데..?
다음은 윈도우에서 한결과 입니다.
456 456 00401212 0012FF74
그리고 리눅스에서는 왜 수많은 에러가 떨어지는 이유가 몬가요?
Forums:


Re: 레퍼런스 쪽 질문입니다.
라고 나오는군요. ㅡ_ㅡㅋ FreeBSD에.. g++은.. 2.95.4
http://redage.net
저는 레드핫 9.0, gcc 버젼은 3.2.2 입니다.다음은 오류코드
저는 레드핫 9.0, gcc 버젼은 3.2.2 입니다.
다음은 오류코드 입니다.
레퍼런스하고는 별 상관 없고,
std::max() 와의 이름 충돌일 겁니다. max() 함수의 이름을 바꿔보시압. (또는 namespace 를 명시하여 ::max 라고 적어줘도 될 겁니다.) 또한, ostream 에 함수 포인터를 받아먹는 << 연산자까지 있다는 보장은 (아마도) 없을 겁니다. 주소를 알아보고 싶으시다면 void* 형으로 캐스트하시길. 이렇게 해 주면 g++ 3.3.2 에서는 제대로 답 나옵니다.
그건 그렇고, 에러가 떨어진다는 막연한 질문 대신 어떤 에러가 떨어지는지 잘 살펴보시고 정확히 올려놓을 수 있도록 노력해보시기 바랍니다. 답변자가 답하기 어려운 건 둘째치고라도, 살펴보다 보면 답이 스스로 나옵니다. 자세히 살펴보셨다면 아마도 수많은 에러 메시지가 떨어지지만 사실 진짜 에러는 다음의 단 한 개 뿐이었다는 것을 아실 수 있었을 겁니다. (g++ 3.3.2 의 에러 메시지입니다.)
test.cc:16: error: no match for 'operator<<' in 'std::cout << &std::max'그리고 그 뒤를 줄줄줄 이어 쏟아지는 메시지들은 다음과 같은 것들이었을 것입니다. (가독성을 위해 이름을 줄이고 컬럼을 맞췄습니다.)
.../bits/ostream.tcc:63: error: candidates are: ostream& ostream::operator<<(ostream&(*)(ostream&)) .../bits/ostream.tcc:85: error: ostream& ostream::operator<<(ios&(*)(ios&)) .../bits/ostream.tcc:107: error: ostream& ostream::operator<<(ios_base&(*)(ios_base&)) .../bits/ostream.tcc:179: error: ostream& ostream::operator<<(long int) .../bits/ostream.tcc:216: error: ostream& ostream::operator<<(long unsigned int) .../bits/ostream.tcc:154: error: ostream& ostream::operator<<(bool) .../ostream:178: error: ostream& ostream::operator<<(short int) .../ostream:189: error: ostream& ostream::operator<<(short unsigned int) .../ostream:193: error: ostream& ostream::operator<<(int) .../ostream:204: error: ostream& ostream::operator<<(unsigned int) .../bits/ostream.tcc:242: error: ostream& ostream::operator<<(long long int) .../bits/ostream.tcc:280: error: ostream& ostream::operator<<(long long unsigned int) .../bits/ostream.tcc:306: error: ostream& ostream::operator<<(double) .../ostream:219: error: ostream& ostream::operator<<(float) .../bits/ostream.tcc:331: error: ostream& ostream::operator<<(long double) .../bits/ostream.tcc:356: error: ostream& ostream::operator<<(const void*) .../bits/ostream.tcc:128: error: ostream& ostream::operator<<(streambuf*) .../bits/ostream.tcc:472: error: ostream& operator<<(ostream&, _CharT) .../ostream:436: error: ostream& operator<<(ostream&, char) .../bits/ostream.tcc:508: error: ostream& operator<<(ostream&, char) .../ostream:447: error: ostream& operator<<(ostream&, signed char) .../ostream:452: error: ostream& operator<<(ostream&, unsigned char) .../bits/ostream.tcc:543: error: ostream& operator<<(ostream&, const _CharT*) .../bits/ostream.tcc:580: error: ostream& operator<<(ostream&, const char*) .../bits/ostream.tcc:630: error: ostream& operator<<(ostream&, const char*) .../ostream:486: error: ostream& operator<<(ostream&, const signed char*) .../ostream:491: error: ostream& operator<<(ostream&, const unsigned char*)오버로드된 << 연산자들의 나열로서 위 에러 메시지에 대한 부가적인 정보에 불과합니다. 즉 문제는 "함수 템플릿인 std::max() 를 받아먹는 << 연산자를 찾을 수 없었다" 로 요약됩니다. 그리고 이것은 처음에 적은 대로 std::max() 와 ::max() 와의 이름 충돌 때문인 거고요.p.s. 대강 replace 하다보니 위 에러 메시지에 약간 이상한 부분이 남아 있군요. 적당히 ^^; 읽어주시기 바랍니다.
흠..
#include <iostream> using namespace std; int maxim( int a, int b ) { return ( a > b ) ? a : b; } int main( void ) { int a = 123, b = 456; int ( &f )( int, int ) = maxim; cout << maxim( a, b ) << ' ' << f( a, b ) << endl; cout << (void*) &maxim << ' ' << (void*) &f << endl; return 0; }함수 이름은 모회사의 제품과 약간 상관이 있습니다. ㅋ
원하시는 결과가 맞는지요?
http://redage.net
댓글 달기