0

First of all it's an exercise given to me so i can't change things and have to work with it. I have a 2d vector aka a matrix. My header file looks like this

#include <vector>
#include <iostream>

using namespace std;
class Matrix{
private:
vector<vector<double>> 2d;
public:
explicit Matrix(unsigned int sizeY=0,unsigned int sizeX=0,double value= 0.0);
~Matrix() = default;
Matrix(const Matrix &other);
Matrix(Matrix &&other) = default;
Matrix& operator=(const Matrix &other);
Matrix& operator=(Matrix &&other) = default;


//other + - operators

//INDEX
vector<double>& at(unsigned int i);
const vector<double>& at(unsigned int i)const;

const vector<double>& operator[] (double m) const;
vector<double>& operator[] (double m);
};

Matrix operator+(const Matrix& d1, const Matrix& d2);

Matrix operator-(const Matrix& d1, const Matrix& d2);


ostream& operator<<(ostream &o, const Matrix& v);
istream& operator>>(istream &i, Matrix& v);

So now I implemented everything except the << and >> operator. Now the question if i want to go through the 2d vec matrix is there another way to get the "depth" outside the Matrix class except writing a getter ? If the Matrix is N X M e.g. 4x4 i can get the 2nd 4 the "width" with something like 2d[0].size() but i cant figure out how I can get the "depth" otherwise then use a getter. Also i cant change 2d to public or use templates.

I tried for around 2-3 hours myself and couldnt find any solution and maybe its not possible under the given conditions.

wonkas42
  • 1
  • 2
  • 4
    `vector> 2d;` will not compile. – Evg Dec 04 '22 at 02:22
  • Have you tried compiling this exact code? It's curious that you're asking how to add to code that already won't compile. – Drew Dormann Dec 04 '22 at 02:30
  • Maybe you need to declare operator<< as friend, [reference](https://stackoverflow.com/questions/2828280/friend-functions-and-operator-overloading-what-is-the-proper-way-to-overlo ) – Kargath Dec 04 '22 at 09:53
  • If you have asked your teacher, he answered that there is no problem with all the interfaces inside the class, maybe you can try some [hack methods](https://stackoverflow.com/questions/37981617/using-the-address- of-a-public-member-variable-to-access-a-private-member) – Kargath Dec 04 '22 at 09:55
  • Why would you want to write `matrix_name.elements[0].size()` instead of `matrix_name.columns()`? – Bob__ Dec 04 '22 at 10:11
  • vector> 2d; should be vector> matrix; my bad ;) – wonkas42 Dec 04 '22 at 12:06
  • @Kargath if i declare it as a friend then i need to move it inside the class in header and cpp file and work as usual ? Is it common pratice ? – wonkas42 Dec 04 '22 at 12:14
  • You need to add the friend `keyword` for `operator<<` in the declaration of the class, and you don't need to add it for the definition in the `*.cpp` file. [This link](https://stackoverflow.com/a/2828320/13792395) mentions "Since the stream operators' left-hand argument is a stream, stream operators either have to be members of the stream class or free functions. " – Kargath Dec 04 '22 at 12:19
  • @Kargath ok maybe i dont get something but i used to keyword and let << outside my Matrix class and done nothing in the cpp diff but now i get the "friend used outside the class " error.The Line in the Headerfile looks like that: friend ostream& operator<<(ostream &o, const Matrix& v); – wonkas42 Dec 04 '22 at 12:29
  • I mean, you should add `friend` at the declaration of `operator<<` in `class`, like [this](https://github.com/KargathEx/Thinking-In-C-resource/blob/master/Resource/C12/IostreamOperatorOverloading.cpp#L23) – Kargath Dec 04 '22 at 12:40
  • @Kargath I think i got it now ;) so In the class friend ostream& operator<<(ostream &o, const Matrix& v); then outside the class (but in header ) i dont change anything and same for cpp file. Yeah Kargath thank you for your time mate – wonkas42 Dec 04 '22 at 12:42
  • Yes, you don't need to add friend keyword outside of class – Kargath Dec 04 '22 at 12:44

1 Answers1

0

2d[0].size() is giving you the length of the first vector stored in 2d. To get the length of 2d you can just call the same directly on 2d.

2d.size() = length of 2d 2d[0].size() = length of first vector stored in 2d

Venom4992
  • 34
  • 2