c++中通过引用传递对象错误
Pass Object by Reference error in C++
我有一个MatrixType
头文件,它有以下定义:http://pastebin.com/DMzf1wGB
//Add Two Matrices and store result into another matrix
void Add(MatrixType otherMatrix, MatrixType& resultMatrix);
上述方法的实现如下:
void MatrixType::Add(MatrixType otherMatrix, MatrixType& resultMatrix)
{
cout << "Inside Add func!" << endl;
cout << "other matrix : " << endl;
otherMatrix.PrintMatrix();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
resultMatrix.values[i][j] = values[i][j] + otherMatrix.values[i][j];
}
cout << "n";
resultMatrix.PrintMatrix();
cout << "n";
}
}
PrintMatrix()
的定义:
void MatrixType::PrintMatrix()
{
//Pre: None
//Post: Matrix is printed row wise
for (int i = 0; i < numRows; i++) {
cout << "[ ";
for (int j = 0; j < numCols; j++) {
cout << values[i][j];
}
cout << "]";
cout << "n";
}
}
现在在我的 Main.cpp
我有MatrixType
数组这样:MatrixType matrixStore[10]
存储10个MatrixType对象。Main.cpp的完整代码在这里:http://pastebin.com/aj2eEGVS
int rows = matrixStore[index1].getRowSize();
int cols = matrixStore[index1].getColSize();
cout << "The two matrices can be added!" << endl;
cout << "Computing... " << endl;
//Create Result Matrix and a pointer variable to it
MatrixType pResultMatrix = MatrixType(rows, cols);
matrixStore[resultIndex] = pResultMatrix;
//Invoke Add function
matrixStore[index1].Add(matrixStore[index2], matrixStore[resultIndex]);
现在,在我的Add()函数中,我执行otherMatrix.PrintMatrix()
,它打印出调用Add()
函数的矩阵的值。由于这=>要么我没有引用矩阵对象调用方法或矩阵对象作为参数传递!
同样,每当我在添加值后(在下一轮Switch Case中)执行PrintMatrix()
时,我总是得到垃圾值。
对此有什么解决方案/解释吗?
TIA
主要问题不在这部分代码中,而是在矩阵创建/初始化期间。你在数组中存储对象可能是好的(尤其是我喜欢它,因为它是在堆栈上创建的,这要快得多,只是要注意不要创建太长的数组,以避免堆栈溢出),但你必须考虑你的代码的整体。
例如,在Main.cpp行38中,您将matrix
对象的内容复制到数组中,但之后您修改matrix
对象,该对象与数组中的对象不同 !这意味着数组中矩阵对象的内容具有一些随机值。你应该直接修改数组中的对象,拥有那个临时对象没有任何意义。
matrixStore[index] = MatrixType(rows, cols);
for (int i = 0; i < rows; i++)
{
std::cout << "Row " << i << " : ";
for (int j = 0; j < cols; j++)
{
std::cin >> value;
matrixStore[index].StoreItem(value, i, j);
}
}
我认为在这个改变之后,你在这里复制的部分应该可以工作,因为你直接在数组中工作。
一些小建议:
传递MatrixType对象总是作为引用,如果可能的话。例如,在
Add
函数中,òtherMatrix也可以是const ref
,您的代码将非常高效,因为不会涉及对象副本。例如:void MatrixType::Add(const MatrixType& otherMatrix, MatrixType& resultMatrix);
这里otherMatrix是一个输入参数,resultMatrix是(可以是)一个输出参数。
在c++中有真正的
bool
类型。避免使用bool isAddComp ... if (isAddComp != 0)
代码,只使用if (isAddComp)
,这是c++的方式。我会开始使用
std::vector
而不是普通数组,更灵活,非常有用的学习如何使用它。我个人不会用
ùsing namespace
代替std
,它更短,更好地阅读你的代码(但也许这只是我的代码风格)。
看看这里的代码:http://pastebin.com/aj2eEGVS这似乎是您在所有路径中创建局部矩阵的唯一原因,例如:
MatrixType matrix = MatrixType(rows, cols);
是给成员numRows和numCols赋值。我看到你有一个注释掉的SetSize方法。我假设你不想使用它,因为你认为行和列在创建后不应该改变。
在这种情况下,你的matrixStore应该被创建为指针:
MatrixType* matrixStore[10];
现在不做:
MatrixType matrix = MatrixType(rows, cols);
matrixStore[index] = matrix;
你这样做:
matrixStore[index] = new MatrixType(rows, cols);
使用:
matrixStore[index]->StoreItem(value, i, j);
或者你想用它做什么
最后,您只需调用:
delete matrixStore[index];
对于所有你使用"new"的矩阵。最好的方法是一开始就将它们赋值给nullptr。
MatrixType* matrixStore[10];
for ( unsigned int i = 0; i < 10; ++i )
{
matrixStore[i] = nullptr;
}
最后:
for ( unsigned int i = 0; i < 10; ++i )
{
if (nullptr != matrixStore[i])
{
delete matrixStore[i];
}
}
简而言之,matrixStore
存储对象值,而不是指针(如您所期望的)。
//Create Result Matrix and a pointer variable to it
MatrixType pResultMatrix = MatrixType(rows, cols);
matrixStore[resultIndex] = pResultMatrix;
似乎你希望存储指针变量,但它不是真的。MatrixType
的值存储在matrixStore
中。
这样做的结果是不正确的matrixStore
填充(来自您的链接的代码):
MatrixType matrix = MatrixType(rows, cols);
matrixStore[index] = matrix;
cout << "Address of matrixStore[index] : " << &matrixStore[index] << endl;
cout << "Address of new matrix is : " << &matrix << endl;
int value;
for (int i = 0; i < rows; i++) {
cout << "Row " << i << " : ";
for (int j = 0; j < cols; j++) {
cin >> value;
matrix.StoreItem(value, i, j);
}
}
所有对matrix
的修改都将丢失,因为matrixStore
包含matrix
对象的旧副本。
解决方案:
matrixStore
保持指针的正确声明是:
MatrixType* matrixStore[10]
如果你想操作MatrixType
值对象,你需要将对象复制到matrixStore
中,每次修改它们
查看http://pastebin.com/aj2eEGVS
中的代码在我看来,你没有在matrixStore中初始化矩阵。你在填充局部变量矩阵。
MatrixType matrix = MatrixType(rows, cols); // LOCAL VARIABLE
**matrixStore[index] = matrix;** // you are copying the uninitialized matrix
//……
int value;
for (int i = 0; i < rows; i++) {
cout << "Row " << i << " : ";
for (int j = 0; j < cols; j++) {
cin >> value;
matrix.StoreItem(value, i, j); // LOCAL VARIABLE
}
}
cout << endl;
//Print matrix so the use can see
matrix.PrintMatrix(); // PRINT LOCAL VARIABLE
- 对象错误地显示为引用?
- 调用带有指针对象错误的指针变量
- 为什么谷歌测试/模拟显示 std::unique_ptr 泄露的模拟对象错误?
- C 数组指向对象错误
- C++:对象错误的多重定义
- 子数组分配:使用聚合对象错误的预期"{...}"进行初始化
- C 字符串对象错误字符
- 声明 Windows API 结构 (DCB) 的对象 - 错误 C4430:缺少类型说明符 - 假定为 int
- 无法调用成员函数而不出现对象错误
- C++指针/对象错误
- C++排序矢量对象错误
- C++:在映射中存储类对象(错误:获取类型为"Node"的临时对象的地址)
- 使用三元运算符初始化类对象错误
- 帧缓冲区对象错误
- 实例化新对象 - 错误
- C++打印出对象错误
- 如何更正类型对象错误
- 简单指针对象错误C++
- 运算符重载添加两个对象错误
- c++列表对象错误