gpt4 book ai didi

c++ - 模板矩阵标量乘法 C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:14:01 28 4
gpt4 key购买 nike

当我尝试编译以下代码时,出现错误提示

错误 1 ​​error LNK2019: 未解析的外部符号 "class Matrix __cdecl operator*(int const &,class Matrix &)"(??D@YA?AV?$Matrix@H$02$01@@ABHAAV0@@ Z) 函数中引用_main C:\Users\pc\Documents\Visual Studio 2013\Projects\Homework4\Homework4\main.obj Homework4

这个错误是由矩阵上的标量乘法引起的,我不知道如何修复它。谁能给我一个建议?

//Matrix.h
#pragma once
#include <iostream>
#include <vector>
using namespace std;

template<class T, int m, int n>
class Matrix
{
vector<vector<T>> elements;
int nrow;
int ncol;

public:
Matrix();
~Matrix();
void print();
vector<T> row(int);
vector<T> col(int);

friend Matrix<T, m, n> operator+<>(const Matrix<T, m, n>&, const Matrix<T, m, n>&);
friend Matrix<T, m, n> operator-<>(const Matrix<T, m, n>&, const Matrix<T, m, n>&);
friend Matrix<T, m, n> operator*(const T&, Matrix<T, m, n>&);

};

template<class T, int m, int n>
Matrix<T, m, n>::Matrix() : nrow(m), ncol(n)
{
for (int i = 0; i < nrow; i++){
vector<T> row(ncol, i);
elements.push_back(row);
}
}

template<class T,int m,int n>
vector<T> Matrix<T, m, n>::row(int i)
{
return elements[i];
}

template<class T, int m, int n>
vector<T> Matrix<T, m, n>::col(int j)
{
vector<T> col;
for (int i = 0; i < ncol; i++)
{
col.push_back(elements[i][j]);
}
return col;
}

template<class T, int m, int n>
Matrix<T, m, n>::~Matrix(){}

template<class T, int m, int n>
void Matrix<T, m, n>::print()
{
for (int i = 0; i < nrow; i++){
for (int j = 0; j < ncol; j++)
{
cout << elements[i][j] << " ";
}
cout << endl;
}
}

template<class T, int m, int n>
Matrix<T, m, n> operator+(const Matrix<T, m, n>& m1, const Matrix<T, m, n>& m2){
Matrix<T, m, n> m3;
int nrow = m1.nrow;
int ncol = m1.ncol;
for (int i = 0; i < nrow; ++i){
for (int j = 0; j < ncol; ++j){
m3.elements[i][j] = m1.elements[i][j] + m2.elements[i][j];
}
}

return m3;
}

template<class T, int m, int n>
Matrix<T, m, n> operator-(const Matrix<T, m, n>& m1, const Matrix<T, m, n>& m2){
Matrix<T, m, n> m3;
int nrow = m1.nrow;
int ncol = m1.ncol;
for (int i = 0; i < nrow; ++i){
for (int j = 0; j < ncol; ++j){
m3.elements[i][j] = m1.elements[i][j] - m2.elements[i][j];
}
}

return m3;
}


template<class T, int m, int n>
Matrix<T, m, n> operator*(const T& c, const Matrix<T, m, n>& m1){
int nrow = m1.nrow;
int ncol = m1.ncol;
Matrix<T, m, n> m2;
for (int i = 0; i < nrow; ++i){
for (int j = 0; j < ncol; ++j){
m2.elements[i][j] = c*m1.elements[i][j];
}
}
return m2;
}

//main.cpp
#include "Matrix.h"
using namespace std;

int main()
{
Matrix<int, 3, 2> a;
Matrix<int, 3, 2> b;
Matrix<int, 3, 2> c;
c = 2*b;

c.print();
}

我认为错误来自类主体中的友元函数(操作*),但我不知道如何修复它...

最佳答案

错误发生是因为一)operator*被声明为非模板(你应该使用 <> )b) 如果你使用 <>语法,您必须提供模板的声明。

修复(在 Matrix 类之前添加):

template<class T, int m, int n>
class Matrix;

template<class T, int m, int n>
Matrix<T, m, n> operator*(T&, const Matrix<T, m, n>&);

//class matrix definition here

添加<>operator* friend声明:

friend Matrix<T, m, n> operator*<>(T&, const Matrix<T, m, n>&);

关于c++ - 模板矩阵标量乘法 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36364888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com