无法使用模板重载运算符
Unable to overload operator using templates
我正在尝试重载矢量 class 的 + 运算符。向量 class 能够使用模板指定数据类型。 + 运算符应该能够采用两个不同的向量并创建一个具有主导数据类型的新结果向量。目前我的代码如下所示:
template<typename T>
class Vector{
private:
int length;
T* data;
public:
/*
Set of constructors and other operators
*/
template<typename S>
Vector<typename std::common_type<S, T>::type> operator+(const Vector<S>& other) const
{
if(length != other.length) throw "Vectors do not have equal length!";
Vector<typename std::common_type<S, T>::type> result(length);
for(auto i = 0; i < length; i++)
{
result.data[i] = data[i] + other.data[i];
}
return result;
}
};
当此代码对于具有不同数据类型(例如 int 和 double)的两个向量 运行 时,它将开始抱怨 other.length 和 result.data 以及 other.data 是在此范围内全部声明为私有。错误消息如下所示:
./student_code.cpp: In instantiation of 'Vector<typename std::common_type<S, T>::type> Vector<T>::operator+(const Vector<S>&) const [with S = double; T = int; typename std::common_type<S, T>::type = double]':
./student_code.cpp:140:20: required from here
./student_code.cpp:91:28: error: 'int Vector<double>::length' is private within this context
91 | if(length != other.length) throw "Vectors do not have equal length!";
| ~~~~~~^~~~~~
./student_code.cpp:13:9: note: declared private here
13 | int length;
| ^~~~~~
./student_code.cpp:97:20: error: 'double* Vector<double>::data' is private within this context
97 | result.data[i] = data[i] + other.data[i];
| ~~~~~~~^~~~
./student_code.cpp:14:8: note: declared private here
14 | T* data;
| ^~~~
./student_code.cpp:97:46: error: 'double* Vector<double>::data' is private within this context
97 | result.data[i] = data[i] + other.data[i];
| ~~~~~~^~~~
./student_code.cpp:14:8: note: declared private here
14 | T* data;
| ^~~~
假设另一个 Vector 具有相同的数据类型(因此代码没有类型名称为 S 的其他模板),当我重载运算符时,这不是问题。我不知道为什么会这样,非常欢迎任何帮助。
问题是当你实例化说Vector<int>
和Vector<float>
时,那么这两个是 不同的类型 。而且,目前他们都不允许访问对方的内部。
要解决提到的错误,您应该添加一个好友声明,如下所示:
template<typename T>
class Vector{
private:
//friend declaration
template<typename S> friend class Vector;
int length;
T* data;
public:
//other members here
};
我正在尝试重载矢量 class 的 + 运算符。向量 class 能够使用模板指定数据类型。 + 运算符应该能够采用两个不同的向量并创建一个具有主导数据类型的新结果向量。目前我的代码如下所示:
template<typename T>
class Vector{
private:
int length;
T* data;
public:
/*
Set of constructors and other operators
*/
template<typename S>
Vector<typename std::common_type<S, T>::type> operator+(const Vector<S>& other) const
{
if(length != other.length) throw "Vectors do not have equal length!";
Vector<typename std::common_type<S, T>::type> result(length);
for(auto i = 0; i < length; i++)
{
result.data[i] = data[i] + other.data[i];
}
return result;
}
};
当此代码对于具有不同数据类型(例如 int 和 double)的两个向量 运行 时,它将开始抱怨 other.length 和 result.data 以及 other.data 是在此范围内全部声明为私有。错误消息如下所示:
./student_code.cpp: In instantiation of 'Vector<typename std::common_type<S, T>::type> Vector<T>::operator+(const Vector<S>&) const [with S = double; T = int; typename std::common_type<S, T>::type = double]':
./student_code.cpp:140:20: required from here
./student_code.cpp:91:28: error: 'int Vector<double>::length' is private within this context
91 | if(length != other.length) throw "Vectors do not have equal length!";
| ~~~~~~^~~~~~
./student_code.cpp:13:9: note: declared private here
13 | int length;
| ^~~~~~
./student_code.cpp:97:20: error: 'double* Vector<double>::data' is private within this context
97 | result.data[i] = data[i] + other.data[i];
| ~~~~~~~^~~~
./student_code.cpp:14:8: note: declared private here
14 | T* data;
| ^~~~
./student_code.cpp:97:46: error: 'double* Vector<double>::data' is private within this context
97 | result.data[i] = data[i] + other.data[i];
| ~~~~~~^~~~
./student_code.cpp:14:8: note: declared private here
14 | T* data;
| ^~~~
假设另一个 Vector 具有相同的数据类型(因此代码没有类型名称为 S 的其他模板),当我重载运算符时,这不是问题。我不知道为什么会这样,非常欢迎任何帮助。
问题是当你实例化说Vector<int>
和Vector<float>
时,那么这两个是 不同的类型 。而且,目前他们都不允许访问对方的内部。
要解决提到的错误,您应该添加一个好友声明,如下所示:
template<typename T>
class Vector{
private:
//friend declaration
template<typename S> friend class Vector;
int length;
T* data;
public:
//other members here
};