如何声明派生类对象的迭代器?

发布于 2025-01-05 15:43:32 字数 1719 浏览 0 评论 0原文

我是否正确声明了派生类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2025-01-12 15:43:33

第 26 行:std::vector 是一个模板,您需要告诉它要专门做什么,以使其成为您可以继承的类。

第 26 行奖励:避免从 std 容器继承

第 27 行:dVector 是模板吗?如果不是,您就不能像它一样使用它!

第 37-38 行:第 27 行和第 28 行中的错误导致 r_iterc_iter 未声明,因此您在这些行上遇到错误。

也就是说,这可能会帮助您完成您想要做的事情:

template<class T>
class dVector : public std::vector<T> {
   typename dVector <dVector <T> >::iterator r_iter;
   typename dVector <T>::iterator 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 and c_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:

template<class T>
class dVector : public std::vector<T> {
   typename dVector <dVector <T> >::iterator r_iter;
   typename dVector <T>::iterator c_iter;

But the advice still remains, don't inherit from std containers.

圈圈圆圆圈圈 2025-01-12 15:43:33

我认为您在继承之前忘记将模板类型分配给 std::vector

class dVector:public std::vector {        // Not valid

class dVector:public std::vector<int> {   // Valid

I think you're forgetting to assign a template type to std::vector before inheriting from it.

class dVector:public std::vector {        // Not valid

class dVector:public std::vector<int> {   // Valid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文