2012-03-12 3 views
-1

찾는 방법에 gcc가 차이가 나는이는 선언

// util.h 
// 
// ... lots of stuff ... 

std::istream& operator>>(std::istream& is, const char *str); 
std::istream& operator>>(std::istream& is, char *str); 

그리고

// util.cpp 
// 
// lots of stuff again 
//! a global operator to scan (parse) strings from a stream 
std::istream& operator>>(std::istream& is, const char *str){ 
    parse(is, str); return is; 
} 
//! the same global operator for non-const string 
std::istream& operator>>(std::istream& is, char *str){ 
    parse(is, (const char*)str); return is; 
} 

과 같은 istreams에 대한 util.h 연산자의 오버로드/cpp를 >> (. V 4.6.3)

std::istream file; 
char *x, *y; 

// opening and allocating space for strings comes here 

file >> "[ " >> x >> "," >> y >> " ]"; 

이것은 GCC/g와 완벽하게 잘 ++ 작동하지만 지금은 C를 사용하고 싶었 : 다른 파일에 I는 다음과 같이 구성이 사용 랭 (V 3.0) 및 해당 연산자 오버로딩을 찾을 수 없다는 진술 오류를 가지고 : GCC는 아무런 문제가없는 반면, 적절한 선언을 찾을 수없는 그 소리 왜

clang -ferror-limit=1 -g -Wall -fPIC -o ors.o -c ors.cpp 
ors.cpp:189:21: error: invalid operands to binary expression ('std::istream' (aka 'basic_istream<char>') and 'const char [2]') 
    file >> "[ " >> x >> "," >> y >> " ]"; 
    ~~~~^~~~~ 
/usr/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.6.3/../../../../include/c++/4.6.3/istream:121:7: note: candidate function 
    not viable: no known conversion from 'const char [2]' to '__istream_type &(*)(__istream_type &)' for 1st argument; 
    operator>>(__istream_type& (*__pf)(__istream_type&)) 
[[ lots of other possible candidates from the stl ]] 

. 이 문제를 어떻게 해결할 수 있습니까?

+0

각 파일에'# include'd를 어떻게 보여줄 수 있습니까? 아마도 g ++ 구현은 clang이 아닌 다른 헤더를 포함하고있을 것입니다. –

+3

컴파일러와 다른 동작에 대해 질문 할 때 항상 작은 컴파일 가능한 코드 샘플을 게시하는 것이 좋습니다. –

+0

나는 최소한의 컴파일 가능한 예제를 얻으려고 노력할 것이다. – hildensia

답변

2

'파일 ""[ "'을 (를) 사용하는 곳에 util.h를 포함 하시겠습니까?

더 이상의 세부 정보가 없으면 어떤 문제에 처해 있는지 알기가 어렵습니다. 나에게 그 소리는 다음과 같은 완벽한 프로그램을 잘 컴파일 : 표준을 재정의 자신의 오버로드 된 연산자를 제공하기 때문에 당신이 다른 방법으로이 일을 고려해야하지만

#include <iostream> 
#include <sstream> 

std::istream& operator>>(std::istream& is, const char *str); 
std::istream& operator>>(std::istream& is, char *str); 

int main() { 
    std::stringstream file("tmp"); 
    char *x, *y; 

    // opening and allocating space for strings comes here 

    file >> "[ " >> x >> "," >> y >> " ]"; 
} 


// util.cpp 
// 
// lots of stuff again 

void parse(std::istream& is, const char *str) {} 

//! a global operator to scan (parse) strings from a stream 
std::istream& operator>>(std::istream& is, const char *str){ 
    parse(is, str); return is; 
} 
//! the same global operator for non-const string 
std::istream& operator>>(std::istream& is, char *str){ 
    parse(is, (const char*)str); return is; 
} 

좋은 방법이 아닙니다. 조회 규칙은 비밀이며 악용 코드는 이해하고 유지하기가 어려울 것입니다.

+0

예,이 최소 예가 컴파일됩니다. 그래서 우리 파일에있는 다른 것들은 연산자 오버로딩을 마술처럼가립니다. 코드베이스가 실제로 오래되었으므로이 연산자 오버로드를 변경하면 많은 문제가 발생합니다. – hildensia

+0

@hildensia 문제를 재현하는 (작은) 프로그램을 제공 할 수 있다면 더 나은 대답을 드릴 수 있습니다. – bames53

+0

나는 예제를 얻으려고 노력할 것이다. 그러나 나는 지금 점령 당했기 때문에 시간이 걸릴 수도있다. – hildensia