试图重载运算符并使用类模板时发生C++错误
C++ Error with trying to overload operators and use class template
我的代码没有编译,我真的很困惑。当我运行代码时,我在头文件中得到一个错误,上面写着"'T'阴影模板参数的声明。我的老师在课堂上告诉我们,在类定义中的运算符声明之前,先放模板前缀模板。我不知道她是否错了,但我尝试删除了朋友函数声明之上的模板,当我尝试编译代码时,我遇到了一些链接器Id问题的错误。其中一个是指rix::矩阵(int)。老实说,我仍然认为我的老师所说的将模板前缀放在运算符声明之上是正确的,因为运算符的参数中有类类型,但有人能帮我吗!
在我的头文件中,我有:
#ifndef __testing_more_stuff__vector__
#define __testing_more_stuff__vector__
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
class Matrix{
public:
Matrix();
Matrix(T diagonal);
//template <class T>
friend ostream& operator <<(ostream& outs, const Matrix<T> &obj);
//template <class T>
friend istream& operator >>(istream& in, Matrix<T> &obj);
//template <class T>
friend Matrix<T> operator *(Matrix<T> A, Matrix<T> B);
private:
const static int n=3;
T a[n][n];
};//class declaration
#endif
在我的实现文件中,我有:
#include "vector.h"
template <class T>
Matrix<T>::Matrix(){
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
a[i][j]=0;
}
template <class T>
Matrix<T>::Matrix(T diagonal){
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (i==j)
a[i][j]=diagonal;
else
a[i][j]=0;
}
template <class T>
ostream& operator <<(ostream& outs, const Matrix<T> & obj)
{
for (int i = 0; i < obj.n; i++){
for (int j = 0; j < obj.n; j++)
outs << " "<< obj.a[i][j];
outs<<endl;
}
outs<<endl;
return outs;
}
template <class T>
istream& operator >>(istream& in, Matrix<T> & obj)
{
for (int i = 0; i < obj.n; i++){
for (int j = 0; j < obj.n; j++){
in >> obj.a[i][j];
}
}
return in;
}
template<class T>
Matrix<T> operator *(Matrix<T> A, Matrix<T> B){
Matrix<T> product;
for (int i=0;i<A.n; i++)
for (int j=0;j<A.n; j++) {
T sum=0;
for (int k=0; k<A.n; k++)
sum = sum+A.a[i][k]*B.a[k][j];
product.a[i][j]=sum;
}
return product;
}
在我的主文件中,我有:
#include "vector.h"
int main(){
Matrix<int> A;
Matrix<int> B(2);
cout << A;
cout <<B;
cout <<"stop here";
ifstream fin;
ofstream fout;
fout.open("output.txt");
if (fout.fail()){
cout <<"error openning output file";
exit(1);
}
fin.open("input.txt");
if (fin.fail()){
cout <<"error openning input file";
exit(1);
}
//input matrix C
Matrix<int> C;
cin >>C;
//fin >>C;
cout <<"C = "<<endl<<C<<endl;
cout <<"B = "<<endl<<B<<endl;
cout <<"C*B = "<<endl<<C*B<<endl;
//fout <<C;
//cout <<C.det();
return 0;
}
所以,您只需要按照编译器所说的去做。没有什么有趣的,我只是在这里放了固定来源的链接:http://rextester.com/NSAPP25204
您的问题似乎是将模板成员函数的实现放在一个单独的实现文件(可能是.cpp)中。将它们放在头文件vector.h中,因为每个想要使用模板类的实现文件也需要查看成员函数的实现。请注意,编译器无法访问早期编译产生的对象文件。
您已经在一个单独的(.cpp)文件中分离出Template类的实现。当编译器试图编译Matrix<int>
类时,它会通过vector.h
文件,但由于该文件不包含类的完整定义(即vector.cpp
中的代码),int
类型的Matrix类的模板类实现不完整,构造函数和重载运算符
你可以用两种不同的方法来解决这个问题:
1) 将所有内容从vector.cpp
移动到vector.h
,以便使用模板编译整个代码。或
2) 如果您仍然想将其放在单独的文件中,那么也可以将vector.cpp
文件包含在主文件中。
第一种方法是更好的方法,因为类的整个代码都在同一个文件中。
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'