c++ - 如何有效地使用包含来自另一个源文件的运算符的结构定义?

标签 c++ struct namespaces operators

我有一个由几个子模块组成的项目。因为我有一些结构,例如点或矩形,我想要一个单独的头文件,其中定义了这些数据结构及其运算符。然后将其包含在其他源文件中。我有

结构.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<=来 self 的数据结构头文件?

最佳答案

错误是由于:

unresolved overloaded function type

必须有多个运算符匹配签名。 您可以使用类似 boost::function 的东西或函数指针来选择特定的重载或使用比较仿函数 http://en.cppreference.com/w/cpp/utility/functional/less_equal

例如:

#include <vector>
#include <algorithm>
#include <functional>

namespace datastructures {

  struct Foo;

  struct Rectangle {
    int width;
    int height;
  };

  bool operator<=(const Rectangle& lhs, const Rectangle& rhs){
    return lhs.width <= rhs.width;
  } // end namespace                                                                                                                                                                                                

  bool operator<=(const Foo&, const Foo&);

}

class Algorithm {
public:
  typedef datastructures::Rectangle Rectangle;
  void func1(int median);
private:
  std::vector<Rectangle> rectangles_;
};

// Algorithm.hpp                                                                                                                                                                                                    
void Algorithm::func1(int median){
  // this fails
  std::nth_element(rectangles_.begin(),
                   rectangles_.begin() + median, rectangles_.end(), datastructures::operator<=);
  // this works
  std::nth_element(rectangles_.begin(),
                   rectangles_.begin() + median, rectangles_.end(), std::less_equal<Rectangle>());

}

您还必须将比较函数声明为内联,否则您将在链接步骤中获得多个定义。

关于c++ - 如何有效地使用包含来自另一个源文件的运算符的结构定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19165902/

相关文章:

c++ - 使用 std::equal 和命名空间时出现 "No match operator=="错误

c++ - Anttweakbar glfw3 OpenGL 3.2 不绘图

c++ - 如何生成 LLVM 位码

c++ - boost::lockfree::queue 正在耗尽我的 CPU

c++ - 迭代器 for 循环不会确认循环完成条件

c - 复制字符串时出现段错误

clojure - 如何在当前命名空间中应用 use 子句中的符号

c - set_add 和 set_contains 错误

c++ - union c++ yacc 中的结构

c++ - 我应该为 size_t 包含 stddef.h 还是 cstddef