Consider this piece of code:
struct MY_VECTOR : public vector<vector<int>>
{
int n = this->size();
int m = (*this)[0].size(); //<== fixed this[0].size()
int at(int i, int j)
{
if (i < 0 || j < 0 || i >= n || j >= m)
return 0;
else
return (*this)[i][j];
}
};
I want to access elements but without throwing any exceptions. (I could have done operator[]
overloading but it doesn't matter for me now.) Firstly, I have never tried to inherit from STL containers so I'm not even sure If it's ok to do that. (I have read already that's generally a bad idea, but at least I want to give it a try). Secondly I'm not even sure if everything will work correctly, cause as I said already I have never tried to do such things. So I tried to create MY_VECTOR
object and call it's constructor.
struct MY_VECTOR : public vector<vector<int>>
{
int n = this->size();
int m = (*this)[0].size();
int at(int i, int j)
{
if (i < 0 || j < 0 || i >= n || j >= m)
return 0;
else
return (*this)[i][j];
}
};
int main()
{
int n;
cin >> n;
MY_VECTOR a(n, vector<int>(n));
}
And it doesn't compile and I don't understand why. Doesn't vector<vector<int>>
constructor must be inherited? Or there is a different problem, which I don't see?
Compiler error: "Error E0289: No instance of constructor "MY_VECTOR::MY_VECTOR" matches the argument list"