#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_ostream> &,矩阵&)”,从引用:在main.o中 LD _main:符号(S)没有发现建筑x86_64的 铛:错误:连接命令失败,退出代码1(使用-v看看调用)
复制建筑时,作为文本的全面和完整的输出,以及*编辑您的问题*展示它。并且请花一些时间[阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask),并学习如何创建一个[** Minimal **,Complete和Verifiable示例](http://stackoverflow.com/help/mcve)。 –
输出运算符签名应该是'ostream&operator <<(ostream&cout,const矩阵& m);'同时命名参数'cout'不是一个好主意。如何解决你应该阅读的链接器错误[https:// stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix)。 – user0042
复制错误。将其粘贴到您的问题中。在''''后面加上4个额外的空格,哦,并且你有两个'>>'实现;不知道这是否是一个转录错误或者什么? – Yakk