2011-04-12 3 views
0

이 같은 몇 가지 다른 게시물을 알고 있지만 한 시간 이상이 오류가 있었고 그것을 알아낼 수 없습니다. 여기에 여기에 문제가C++ 오류 설명 할 수 없음 : 오류 : '&'토큰 앞에 예상 된 이니셜 라이저

istream& operator>>(istream& in, UndirectedGraph& g) 
{ 
    int numVerticies; 
    in >> numVerticies; 
    g = UndirectedGraph(numVerticies); 

    for(int i = 0; i < numVerticies; i++) 
    { 
     int temp; 
     in >> temp; 
     if(temp != i) 
     { 
      g.linkedAdjacencyList[i]->value = temp; 
     } 
    } 

    int edges; 
    in >> edges; 
    g.edges = edges; 
    for(int i = 0; i < edges; i++) 
    { 
     int first; 
     int second; 
     in >> first >> second; 
     addEdge(first, second); 
    } 
    return in; 
} 

ostream& operator<<(ostream& out, UndirectedGraph& g) 
{ 
    out << g.numVerticies << endl; 

    for(int i = 0; i < g.numVerticies; i++) 
    { 
     out << g.linkedAdjacencyList[i] << " "; 
    } 
    out << endl; 

    out << g.edges << endl; 

    for(int i = 0; i < g.numVerticies; i++) 
    { 
     out << linkedAdjacencyList[i]->value; 
     Node* whereto; 
     whereto = linkedAdjacencyList[i]->adj; 
     while(whereto->adj != NULL) 
     { 
      out << " " << whereto->value; 
      whereto->adj = whereto->adj->adj; 
     } 
    } 
    return out; 
} 

int main() 
{ 

    ifstream inFile; 
    inFile.open("hw8.in"); 

    UndirectedGraph graph; 
    inFile >> graph; 

...

을주고 코드입니다, 오류가 IStream을하고 ostream에의 과부하로, 라인 1, 28에 있습니다.

도움 주셔서 감사합니다. voidUndirectedGraph::istream& :

+0

라인 (1) 및 (28)는 무엇인가? –

+0

@Daniel istream과 ostream 선언 – jlehenbauer

답변

2

이 :

void UndirectedGraph::istream& operator>>(istream& in, UndirectedGraph g) 

이 이해가되지 않습니다! 당신은 아마도 원할 것입니다 :

당신은 함수 정의에서 아무것도 반환하지 않는 것처럼 보입니다.

+0

사실입니다. 나는 그것을 간과했습니다. 제안 된대로, 나는 ('istream & in, UndirectedGraph & g)'UndirectedGraph :: istream & operator >> (istream과 ostream 둘 다) 행을 바꿨지 만'& '토큰 앞에 예상되는 생성자, 소멸자 또는 형식 변환이 생겼다. 오류 – jlehenbauer

+0

@ Jacob : 질문에 대한 편집 내용이 대표적이라면 코드를 올바르게 수정하지 않은 것입니다. 다시 한번보세요! –

+0

중요한 경우 UndirectedGraph는 내가 포함하고있는 .h 파일에 있으며 .cpp 파일에서 오버로드됩니다. – jlehenbauer

1
void UndirectedGraph::istream& operator>>(istream& in, UndirectedGraph g) 

여기 이 개 반환 유형이 있습니다. 하나는 갈 필요가있다. 다른 방법에도 똑같이 적용됩니다.

또한 std::istream이 아니라고 가정합니다. UndirectedGraph::istream이 맞습니까? 이 코드가 작동하려면

그리고 마침내, 그렇지 않으면 당신이 그것을 변경할 수 없습니다, 인수 기준에 의해 g을 통과해야합니다.

우리 잎 어떤 :

std::istream& operator>>(std::istream& in, UndirectedGraph& g) 
+0

도 Charlesworth에게 말했습니다. 나는 isstream과 ostream & ostream에서 void를 제거했습니다. 이제 다른 오류가 발생합니까? '&'토큰 앞에 예상되는 생성자, 소멸자 또는 형식 변환 – jlehenbauer

+0

@JacobL 업데이트를 참조하십시오. –

+0

@Konrad : @Wakefield와 @Oli은 istream이나 ostream 앞에'std ::'또는'UndirectedGraph ::'가 없습니다. Oli의 게시물에 대한 의견을 참조하십시오. 입력 연산자를 사용하려고하면 새로운 오류가 발생합니다. – jlehenbauer

0

몇 가지 문제가있는 것 같습니다. 첫째, 삽입 및 추출 연산자 멤버 메서드 또는 그냥 도우미 함수 있습니까? 후자의 경우, 서명은 위의 그림과 같이, 당신은 UndirectedGraph 매개 변수 참조 인수해야한다, 이전

어느 경우
istream &UndirectedGraph::operator>>(istream& in, UndirectedGraph &g) 

를 들어

istream & operator>>(istream& in, UndirectedGraph &g) 

같은 것을 할 것이다. 그렇지 않으면, 당신은 그냥 값으로 매개 변수를 전달하는 것입니다, 아마도 당신이 원하는 것이 아닙니다.

0

@ don-wakefield가 말한 것을 되풀이하는 종류. 귀하의 운영자가 std :: istream & 및 std :: ostream & (및 UndirectedGraph :: istream 및 UndirectedGraph :: ostream이 아님)을 반환하기를 원합니다.

그래서 서명과 같습니다

std::istream& UndirectedGraph::operator>>(istream& in, UndirectedGraph g) 
std::ostream& UndirectedGraph::operator<<(ostream& out, UndirectedGraph& g) 
관련 문제