Noob here,
I'm trying to compile this segment of code from Bjarne Stroustrup's 'The C++ Programming Language' but CodeBlocks keeps throwing me this error.
The code is about range checking an array held in a vector function.
Here is the code:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
int i = 1000;
template<class T> class Vec : public vector<T>
{
public:
Vec() : vector<T>() { }
T& operator[] (int i) {return at(i); }
const T& operator[] (int i) const {return at(i); }
//The at() operation is a vector subscript operation
//that throws an exception of type out_of_range
//if its argument is out of the vector's range.
};
Vec<Entry> phone_book(1000);
int main()
{
return 0;
}
The errors returned are:
Can someone explain this to me?
Also, how would I implement this if I were to not use 'using namespace std;'
Replace at
with vector<T>::at
or this->at
.
Rules for how functions are looked up in templates are tighter now than when C++ was being originally designed.
Now, methods in dependent bases are only looked up if you this->
, otherwise it is assumed to be a global function (or a non-dependent base/class local/etc).
This can help avoid nasty surprises in practice, where what you thought was a method call becomes a global one, or a global call becomes a method call. It also allows earlier checking of template method bodies.
In addition to Yakk's answer, another solution would be to add
using vector<T>::at;
to Vec
basically adding it to the list of looked up functions.
This way, at()
can be used as usual without prefixing it with the base class type or this->
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With