我无法解决错误。标识符“ node”在.cpp文件的最后第二行中未定义。我想返回该节点,但是编译器显示错误文件,请帮助我解决该问题。
#include<iostream>
using namespace std;
class Node
{
private:
int data;
Node *node;
public:
Node(int, Node *);
void setdata(int);
void setnode(Node *);
int getdata() const;
Node *getnode();
};
#include<iostream>
#include"Node.h"
using namespace std;
Node::Node(int Data = 0, Node *n = NULL)
{
setdata(Data);
setnode(n);
}
void Node::setdata(int D)
{
data = D;
}
void Node::setnode(Node *N)
{
node = N;
}
int Node::getdata() const
{
return data;
}
Node Node::*getnode()
{
return node;
}
答案 0 :(得分:0)
class Node {
private:
// ...
public:
// ...
Node *getnode(); // Returning a pointer to a Node, my guess is the * placement
// is confusing you
};
// Other implementations
Node* Node::getnode() // Means the same thing as above; * placement is relatively
// flexible
{
return node; // return type now matches what you're returning
}
您的函数实现编写不正确就是全部。鉴于C ++通常有多严格,放置*的灵活性可能会使初学者感到困惑。