std::vector<T>::

Sep 12, 2013 at 6:56pm
Hello,

is there a way to write a template like this:

1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
class MyVector {

private:
    std::vector<T> vector;

public:
    template<typename T> std::vector<T>::size_type size() {
        return vector.size();
    }

};


The code above doesn't compile.
Sep 12, 2013 at 6:57pm
You don't need to write template again on line 8. The one on line 1 will encompass the entire class definition.
Sep 12, 2013 at 7:12pm
Okay, but that doesn't solve the problem:

Warning: 'std::vector<T>::size_type' : dependent name is not a type
Error error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Sep 12, 2013 at 7:17pm
1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
class MyVector {

private:
    std::vector<T> vector;

public:
    // add 'typename' to clarify that the dependent name is the name of a type 
    typename std::vector<T>::size_type size() const {
        return vector.size();
    }
};
Sep 12, 2013 at 7:24pm
Thanks!
Topic archived. No new replies allowed.