2015-04-01 70 views
0

菜鸟在这里,遗憾的错误填写标题3错误:错误:'Entry'未在此范围内声明。错误:模板参数1无效。错误:无效的类型在之前的声明“(”令牌

我试图编译从Bjarne的Stroustrup的“C++编程语言”这个代码段,但代码块不断抛出。我这个错误

的代码是关于检查范围的向量函数举行数组

#include <iostream> 
#include <vector> 
#include <array> 

using namespace std; 

int i = 1000; 

template<class T> class Vec : public vector<T> 
{ 
public: 
    Vec() : vector<T>() { } 

    T& operator[] (int i) {return vector<T>::at(i); } 
    const T& operator[] (int i) const {return vector<T>::at(i); } 
    //The at() operation is a vector subscript operation 
    //that throws an exception of type out_of_range 
    //if its argument is out of the vector's range. 
}; 

Vec<Entry> phone_book(1000); //this line is giving me trouble 

int main() 
{ 

    return 0; 
} 

它给我的这些错误:

  • 错误:'Entry'未在此范围内声明。
  • 错误:模板参数1无效。
  • 错误:??无效的类型在之前的声明“(”令牌

什么我不得不改变我如何申报VEC进入

+1

您尚未申报Entry。 Entry是您传递给Vec模板类的类型。它可能是一个结构体或类,但是它没有在代码中的任何地方定义过,所以你不能使用它。 – Prismatic 2015-04-01 15:00:03

+0

'Entry'似乎是本示例之前的几页。 – chris 2015-04-01 15:00:43

+1

也没有Vec 构造函数,它需要一个整数。 – Prismatic 2015-04-01 15:02:08

回答

0

老实说,我怀疑你直接复制这片从Bjarne的代码然而,问题正是错误说:Entry未声明 如果你只是想玩弄的例子,你可以尝试例如

Vec<int> 

,而不是何。 wever,还有一个小问题(这是一个让我相信你改变Bjarnes代码):您Vec类没有构造函数的int,从而

Vec<int> phone_book(1000); 

不能工作。只要提供这个构造函数:

Vec(int size) : vector<T>(size) {} 

它应该工作。

相关问题