64,646
社区成员
发帖
与我相关
我的任务
分享
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T>
friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
[/quote]
VS太不讲究了吧,这也能过。T重复声明了。
参见标准http://www.eel.is/c++draft/temp#local-6
顺便GCC是过不了的:
http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote]
operator * 的模板参数和matrix 有毛关系?
不是matrix的模板参数决定operator* 的模板参数,
因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数
因此
改一下就可以了
#include <iostream>
#include <vector>
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T1>
friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
int main()
{
Matrix<int> m;
Matrix<int> b;
b = 10*m;
}
[/quote]
这样写代表任何operator*<U>都是matrix<T>的友元,比如operator*<double>可以访问matrix<int>之类。虽然在我看来没什么区别,但从题主最开始的代码来看他似乎只希望operator*<T>能访问matrix<T>。[/quote]
你确定 ??
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T>
friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
[/quote]
VS太不讲究了吧,这也能过。T重复声明了。
参见标准http://www.eel.is/c++draft/temp#local-6
顺便GCC是过不了的:
http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote]
operator * 的模板参数和matrix 有毛关系?
不是matrix的模板参数决定operator* 的模板参数,
因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数
因此
改一下就可以了
#include <iostream>
#include <vector>
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T1>
friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
int main()
{
Matrix<int> m;
Matrix<int> b;
b = 10*m;
}
[/quote]
这样写代表任何operator*<U>都是matrix<T>的友元,比如operator*<double>可以访问matrix<int>之类。虽然在我看来没什么区别,但从题主最开始的代码来看他似乎只希望operator*<T>能访问matrix<T>。[/quote]是想一个友元对一个具体化,但是像你那样加约束会报错。template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T>
friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
[/quote]
VS太不讲究了吧,这也能过。T重复声明了。
参见标准http://www.eel.is/c++draft/temp#local-6
顺便GCC是过不了的:
http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote]
operator * 的模板参数和matrix 有毛关系?
不是matrix的模板参数决定operator* 的模板参数,
因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数
因此
改一下就可以了
#include <iostream>
#include <vector>
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T1>
friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
int main()
{
Matrix<int> m;
Matrix<int> b;
b = 10*m;
}
[/quote]
这样写代表任何operator*<U>都是matrix<T>的友元,比如operator*<double>可以访问matrix<int>之类。虽然在我看来没什么区别,但从题主最开始的代码来看他似乎只希望operator*<T>能访问matrix<T>。template <typename T>
friend MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);
template <typename T>
friend MyMatrix operator*(const T k, const MyMatrix & ma);
这两种写法在VS里面显示都是一样的,后面那种把光标移上去都会显示正确的备注,但是实际只有第一个能用。而且加上名称空间又不对了。[/quote]
看#19楼。
我不知道VS对这种情况是怎么处理的,语义是什么(比如operator*<double>是不是MyMatrix<int>的友元之类),你可能需要自己去查阅VS的文档。
总而言之这种写法是不规范的,至少不具备可移植性,不推荐。template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T>
friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
[/quote]
VS太不讲究了吧,这也能过。T重复声明了。
参见标准http://www.eel.is/c++draft/temp#local-6
顺便GCC是过不了的:
http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote]
operator * 的模板参数和matrix 有毛关系?
不是matrix的模板参数决定operator* 的模板参数,
因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数
因此
改一下就可以了
#include <iostream>
#include <vector>
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T1>
friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
int main()
{
Matrix<int> m;
Matrix<int> b;
b = 10*m;
}
template <typename T>
friend MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);
template <typename T>
friend MyMatrix operator*(const T k, const MyMatrix & ma);
这两种写法在VS里面显示都是一样的,后面那种把光标移上去都会显示正确的备注,但是实际只有第一个能用。而且加上名称空间又不对了。[quote=引用 7 楼 qq_34291505 的回复:] [quote=引用 4 楼 qq_34291505 的回复:] [quote=引用 3 楼 hdt 的回复:] 既然没学会走,就要跑,肯定要摔跤 我指出一点,你的operator* 是不是和 matrix类声明 在不同的文件里,把它改成同一个。
template<typename T>
class Matrix{
public:
Matrix()
{}
Matrix(Matrix const& rhs)
{
}
template <typename T>
friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );
};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
Matrix<T> result;
return result;
}
[/quote]
VS太不讲究了吧,这也能过。T重复声明了。
参见标准http://www.eel.is/c++draft/temp#local-6
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
template <typename T>
class MyMatrix
{
private:
int row, col;
T ** matrix;
public:
MyMatrix(); // default constructor
MyMatrix(int ro, int co); // constructor #1
MyMatrix(int ro, int co, T ** f); // constructor #2
~MyMatrix() { for (int n = 0; n < row; n++) delete[] matrix[n]; delete[] matrix; }; // destructor
MyMatrix(const MyMatrix & ma); // copy constructor
template <typename T>
friend MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);
};
template <typename T>
MyMatrix<T>::MyMatrix()
{
row = 0;
col = 0;
matrix = nullptr;
}
template <typename T>
MyMatrix<T>::MyMatrix(int ro, int co)
{
row = ro;
col = co;
matrix = new T*[row];
for (int n = 0; n < row; n++) matrix[n] = new T[col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
matrix[i][j] = 0;
}
template <typename T>
MyMatrix<T>::MyMatrix(int ro, int co, T ** f)
{
row = ro;
col = co;
matrix = new T*[row];
for (int n = 0; n < row; n++) matrix[n] = new T[col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
matrix[i][j] = f[i][j];
}
template <typename T>
MyMatrix<T>::MyMatrix(const MyMatrix & ma)
{
row = ma.row;
col = ma.col;
matrix = new T*[row];
for (int n = 0; n < row; n++) matrix[n] = new T[col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
matrix[i][j] = ma.matrix[i][j];
}
template <typename T>
MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma)
{
MyMatrix<T> result(ma.row, ma.col);
for (int i = 0; i < ma.row; i++)
for (int j = 0; j < ma.col; j++)
result.matrix[i][j] = k*ma.matrix[i][j];
return result;
}
解决一个问题的时候,先尽可能的把它简化。不要被旁枝末节所干扰。 你试过我的代码了吗?
说明,你提出了一个错误的问题,也就是说你的方向是错误。 因为你完全没有掌握c++的语法,所以你才会被那些旁枝末节所干扰。 大致看了一下你的贴图,猜测可能是因为那个命名空间的问题。试着把所有的命名空间删掉。 如果不行,把你的代码贴出来