Tôi có một lớp thông thường (không phải là một mẫu, có nghĩa là) với một nhà điều hành người bạn tin < <tài liệu tham khảo không xác định để điều hành <<
nó tuyên bố là:
std::ostream& operator<<(std::ostream& out, const Position& position);
trong cpp nộp đó là định nghĩa là:
std::ostream& operator<<(std::ostream& out, const Position& position)
{
out << position.getXPoint() << "," << position.getYPoint();
return out;
}
nó đang được biên dịch rồi liên kết với chức năng chính sử dụng nó, tuy nhiên khi tôi sử dụng nó, tôi nhận được tham chiếu không xác định ...
tuy nhiên khi tôi thêm các định nghĩa để phía trên cùng của tập tin cpp chính và loại bỏ các tuyên bố người bạn, nó hoạt động tốt ...
heres làm thế nào tôi đang sử dụng nó trong chức năng chính của tôi
std::cout << node->getPosition() << std::endl;
không hơn không kém ...
heres lỗi mối liên kết
/home/luke/Desktop/pathfinder/parse_world.cpp:34: undefined reference to `pathfinder::operator<<(std::ostream&, pathfinder::Position const&)'
và heres lớp tiêu đề ...
#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
bây giờ sau khi loại bỏ các tuyên bố người bạn Tôi nhận được thông báo lỗi như:
cannot bind ‘std::ostream {aka std::basic_ostream}’ lvalue to ‘std::basic_ostream&&’
nó được xảy ra trên cùng một dòng như tuyên bố std :: cout ...
Vì vậy, whats thỏa thuận ...
cảm ơn trước
Bạn có thể đăng cách bạn đang gọi nó trong chức năng chính không? – MrWuf
Có vẻ như tệp cpp của bạn không liên kết đúng cách. Có gì khác trong đó làm việc? – chris
Có, mọi thứ khác hoạt động, như các nhà xây dựng và các nhà hủy diệt –