这里没有,很抱歉标题填写错误。
我正试图从Bjarne Stroustrup的"C++编程语言"中编译这段代码,但CodeBlocks一直给我带来这个错误。
该代码是关于对向量函数中的数组进行范围检查的。
#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入境?
老实说,我怀疑您是否直接从Bjarne复制了这段代码。然而,问题只是错误所说的:Entry
没有声明。如果你只是想玩这个例子,你可以试试
Vec<int>
相反。然而,还有另一个小问题(这让我相信你更改了Bjarnes代码):你的类Vec
没有接受int
的构造函数,因此
Vec<int> phone_book(1000);
无法工作。只需提供此构造函数:
Vec(int size) : vector<T>(size) {}
它应该起作用。