模板重载=操作符在一个类中工作,在另一个类中无法编译
Template overloaded = operator works in one class and fails to compile in another?
我做了一个小的自定义数组容器列表前一段时间,它工作得很好,这就是我如何重载它的'='操作符:
List<T>& operator=(const List<T>& other); //in List.h
//in List.inl
template<typename T>
List<T>& List<T>::operator=(const List<T>& other)
{
_size = other._size;
if(_size < _capacity)
{
_capacity = _size;
_AdaptCapacityChange();
}
for(uint i = 0; i < other._size; i++)
{
_data[i] = other._data[i];
}
return(*this);
}
然而,现在我在另一个类中做了同样的事情:
PointIndices<T>& operator=(const PointIndices<T>& point); //in PointIndices.h
//in PointIndices.inl
template<typename T>
PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
{
indices[0] = point.indices[0];
return(*this);
}
它不突出PointIndices和操作符关键字保持蓝色,编译器给我:错误2错误C4430:缺少类型说明符- int假设。注意:c++不支持default-int
在这两种情况下,我都正确地包含了.inl文件,PointIndices的其余方法工作得很好,只有操作符给了我一个问题。然而,在List中,相同的重载操作符可以正常工作。我很困惑,是什么引起的?
EDIT: requested testcase:
头: template<class T>
class PointIndices
{
public:
PointIndices();
PointIndices(T P1);
virtual ~PointIndices();
PointIndices<T>& operator=(const PointIndices<T>& point);
T P1() const;
T& P1();
protected:
T indices[1];
};
#include "PointIndices.inl"
INL文件:
template<typename T>
PointIndices<T>::PointIndices()
{
indices[0] = 0;
}
template<typename T>
PointIndices<T>::PointIndices(T P1)
{
indices[0] = P1;
}
template<typename T>
PointIndices<T>::~PointIndices()
{
}
template<typename T>
PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
{
indices[0] = point.indices[0];
return(*this);
}
template<typename T>
T PointIndices<T>::P1() const
{
return(indices[0]);
}
template<typename T>
T& PointIndices<T>::P1()
{
return(indices[0]);
}
您声明了一个类模板PointIndices
,但在函数定义中拼写错误:
template<typename T>
PointIndicess<T>& PointIndices<T>::operator=(const PointIndicess<T>& point)
// ^ extra "s" here ^ and here
相关文章:
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- 给定的两个代码有什么区别.一个在 ideone 上运行时超出时间限制,另一个工作正常
- Rcpp:一个函数在NumericVector上工作,另一个需要将其视为标量
- 在删除另一个类中的一个类的对象时析构函数如何工作
- VS为我提供了对构造函数的另一个解决方案,但我想知道为什么我的工作不起作用
- OpenCV:文件存储操作员">>"在一个项目中工作,但不在另一个项目中工作(在同一类上)
- LNK2019与另一个工作项目具有相同属性的项目上的错误
- xcb_get_image_reply另一个工作区/桌面上的窗口失败
- 指针将其复制到另一个指针时的工作方式
- c++类函数在另一个函数中不能按预期工作
- 如何在C++中使一个void函数与另一个void功能协同工作
- C++ 不允许我在以前工作后通过引用将 vector 传递给另一个函数
- 一个C++客户端无法使用我们新的 COM 注册的 .NET .dll加载,另一个客户端可以正常工作
- glAccum(模糊效果)工作与一个显卡,但不是另一个
- 如何将一个窗口的工作区复制到另一个窗口?
- 头文件在一个#include中工作正常,在另一个#include中导致语法错误
- 在C/ c++中,当两个函数具有相同的名称,一个函数调用另一个函数时,它将工作
- 模板被另一个模块使用时是如何工作的
- 模板重载=操作符在一个类中工作,在另一个类中无法编译
- 构造函数在必须创建另一个类的新对象时是如何工作的