矩阵运算C++运算符重载程序错误
Matrix operations C++ operator overloading program error
类别:
#include <string>
using namespace std;
class Matrix{
public:
float **grid;
Matrix(int r, int c);
friend istream& operator >>(istream& cin, Matrix & m );
void setMatrix();
void printMatrix();
friend ostream& operator <<(ostream& cout, Matrix & m);
private:
int row;
int column;
};
istream& operator >>(istream& cin, Matrix & m );
ostream& operator <<(ostream& cout, Matrix & m);
矩阵cpp
#include "Matrix.h"
#include <iostream>
#include <string>
using namespace std;
//First constructor
Matrix::Matrix(int r, int c){
row=r; // Row size
column=c; // Column Size
if(row>0 && column>0)
{
grid = new float*[row]; // Creating 2d dynamic array
for(int i=0; i<row;i++)
grid[row] = new float [column];
//Setting all elements to 0
for(int i=0; i<row; i++)
{
for(int j =0; j<column; j++)
{
grid[i][j]=0;
}
}
}
else{
cout<<"Invalid number of rows or columns!"<<endl;
}
}
//Setting values to the matrix
void Matrix::setMatrix()
{
for(int i=0; i<row; i++)
{
for(int j=0;j<column;j++)
{
cin>>grid[i][j];
}
}
}
//Print matrix
void Matrix::printMatrix()
{
for(int i=0;i<row;i++)
{
for(int j=0; j<column; j++)
{
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
}
//Istream function
istream& operator >>(istream& cin, Matrix & m)
{
m.setMatrix();
return cin;
}
//ostream function
ostream& operator>>(ostream& cout, Matrix & m)
{
m.printMatrix();
return cout;
}
主要功能
#include <iostream>
#include "Matrix.h"
using namespace std;
int main()
{
Matrix m = Matrix(3,2);
cout << m;
return 0;
}
我正在尝试编写一个程序来执行不同的矩阵运算。这段代码应该主要创建一个尺寸为3*2的矩阵,构造函数将其所有值初始化为0,然后打印矩阵。在编译这个时,我得到了一个"链接器命令失败,退出1"错误,我真的不知道如何修复。我怎么能把它修好呢?
错误:
体系结构x86_64的未定义符号:"运算符<<(std::__1::basic_stream>&,矩阵&)",引用自:_main in mainld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
您有打字错误,请更改其中一个操作员>>
ostream& operator>>(ostream& cout, Matrix & m)
{
m.printMatrix();
return cout;
}
至
ostream& operator<<(ostream& cout, Matrix & m) {
m.printMatrix();
return cout;
}
还有
grid = new float*[row]; // Creating 2d dynamic array
for (int i = 0; i<row; i++)
grid[row] = new float[column];
应该是
grid = new float*[row]; // Creating 2d dynamic array
for (int i = 0; i<row; i++)
grid[i] = new float[column];
相关文章:
- 重载操作程序时出错>>用于类中的字符串 memebr
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 在Qt小部件应用程序中,在重载的"paintEvent"中的"setValue"
- 重载 <= 运算符,但程序以退出代码 -1073741571 (0xC00000FD) 结尾
- 运算符重载 new 时 void * nullptr 使程序崩溃
- 我的移动分配操作程序重载的分段错误
- 基于函数重载的程序
- 重载" >> " 程序崩溃
- CGI程序的自定义输出缓冲区,运算符重载
- 矩阵运算C++运算符重载程序错误
- 如何删除模板类的输出操作程序重载的错误
- 赋值重载:为什么使用相同的对象会导致程序中出现问题
- 提升程序选项:自定义验证器是否需要重载运算符>>?
- 运算符重载程序中的混淆
- 在 c++ 计算器程序中重载 '+' 运算符时无法获得正确的输出
- 在 XML 解析程序上没有可行的重载
- C++ 程序上没有可行的重载'='
- 重载运算符==具有采用模板化迭代程序的自由函数
- 重载内存映射文件加载程序(C++)
- “我的程序”使用重载运算符保持seg错误