2016-10-29 17 views
1

抽象データ型を定義していますが、演算子を定義し始めたときにこのエラーが発生しました。私EventoHistorico.cppで関数演算子の定義されていない参照>>

EventoHistorico.h

#ifndef __EVENTO 
#define __EVENTO 

#include <iostream> 
#include <string> 
#include <vector> 

using namespace std; 

/** 
    * @file EventoHistorico.h 
    * @brief T.D.A. EventoHistorico 
    * 
    * The instance of @e c its Abstract data type @c EventoHistorico is an object 
    * that contains a date and a colection of events associated with that date 
    * 
    * Representation: 
    * "date" : <"event1","event2",...> 
    * 
    * Examples: 
    * "1924 : Greed, Sherlock Jr., The Sea Hawk ,The Last Laugh" 
    * "1945 : Merge sort developed by John von Neumann" 
    * 
    * 
    */ 

    string anio; 

    vector<string> eventos; 

    vector<string> GetEvents(); 


    friend istream& operator>> (istream& is, EventoHistorico& EH); 



}; 
#endif 

私は、この機能の実装を持っている:

istream& operator>> (istream is, EventoHistorico& EH) 
{ 
    // Obtenemos el primer año y la asignamos a la variable 
    // (utilizamos como separador #) 
    getline(is, EH.anio, '#'); 

    // Bandera para salir del bucle cuando se llegue al salto de linea 
    // se utiliza este sistema para no usar break 
    bool salir = false; 

    // Recorre 
    while (!salir && is) 
    { 
     string event; 

     getline(is, event, '#'); 

     EH.eventos.push_back(event); 

     //Si salto de linea 
     if(is.peek() == '\n') 
     { 
      //Obtenemos el siguiente caracter (salto) y salimos del bucle 
      is.get(); 
      salir=true; 
     } 
    } 
    //referencia istream 
    return is; 

} 

その後、私はこのエラーに

src/EventoHistorico.cpp: In function ‘std::istream& operator>>(std::istream, EventoHistorico&)’: 
src/EventoHistorico.cpp:19:10: warning: reference to local variable ‘is’ returned [-Wreturn-local-addr] 
istream& operator>> (istream is, EventoHistorico& EH) 
     ^
g++ -o bin/pruebacronologia obj/cronologia.o obj/pruebacronologia.o obj/EventoHistorico.o 
obj/cronologia.o: On function `operator>>(std::istream&, Cronologia&)': 
/home/antonio/Escritorio/TDA/cronologia/src/cronologia.cpp:32: reference to `operator>>(std::istream&, EventoHistorico&)' undefined 
collect2: error: ld returned 1 exit status 
make: *** [bin/pruebacronologia] Error 1 
+0

ヘッダーにクラスなどが定義されている必要がありますか?あなたはその部分を切った。 – Deduplicator

+0

[MCVE]はどこですか? –

+1

'__EVENTO'は予約済みの識別子です。ヘッダーに定義すると、未定義の動作が発生します。 – user2079303

答えて

2
を取得し、こののproyectイムをコンパイルするとき

これを変更...

istream& operator>> (istream is, EventoHistorico& EH){ 
    return is; // not OK ! ^^--- pass by value 
} 

isがローカル変数(パラメータとして渡されたときにistreamのコピーを作成する)である場合、参照を返さないでください。 itが参照の場合は、参照を返すこともできます。

istream& operator>> (istream& is, EventoHistorico& EH) 
    return is; // OK !  ^^--- pass by reference 
}  
+0

これは、ローカル変数への参照を返すことについての警告だけでなく、未定義の関数の問題も修正します。 "EventoHistorico.h"は値でストリームを受け取る関数を約束しましたが、値でストリームを受け取る異なるオーバーロードしか提供されませんでした。 –

+0

ありがとうございます – Rusillo

関連する問題