【发布时间】:2013-04-10 20:47:49
【问题描述】:
下面有一个函数可以搜索my_type 的向量。目前,它有一个编译警告:control reaches end of non-void function [-Wreturn-type]。看来,只要我使用引用作为返回类型而不是指针,就不可能返回类似 null 的值?
struct my_type{
type_a a;
type_b b;
}
type_b& value(type_a& t){
typename std::vector<my_type>::iterator it;
for(it = v.begin(); it != v.end(); ++it){
if ((*it).a == t) return (*it).b;
}
}
【问题讨论】:
-
引用永远不能为空。
-
返回对容器中元素(的成员)的引用并不是一个好主意。
-
当您的
for结束时添加return something_meaningfull。
标签: c++ pointers reference warnings void