簡體   English   中英

std :: vector的C ++模板包裝器類

[英]C++ Template Wrapper class for std::vector

我正在嘗試使用模板封裝C ++標准庫的矢量類,但是我一直收到錯誤消息

SceneVector.h: In member function ‘void scenegraph::SceneVector<V>::print()’:
SceneVector.h:40: error: expected ‘;’ before ‘it’
SceneVector.h:40: error: ‘it’ was not declared in this scope

我設法創建的代碼是

#include <map>
#include <vector>
#include <iostream>

namespace scenegraph
{
    template <class V> class SceneVector
    {
        typedef std::vector<V> Vector;
        Vector vector;

        public:
            SceneVector();
            void insert(const V value);
            void print();
    };

    template <class V> SceneVector<V>::SceneVector()
    {
        vector.clear();
    }

    template <class V> void SceneVector<V>::insert(const V value)
    {
        vector.push_back(value);
    }

        template <class V> void SceneVector<V>::print()
    {
        for(Vector::iterator it = vector.begin(); it != vector.end(); ++it)
        {
            std::cout << "[" << (*it) << "] " << std::endl;
        }
        std::cout << std::endl;
    }
}

有人可以在這里糾正我嗎? 我必須強調自己是C ++新手,因此答案可能非常瑣碎。

當訪問依賴於模板參數的類型時,必須在typename之前添加前綴,以使解析器清楚該類型是類型。

for(typename Vector::iterator it = vector.begin(); it != vector.end(); ++it)

Vector :: iterator可能是一個編譯時常數,而編譯器直到實例化時才知道。 這就是為什么您必須明確地告訴它。

如果你有平原

std::vector<V>::iterator

很明顯,它是從屬名稱,您需要一個typename來指示迭代器是一個類型。

使用typedef並不會真正改變它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM