未定义的对运算符的引用<<

时间:2012-05-23 01:34:49

标签: c++ operators operator-overloading

我有一个私人朋友运营商<<

的常规课程(不是模板)

它的声明是:

std::ostream& operator<<(std::ostream& out, const Position& position);

在cpp文件中它的定义是:

std::ostream& operator<<(std::ostream& out, const Position& position)
{
  out << position.getXPoint() << "," << position.getYPoint();
  return out;
}

它正在编译然后链接到使用它的main函数,但是当我使用它时,我得到一个未定义的引用...

然而,当我将定义添加到主cpp文件的顶部并删除友元声明时,它工作正常......

继承人我在主要功能中如何使用它

std::cout << node->getPosition() << std::endl;

不多也不少......

继承人错误

  

/home/luke/Desktop/pathfinder/parse_world.cpp:34:对`pathfinder :: operator&lt;&lt;(std :: ostream&amp;,pathfinder :: Position const&amp;)&#39;

并且继承了班级标题......

#ifndef PATHFINDER_H
#define PATHFINDER_H
#include <ostream>
#include <istream>
#include <list>
#include <vector>
#include <stdexcept>
#include <cstring>
namespace pathfinder
{
class Node;
typedef unsigned int GCost;
typedef std::vector<std::vector<Node*> > World;
typedef std::vector<Node*> WorldRow;
class Position
{
public:
    typedef unsigned int point_type;
private:
    point_type* xPoint_;
    point_type* yPoint_;
    friend std::ostream& operator<<(std::ostream& out, const Position& position);
public:
    Position(const point_type& xPoint = 0, const point_type& yPoint = 0);
    Position(const Position& position);
    Position(Position&& position);
    ~Position();

    Position& operator=(const Position& position);
    Position& operator=(Position&& position);

    point_type getXPoint() const;
    point_type getYPoint() const;

    void setXPoint(const point_type& xPoint);
    void setYPoint(const point_type& yPoint);
};
class Node
{
private:
    bool* isPassable_;
    bool* isStartingNode_;
    bool* isTargetNode_;
    Position* position_;
    GCost* additionalGCost_;
    Node* parent_;
public:
    Node(const bool& isPassable = true, const bool& isStartingNode = false, const bool& isTargetNode = false, const Position& position = Position(0,0), const GCost& additionalGCost = 0, Node* parent = nullptr);
    Node(const Node& node);
    Node(Node&& node);
    ~Node();

    Node& operator=(const Node& node);
    Node& operator=(Node&& node);

    bool isPassable() const;
    bool isStartingNode() const;
    bool isTargetNode() const;
    Position getPosition() const;
    GCost getAdditionalGCost() const;
    Node* getParent() const;

    void setAsPassable(const bool& isPassable);
    void setAsStartingNode(const bool& isStartingNode);
    void setAsTargetNode(const bool& isTargetNode);
    void setPosition(const Position& position);
    void setAdditionalGCost(const GCost& additionalGCost);
    void setParent(Node* parent);
};
class WorldHelper
{
public:
    static World fromStream(std::istream& input);
    static void syncPositions(World& world);
    static void uninitializeWorld(World& world);
    static Node* findStartingNode(const World& world);
    static Node* findTargetNode(const World& world);
};
class Pathfinder
{
public:
    virtual std::list<Node*> operator()(World& world) const = 0;
};
};
#endif //PATHFINDER_H

现在删除好友声明后,我收到错误消息,如:

  

无法将'std :: ostream {aka std :: basic_ostream}'左值绑定到'std :: basic_ostream&amp;&amp;'

它与std :: cout语句在同一行发生......

那么,这笔交易是什么......

提前致谢

2 个答案:

答案 0 :(得分:16)

我的猜测,从描述中你要声明两个operator<<但只定义一个namespace A { struct B { friend std::ostream& operator<<( std::ostream&, B const & ); // [1] }; } std::ostream& operator<<( std::ostream& o, A::B const & ) { // [2] return o; } 。例如:

A::operator<<

第[1]行声明了一个B函数,可以通过类型::operator<<上的ADL找到,但[2]声明并定义A::B b; std::cout << b; 。当编译器看到代码时:

A::operator<<

它使用ADL并找到friend(来自友元声明)并使用它,但该函数未定义。如果删除operator<<声明,则在全局命名空间中声明并定义了一个A实例,并且可以通过常规查找找到它。

另请注意,如果程序中存在使用指令,则可能难以发现,因为[2]中的定义可能未明确命名参数的// cpp [assume that the definition of B contained a member f using namespace A; void B::f() { } 命名空间。这也可以解释您可以定义该类型的其余成员:

B::f

B的定义驻留在全局命名空间中(在代码中),但由于using指令,A将在A::B命名空间中找到,类型说明符将是相当于void ::A::B::f() {}在解析B后,定义等同于namespace A { struct B { friend std::ostream& operator<<( std::ostream&, B const & ); }; std::ostream& operator<<( std::ostream&, B const & ); } std::ostream& A::operator<<( std::ostream& o, B const & ) { return o; } 的原因。对于免费功能,这将

我建议您避免使用指令,因为它们允许像这样的细微错误。另请注意,您实际上可以在命名空间中明确地定义运算符(但是您还需要在命名空间中声明它:

namespace A {
   struct B  {
      friend std::ostream& operator<<( std::ostream&, B const & ); // [1]
   };
   std::ostream& operator<<( std::ostream&, B & ) {                // [3]
      return o;
   }
}

这个技巧(通过完全限定来定义其自然命名空间之外的自由函数)有时用于避免隐式声明它的函数的定义,这容易出现这种类型的错误。例如,如果您在适当的命名空间中定义了运算符,但签名略有不同:

{{1}}

[3]中的定义也是一个声明,但是它声明了一个与[1]中声明的函数不同的函数,你最终可能会不知道链接器为什么找不到[1]。

答案 1 :(得分:0)

大卫对这个问题给出了很好的答案。在这里,我只是提供一个易于理解的例子。

在我的标题文件中:

namespace BamTools {
class SomeBamClass {
.....
public:
    friend std::ostream& operator<<(std::ostream& ous, const SomeBamClass& ba);
    .....
}
}

在cpp文件中:

using namespace BamTools; // this work for Class member function but not friends
using namespace std;

........
std::ostream& operator<<(std::ostream &ous, const SomeBamClass &ba) {
  // actual implementation
  return ous;
}

当您尝试从您自己的应用程序链接到此库时,上面将为您提供对运算符&lt;&lt;(..,const SomeBamClass&amp;)的未定义引用,因为它是在命名空间之外的运算符的声明和实现。

undefined reference to `BamTools::operator<<(std::ostream&, BamTools::SomeBamClass const&)'

对我来说唯一有用的是在实现文件中的friend函数中添加名称空间括号:

namespace BamTools {
std::ostream& operator<<(std::ostream &ous, const SomeBamClass &ba) {
...
   return ous;
}
}

使用... BamTools :: operator&lt;&lt;(...,const BamTools :: SomeBamClass&amp;)将导致我的g ++ 5.4.0编译错误。

相关问题