Inheriting Constructors

Hi guys, I'm in chapter 20 of the c++ programming language 4th edition. This is a struct that is supposed to Inherit std::vector's constructor.

template<class T>
struct Vector : std::vector<T> {
using vector<T>::vector; // inherit constructors
T& operator=[](size_type i) { check(i); return this–>elem(i); }
const T& operator=(size_type i) const { check(i); return this–>elem(i); }
void check(size_type i) { if (this–>size()<i) throw Bad_index(i); }
};
Vector<int> v { 1, 2, 3, 5, 8 }; // OK: use initializer-list constructor from std::vector

However, when I run this, it's giving lots of errors:

..\src\sample.cpp:8:7: error: expected nested-name-specifier before 'vector'
using vector<T>::vector; // inherit constructors
^
..\src\sample.cpp:9:16: error: declaration of 'operator=' as non-function
T& operator=[](size_type i) { check(i); return this->elem(i); }
^
..\src\sample.cpp:9:14: error: expected ';' at end of member declaration
T& operator=[](size_type i) { check(i); return this->elem(i); }
^
..\src\sample.cpp:9:26: error: expected ')' before 'i'
T& operator=[](size_type i) { check(i); return this->elem(i); }
^
..\src\sample.cpp:10:20: error: declaration of 'operator=' as non-function
const T& operator=(size_type i) const { check(i); return this->elem(i); }
^
..\src\sample.cpp:10:18: error: expected ';' at end of member declaration
const T& operator=(size_type i) const { check(i); return this->elem(i); }
^
..\src\sample.cpp:10:30: error: expected ')' before 'i'
const T& operator=(size_type i) const { check(i); return this->elem(i); }
^
..\src\sample.cpp:11:12: error: 'size_type' has not been declared
void check(size_type i) { if (this->size()<i) throw Bad_index(i); }
^
..\src\sample.cpp: In member function 'void Vector<T>::check(int)':
..\src\sample.cpp:11:64: error: there are no arguments to 'Bad_index' that depend on a template parameter, so a declaration of 'Bad_index' must be available [-fpermissive]
void check(size_type i) { if (this->size()<i) throw Bad_index(i); }
^
..\src\sample.cpp:11:64: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

I am new in Inheriting classes, and I can't believe this has many errors. Is there something wrong with the book?

Last edited on
1
2
3
..\src\sample.cpp:8:7: error: expected nested-name-specifier before 'vector'
using vector<T>::vector; // inherit constructors
^
using std::vector<T>::vector;
1
2
3
..\src\sample.cpp:9:16: error: declaration of 'operator=' as non-function
T& operator=[](size_type i) { check(i); return this->elem(i); }
^
T& operator= [] (typename std::vector<T>::size_type i) { check(i); return this->elem(i); }
Change other size_types correspondingly
1
2
3
4
..\src\sample.cpp: In member function 'void Vector<T>::check(int)':
..\src\sample.cpp:11:64: error: there are no arguments to 'Bad_index' that depend on a template parameter, so a declaration of 'Bad_index' must be available [-fpermissive]
void check(size_type i) { if (this->size()<i) throw Bad_index(i); }
^
Basicly compiler does not know what Bad_index is. Make sure that it is declared before use
Ok, do you have the complete code? My version of the book only have the code I posted above. Thanks
Check other sections of the book. If you won't find anything, create one.
This code should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <vector>
#include <exception>

struct Bad_index : std::exception
{
    Bad_index(std::size_t index_) : index(index_) {}
    std::size_t index;
};

template<class T>
struct Vector : std::vector<T> {
    using std::vector<T>::vector; // inherit constructors
    T& operator=(typename std::vector<T>::size_type i)
    {
        check(i);
        return this->elem(i);
    }
    const T& operator=(typename std::vector<T>::size_type i) const
    {
        check(i);
        return this->elem(i);
    }
    void check(typename std::vector<T>::size_type  i)
    {
        if (this->size()<i) throw Bad_index(i);
    }
};

int main()
{
    Vector<int> v { 1, 2, 3, 5, 8 };
}


EDIT: after actually reading your code, I had feeling that you should have operator[] instead of operator=
Last edited on
Thanks a lot!
Topic archived. No new replies allowed.