C++ 报错相关¶
Expression must have class type¶
T operator[](size_t idx) const noexcept {
assert(idx < data_.size()); // 此处报错
return data_[idx];
}
T& operator[](size_t idx) noexcept {
assert(idx < data_.size()); // 此处报错
return data_[idx];
}
private:
T* data_{ nullptr };
size_t rows_{0};
size_t cols_{0};
上述代码中,编译时出现错误 expression must have class type but it has type "T *const"
只需把 data_.size() 改成 data_->size() 可解决
参考链接(需要魔法):https://stackoverflow.com/questions/6547602/expression-must-have-class-type