函数运算符的未定义引用>>
Undefined reference on function operator>>
我正在定义一个抽象数据类型,当我开始定义运算符时,出现了这个错误,不明白为什么其他类似的项目我的运算符可以工作,而不是这个。
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
在我的 EventoHistorico.cpp 我有这个功能的实现:
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
改变这个...
istream& operator>> (istream is, EventoHistorico& EH){
return is; // not OK ! ^^--- pass by value
}
如果 is
是局部变量,则不应 return 引用(当 istream
作为参数传递时,您会复制它)。当 it
是引用时,您还可以 return 引用它:
istream& operator>> (istream& is, EventoHistorico& EH)
return is; // OK ! ^^--- pass by reference
}
我正在定义一个抽象数据类型,当我开始定义运算符时,出现了这个错误,不明白为什么其他类似的项目我的运算符可以工作,而不是这个。
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
在我的 EventoHistorico.cpp 我有这个功能的实现:
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
改变这个...
istream& operator>> (istream is, EventoHistorico& EH){
return is; // not OK ! ^^--- pass by value
}
如果 is
是局部变量,则不应 return 引用(当 istream
作为参数传递时,您会复制它)。当 it
是引用时,您还可以 return 引用它:
istream& operator>> (istream& is, EventoHistorico& EH)
return is; // OK ! ^^--- pass by reference
}