【问题标题】:"required from here" error declaring a List of Lists“从这里需要”错误声明列表列表
【发布时间】:2012-11-04 11:26:05
【问题描述】:

我正在尝试以这种方式声明列表列表:

List_vector<List_vector<int> > multilist;

但是 Eclipse 强调了上面的声明并给出了这个错误:

这里需要

部分 List_vector 实现:

template<class T>
class List_vector: public Linear_list<T, int> {
public:
  typedef typename Linear_list<T, int>::value_type value_type;
  typedef typename Linear_list<T, int>::position position;

  List_vector();
  List_vector(int);
  List_vector(const List_vector<T>&);
  ~List_vector();
private:
    void change_dimension_(T*&, int, int);
    value_type* elements_;
    int length_; // the length of the list
    int array_dimension_; // array's dimension
};

编译器输出:

g++ -O3 -Wall -c -fmessage-length=0 -o multilista.o "..\\multilista.cpp" 
In file included from ..\multilista.cpp:1:0:
..\list_vector.h: In instantiation of 'List_vector<T>::~List_vector() [with T = List_vector<int>]':
..\multilista.cpp:16:32: required from here
..\list_vector.h:78:5: warning: deleting object of polymorphic class type List_vector<int>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
..\list_vector.h: In member function 'List_vector<T>::value_type List_vector<T>::read(List_vector<T>::position) const [with T = int; List_vector<T>::value_type = int; List_vector<T>::position = int]':
..\list_vector.h:136:1: warning: control reaches end of non-void function [-Wreturn-type]
..\list_vector.h: In member function 'List_vector<T>::value_type List_vector<T>::read(List_vector<T>::position) const [with T = List_vector<int>; List_vector<T>::value_type = List_vector<int>; List_vector<T>::position = int]':
..\list_vector.h:136:1: warning: control reaches end of non-void function [-Wreturn-type]
g++ -O3 -Wall -c -fmessage-length=0 -o tester.o "..\\tester.cpp" 
g++ -o Lista.exe tester.o multilista.o 

【问题讨论】:

  • 给我们 Eclipse 给出的完整错误,这不足以知道什么是错的。是否包含 List_vector 头文件?
  • 这是完整的错误,是的,包含标题。
  • Eclipse 只打印一行?查看控制台选项卡,您将找到错误的完整跟踪
  • 只有警告,没有错误,对吧?他们很清楚:你使用类继承,但你的析构函数不是虚拟的,你有一个应该返回值的函数,但没有。
  • “从这里需要”不是错误。它告诉您有关为什么需要该实例化的附加信息。真正的错误(或警告)在后面的几行中。请更新您的问题以反映实际错误。

标签: c++ templates constructor declaration


【解决方案1】:
In file included from ..\multilista.cpp:1:0:
..\list_vector.h: In instantiation of 'List_vector<T>::~List_vector() [with T = List_vector<int>]':
..\multilista.cpp:16:32:   required from here
..\list_vector.h:78:5: warning: deleting object of polymorphic class type 'List_vector<int>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

如果您要从Linear_List 派生,您应该考虑将析构函数设为虚拟,就像它说的那样。这只是一个警告,只有你知道它是否真的需要(没有足够的代码粘贴来判断)。

..\list_vector.h: In member function 'List_vector<T>::value_type List_vector<T>::read(List_vector<T>::position) const [with T = int; List_vector<T>::value_type = int; List_vector<T>::position = int]':
..\list_vector.h:136:1: warning: control reaches end of non-void function [-Wreturn-type]

您尚未粘贴 List_vector::read 的代码,但它似乎做错了什么:函数的每条路径都应返回一个 List_vector::value_type(除非它引发异常),但您正在让控制权什么都不做就到达终点。

..\list_vector.h: In member function 'List_vector<T>::value_type List_vector<T>::read(List_vector<T>::position) const [with T = List_vector<int>; List_vector<T>::value_type = List_vector<int>; List_vector<T>::position = int]':
..\list_vector.h:136:1: warning: control reaches end of non-void function [-Wreturn-type]

【讨论】:

  • Linear_list 类没有 costructor/destructor。虽然第二次警告是可以的(在 if 中返回)。
  • if 中返回不是问题。但是您不能if 内部返回,因为如果采用else 路径会发生什么?
  • 使用不同的编译器(Dev C++ 自带的 MINGW32 解决了这个问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 2015-08-04
相关资源
最近更新 更多