在访问该类的函数时,如何在我的类的 2D 矢量上使用点运算符?
Posted
技术标签:
【中文标题】在访问该类的函数时,如何在我的类的 2D 矢量上使用点运算符?【英文标题】:How do I use the dot operator on a 2D Vector of my class while accessing a function of that class? 【发布时间】:2020-04-04 03:53:25 【问题描述】:我很难解释我想要做什么——对不起,如果我把标题搞砸了:所以,我制作了一个 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>>>>'
我可以看到它试图在 STL 向量容器内找到fill2dVector
;但是,我不希望它这样做。另外,我无法使用原型中的函数,因为这是我班级的作业,但我可以修改它们。
#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);
【问题讨论】:
矩阵是std::vector
。 std::vector
s 没有名为 fill2dVector
的方法。仅仅因为这个std::vector
包含另一个std::vector
,其中包含有一个名为fill2dVector
的方法的对象,并不意味着第一个std::vector
也必须有一个名为fill2dVector
的方法来做同样的事情。 C++ 不能以这种方式工作。是的,你说“我不希望它那样做”,所以你需要弄清楚你想要做什么。
注意:data[10][10] = ;
不会分配存储空间。它将 0 分配给您尚未分配的存储空间。我建议阅读some good std::vector
documentation。
【参考方案1】:
而不是:
std::vector< std::vector <Matrix> > matrix;
写:
Matrix matrix;
您的矩阵实现也被破坏了,因为您在没有分配的情况下访问向量元素,而填充时不检查向量大小,因此您可以进入越界访问,为什么要通过引用传递这些整数?
【讨论】:
很多代码来自作业的第 1 部分,我必须使用一个数组,私有成员变量是 max_rows 和 max_cols,所以我需要通过引用传递这两个整数,以免在之后丢失它们接受用户选择的行和列;第 2 部分要求我使用矢量——这让我很困惑【参考方案2】:我不能 100% 确定我是否理解您的问题,所以我认为 我提供了一个关于访问我编写的 3d 矢量对象的代码示例 不久前,我的猜测是,这真的会帮助你解决你的问题 问题。只需移除一层即可访问 2d 矢量。
使用该示例代码,您应该能够解决您的问题。
还有一个 <random>
示例指的是您的另一个问题。
#include <iostream> // cout
#include <vector> // vector
#include <random> //rand, srand
#include <algorithm> //shuffle
#include <string> //using strings
#include <limits> //cin
using my_engine = std::random_device; //most random
class deck
std::string comment;
public:
void output(std::string); //txt output
void display_card_deck();//display all cards
void mix_cards(); //delete and remix deck new game
size_t getCards(size_t playnum, size_t color, size_t number);
private:
size_t _inp_check(); //input player number + check
std::vector < std::vector < std::vector < size_t > > > _card_deck; //assign 3d vector
void _shuffle_cards(size_t players); //mix and shuffle cards randomly
;
void deck::output(std::string comment)
std::cout << comment << " cards are used for " << _card_deck.size() << std::endl;
int main()
deck play;
//round 1
play.mix_cards(); //mix cards randomly and input player number 2-8
play.output ("Welcome Player ... and Player ... "); //text, number of players
std::cout << play.getCards(0,2,2) << "test output" << std::endl; //pick one card
//round 2
play.mix_cards();
std::cout << play.getCards(0,2,2) << std::endl; //pick one card
play.output ("Welcome Player ... and Player ... "); //text, number of players
play.display_card_deck();
return 0;
//members of deck class
size_t deck :: _inp_check()
size_t input;
while (!(std::cin >> input) || input < 2 || input > 8)
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Try again and enter a valid player number from 2 to 8: "<< "\n";
return input;
void deck :: mix_cards()
if (_card_deck.empty())
std::cout << "How many Players are playing today? (2-8):" << std::endl;
size_t players = _inp_check();
_shuffle_cards(players);
else
std::cout << "How many Players are playing today? (2-8):" << std::endl;
_card_deck.clear();
size_t players = _inp_check();
_shuffle_cards(players);
void deck :: _shuffle_cards(size_t players)
my_engine engn; //random_device
for(size_t playnum = 0; playnum < players; ++playnum)
std::vector < std::vector < size_t > > d1;
_card_deck.push_back( d1 );
for(size_t color = 0; color < 4; color++)
std::vector < size_t > d2;
_card_deck[playnum].push_back( d2 );
for(size_t k = 1; k < 11; k++)
_card_deck[playnum][color].push_back( k ); //1 2 3 4 5 6 7 8 9 10
shuffle(_card_deck[playnum][color].begin(), _card_deck[playnum][color].end(), engn);
void deck :: display_card_deck()
for (size_t i = 0; i < _card_deck.size(); i++)
for (size_t j = 0; j < _card_deck[i].size(); j++)
for (size_t k = 0; k < _card_deck[i][j].size(); k++)
std::cout << "_card_deck[" << i << "][" << j << "][" << k << "] = " << _card_deck[i][j][k] << std::endl;
size_t deck::getCards(size_t playnum, size_t color, size_t number)
return _card_deck[playnum][color][number];
【讨论】:
以上是关于在访问该类的函数时,如何在我的类的 2D 矢量上使用点运算符?的主要内容,如果未能解决你的问题,请参考以下文章