函数运算符上的未定义引用>>
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
在我的eventtohistorico .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
是一个局部变量,你不应该返回一个引用(当istream
作为参数传递时,你会复制它)。当it
是一个引用时,你也可以返回一个对它的引用:
istream& operator>> (istream& is, EventoHistorico& EH)
return is; // OK ! ^^--- pass by reference
}
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 如何引用基类的派生类?