【发布时间】:2013-10-10 13:08:28
【问题描述】:
我有一个由几个子模块组成的项目。因为我有一些结构,例如点或矩形,我想有一个单独的头文件,其中定义了这些数据结构及其运算符。然后将其包含在其他源文件中。我有
结构.hpp
namespace datastructures {
struct Rectangle {
int width;
int height;
};
bool operator<=(const Rectangle& lhs, const Rectangle& rhs){
return lhs.width <= rhs.width;
}
}// end namespace
算法.hpp
我有另一个文件 Algorithm.hpp,它看起来类似于:
#include "structures.hpp"
class Algorithm {
public:
typedef datastructures::Rectangle Rectangle;
void func1(int median);
private:
std::vector<Rectangle> rectangles_;
}
这一切都很好。但是使用运算符似乎根本不起作用。
算法.cpp
void Algorithm::func1(int median){
std::nth_element(rectangles_.begin(),
rectangles_.begin() + median, rectangles_.end(), datastructures::operator<=);
}
这会给模板带来编译错误,最有意义的是
no matching function for call to
‘nth_element(std::vector<datastructures::Rectangle>::iterator&,
std::vector<datastructures::Rectangle>::iterator&,
std::vector<datastructures::Rectangle>::iterator&,
<unresolved overloaded function type>)’
为什么它不知道我的数据结构头文件中的operator<=?
【问题讨论】:
标签: c++ struct namespaces operators