如何声明派生类对象的迭代器?
我是否正确声明了派生类 r_iter 和 c_iter 的迭代器? 在派生类中,r_iter 是 2D 向量的迭代器,c_iter 是 2D 向量的迭代器。
我收到以下错误,如果有人告诉我哪里错了,我将不胜感激
add_round_key.cpp:26:34: error: expected class-name before ‘{’ token
add_round_key.cpp:27:2: error: ‘dVector’ is not a template
add_round_key.cpp:27:11: error: ‘dVector’ is not a template
add_round_key.cpp:27:28: error: invalid use of ‘::’
add_round_key.cpp: In member function ‘void dVector::RotWord()’:
add_round_key.cpp:37:2: error: ‘r_iter’ was not declared in this scope
add_round_key.cpp:37:17: error: ‘class dVector’ has no member named ‘begin’
add_round_key.cpp:38:2: error: ‘c_iter’ was not declared in this scope
line# 26 class dVector:public std::vector {
line# 27 dVector <dVector <int> >::iterator r_iter;
dVector <int>::iterator c_iter;
public:
void RotWord();
void SubWord();
};
void dVector::RotWord() {
int temp ;
line# 37 r_iter = this->begin();
#38 c_iter = (*r_iter).end();
*(c_iter) = *(c_iter+4);
*(c_iter+4) = *(c_iter+8);
}
void dVector::SubWord(){
//function definition
}
int main (int argc, char *argv[])
{
/*wordArray is a 4x4 word array stored in column-order form*/
dVector <dVector <int> > wordArray(4,dVector<int>(40,0));
dVector <dVector <int> >::iterator ckIter,i ,j, row_iter;
dVector <int>::iterator ii,jj, col_iter;
wordArray.RotWord();
wordArray.Subword();
}
Am I declaring the iterators of the derived class r_iter and c_iter properly ?
Inside the derived class, r_iter is a iterator of a 2D vector and c_iter is iterator into the 2 D vector.
I'm getting the following error and I'd really appreciate if somebody tells me where I'm wrong
add_round_key.cpp:26:34: error: expected class-name before ‘{’ token
add_round_key.cpp:27:2: error: ‘dVector’ is not a template
add_round_key.cpp:27:11: error: ‘dVector’ is not a template
add_round_key.cpp:27:28: error: invalid use of ‘::’
add_round_key.cpp: In member function ‘void dVector::RotWord()’:
add_round_key.cpp:37:2: error: ‘r_iter’ was not declared in this scope
add_round_key.cpp:37:17: error: ‘class dVector’ has no member named ‘begin’
add_round_key.cpp:38:2: error: ‘c_iter’ was not declared in this scope
line# 26 class dVector:public std::vector {
line# 27 dVector <dVector <int> >::iterator r_iter;
dVector <int>::iterator c_iter;
public:
void RotWord();
void SubWord();
};
void dVector::RotWord() {
int temp ;
line# 37 r_iter = this->begin();
#38 c_iter = (*r_iter).end();
*(c_iter) = *(c_iter+4);
*(c_iter+4) = *(c_iter+8);
}
void dVector::SubWord(){
//function definition
}
int main (int argc, char *argv[])
{
/*wordArray is a 4x4 word array stored in column-order form*/
dVector <dVector <int> > wordArray(4,dVector<int>(40,0));
dVector <dVector <int> >::iterator ckIter,i ,j, row_iter;
dVector <int>::iterator ii,jj, col_iter;
wordArray.RotWord();
wordArray.Subword();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第 26 行:
std::vector
是一个模板,您需要告诉它要专门做什么,以使其成为您可以继承的类。第 26 行奖励:避免从 std 容器继承
第 27 行:
dVector
是模板吗?如果不是,您就不能像它一样使用它!第 37-38 行:第 27 行和第 28 行中的错误导致
r_iter
和c_iter
未声明,因此您在这些行上遇到错误。也就是说,这可能会帮助您完成您想要做的事情:
但建议仍然存在, 不要从 std 容器继承。
Line 26:
std::vector
is a template you need to tell it what to specialize on to make it a class you can inherit.Line 26 Bonus: Avoid inheriting from std containers
Line 27: Is
dVector
a template? If it's not you can't use it as if it is!Lines 37-38: Errors in lines 27 and 28 cause
r_iter
andc_iter
not to be declared, thus the errors you're getting on these lines.That said, this might help you accomplish what you're trying to do:
But the advice still remains, don't inherit from std containers.
我认为您在继承之前忘记将模板类型分配给
std::vector
。I think you're forgetting to assign a template type to
std::vector
before inheriting from it.