У меня проблемы с объяснением того, что я пытаюсь сделать - извините, если испортил название: итак, я сделал 2D Вектор. 2D Вектор - это объект моего класса, который называется Matrix. Я попытался сделать matrix.fill2dVector(numRows,numCols)
, и я получил бы эти ошибки:
class "std::vector<std::vector<Matrix, std::allocator<Matrix>>, std::allocator<std::vector<Matrix, std::allocator<Matrix>>>>" has no member "fill2dVector"
'fill2dVector': is not a member of 'std::vector<std::vector<Matrix,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>'
Я вижу, что он пытается найти fill2dVector
внутри векторный контейнер STL; но я не хочу этого делать. Кроме того, я застрял с использованием функций в прототипах, так как это задание для моего класса, но мне разрешено изменять их.
#include <iostream>
#include <vector>
class Matrix
{
public:
Matrix();
double& operator()(const int rn, const int cn);
void operator()();
void fill2dVector(int &numRows, int &numCols);
void display2dVector(int &numRows, int &numCols) const;
private:
int numRows = 10, numCols = 10;
std::vector<std::vector <double>> data;
};
Matrix::Matrix()
{
data[10][10] = {};
}
double& Matrix::operator()(const int rn, const int cn)
{
return data[rn][cn];
}
void Matrix::operator()()
{
for (int r = 0; r < numRows; ++r)
{
for (int c = 0; c < numCols; ++c)
{
data[r][c] = 0;
}
}
}
void Matrix::display2dVector(int &numRows, int &numCols) const
{
for (int r = 0; r < numRows; ++r)
{
for (int c = 0; c < numCols; ++c)
{
std::cout << " " << data[r][c] << " ";
}
std::cout << std::endl;
}
}
void Matrix::fill2dVector(int &numRows, int &numCols)
{
for (int r = 0; r < numRows; ++r)
{
std::cout << "Enter " << numCols << " values for row #" << r << std::endl;
for (int c = 0; c < numCols; ++c)
{
std::cin >> data[r][c];
}
std::cout << std::endl;
}
}
int main()
{
std::cout << "Enter the size of the matrix:" << std::endl;
std::cout << " How many rows? ";
int numRows;
std::cin >> numRows;
std::cout << "How many columns? ";
int numCols;
std::cin >> numCols;
std::cout << std::endl;
std::cout << "*** numRows = " << numRows << ", " << "numCols = " << numCols << std::endl;
std::vector< std::vector <Matrix> > matrix;
std::cout << "Contents of the " << numRows << " x " << numCols << " vector:" << std::endl;
matrix.fill2dVector(numRows,numCols);
}