所以我有一个richMatrix模板类,它对矩阵进行一些基本的操作。但是在尝试调用main.cpp中的函数时遇到错误。我一直得到:没有已知的从'richMatrix *'到'int **'的参数1的转换。
确切的错误消息是这样的:
C:/Users/annan/Desktop/prog/main.cpp: In function 'int main()':
C:/Users/annan/Desktop/prog/main.cpp:19:36: error: no matching function for call to 'richMatrix<int>::multiplyMatrix(richMatrix<int>*&, richMatrix<int>*&, int&)'
eh->multiplyMatrix(meme, lol, s);
In file included from C:/Users/annan/Desktop/prog/main.cpp:2:0:
C:/Users/annan/Desktop/prog/richMatrix.cpp:21:5: note: candidate: T** richMatrix<T>::multiplyMatrix(T**, T**, int) [with T = int]
T** richMatrix<T>::multiplyMatrix(T** mat1, T** mat2, int size)
C:/Users/annan/Desktop/prog/richMatrix.cpp:21:5: note: no known conversion for argument 1 from 'richMatrix<int>*' to 'int**'
mingw32-make.exe: *** [Debug/main.cpp.o] Error 1
prog.mk:105: recipe for target 'Debug/main.cpp.o' failed
====1 errors, 23 warnings====
关于我的main.cpp,我不确定如何从richMatrix类调用函数,所以我的主要可能没有多大意义。我还是新编码。据我所知,我的createNewMatrix函数工作正常,但我对乘法和除法函数的调用正在被淘汰。
据我所知,模板应该自动将其模板类型转换为已传递的变量类型,如int,char,string等。
如果你能告诉我我做错了什么,我会非常感激,如果可能的话,请给我一些指示,以便将来避免这种情况。
提前致谢。
main.cpp中
#include "richMatrix.h"
#include "richMatrix.cpp"
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
int main() {
int s = 5;
richMatrix<int> *meme;
richMatrix<int> *lol;
richMatrix<int> *eh;
meme->createNewMatrix(s);
lol->createNewMatrix(s);
eh->createNewMatrix(s);
eh->multiplyMatrix(meme, lol, s);
eh->divideMatrix(meme, lol, s);
};
richMatrix.h
#ifndef RICHMATRIX_H
#define RICHMATRIX_H
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
template <class T> class richMatrix {
public:
T** createNewMatrix(int size);
T** multiplyMatrix(T** mat1, T** mat2, int size);
T** divideMatrix(T** mat1,T** mat2, int size);
};
#endif
richMatrix.cpp
#include "richMatrix.h"
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
template <class T>
T** richMatrix<T>::createNewMatrix(int size) {
T** matrix;
int s;
s = size;
matrix = new T*[s];
for(int i = 0; i < s; i++) {
matrix[i] = new T[s];
}
return matrix;
}
template <class T>
T** richMatrix<T>::multiplyMatrix(T** mat1, T** mat2, int size) {
int s = size;
T** newMatrix;
newMatrix = new T*[s];
for(int a = 0; a < s; a++) {
newMatrix[a] = new T[s];
}
for(int i = 0; i < s; i++) {
for(int j = 0; j < s; j++) {
newMatrix[i][j] = mat1[i][j] * mat2[i][j];
}
}
return newMatrix;
}
template <class T>
T** richMatrix<T>::divideMatrix(T** mat1,T** mat2, int size) {
int s = size;
T** newMatrix;
newMatrix = new T*[s];
for(int a = 0; a < s; a++) {
newMatrix[a] = new T[s];
}
for(int i = 0; i < s; i++) {
for(int j = 0; j < s; j++) {
newMatrix[i][j] = mat1[i][j] / mat2[i][j];
}
}
return newMatrix;
}
我相信你的问题是你的函数的类型声明和/或你传递给函数的内容。
对于下面的multiplyMatrix函数,您的参数是T **类型,当您在main函数中创建它时,它是一个int **。
template <class T> class richMatrix {
public:
T** createNewMatrix(int size);
T** multiplyMatrix(T** mat1, T** mat2, int size);
T** divideMatrix(T** mat1,T** mat2, int size);
};
但是在main.cc中,你传递的函数是传递richMatrix *而不是int **
int main() {
int s = 5;
richMatrix<int> *meme;
richMatrix<int> *lol;
richMatrix<int> *eh;
meme->createNewMatrix(s);
lol->createNewMatrix(s);
eh->createNewMatrix(s);
eh->multiplyMatrix(meme, lol, s);//right here
eh->divideMatrix(meme, lol, s);
};
您应该将传入的对象更改为multiplyMatrix函数为T **或更改函数接受的参数以获取richMatrix *
当你使用richMatrix<int> *meme;
时,你的multiplyMatrix()
函数变为:
int** multiplyMatrix(int** mat1,int** mat2, int size);
你可以看到,mat1
和mat2
类型是richMatrix<int> *
而不是int **
你在函数声明中的期望。
和矩阵应该这样创建:
auto meme = new richMatrix<int>();
//or in C++14 this way:
auto meme = std::make_unique<richMatrix<int>>();
因为richMatrix<int> *meme;
只声明一个没有分配的指针,所以在编译成功后首先调用
meme->createNewMatrix(s);
会造成崩溃。