C++ 无效数组<T、N> 下标
C++ invalid array<T, N> subscript
我在C++中制作了一个基本的类/结构集合,其中包含我没有包含的类Book
,以及其他一些类,以及struct
FixedSizeBookCollection
。但是,使用 FSCB
时,它无法按计划工作:
书籍.h
template<size_t Size> struct FixedSizeBookCollection : private std::array<const Book*, Size>{
FixedSizeBookCollection(const char* Name) : name_(Name){}
void operator+=(const Book& Book)try{
if((*this).size() > Size){
std::ostringstream errorMessage;
errorMessage << "The FixedSizeBookCollection " << name_ << "'s size capacity has been overfilled" << std::endl;
throw std::exception(errorMessage.str().c_str());
}
/* Line 97 - */ (*this).at(currentPos++) = &Book;
}catch(const std::exception& e){
std::ostringstream errorMessage;
errorMessage << e.what() << " - on line (approx.) " << (__LINE__ -3);
throw std::exception(errorMessage.str().c_str());
}
private:
const char* name_;
int currentPos;
};
而且主要是.cpp
FixedSizeBookCollection<5> Collection("My Fixed Size Collection");
Collection += MyBook;
但是我得到错误:
Error: invalid array <T, N> subscript on line (approx.) 97
问题出在哪里?
您的currentPos
未初始化。它可以具有任何值。将此变量初始化添加到 c-tor 中 0。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 如何在C++程序中使用下标数字?
- 一组值的零开销下标运算符
- 运行时错误:矢量下标超出范围:正在检查空集
- 如何重载下标运算符 [] 以引用 2d STL 数组?
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 矢量无效下标.SFML
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 有什么理由不扩展 std::set 以添加下标运算符吗?
- 在继承的模板类中使用下标 [] 运算符
- 对矢量来说是全新的.矢量下标超出范围
- 运算符重载多个任务的下标和赋值运算符
- 为什么函数的任何索引处的下标运算符在C++中总是返回1
- c++下标运算符到下级类向量
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 数组下标运算符 ([ ]) 对数组有什么作用?