gpt4 book ai didi

c++ - 错误 : 'object' was not declared in this scope (C++)

转载 作者:行者123 更新时间:2023-11-28 06:05:29 26 4
gpt4 key购买 nike

我正在尝试编写一个 Node 对象的链表,其中每个 Node 包含:一个字符串 - data 和一个指针- 下一步。为了管理列表(例如:添加/删除节点),我有 Node 对象:headcurrtemp。当新数据添加到列表时,我正在尝试弄清楚如何将每个节点链接到前一个节点,但我在这样做时遇到了麻烦。当我尝试将新节点 n 链接到 curr 节点 next 指针时,Node.cpp 中出现错误。

链表.cpp:

#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"

using namespace std;

LinkedList::LinkedList(value_type setData) {
head.setData(setData);
cout << head.getData() << endl;
}

void LinkedList::addNode(value_type addData) {
Node n;
n.setData(addData);

if (head.getData() != "") {
curr = head;
while(curr.getNext() != NULL) {
//Still writing this..
}
curr.setNext(n); //Is this correct?
}
else {
head = n;
}
}

LinkedList.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"

class LinkedList {
public:
typedef std::string value_type;

LinkedList();
LinkedList(value_type setData);
void addNode(value_type addData);

private:
Node head;
Node curr;
Node temp;

};

#endif

节点.cpp:

#include <iostream>
#include <cstdlib>
#include "Node.h"

using namespace std;

Node::Node() {
data = "";
next = NULL;
}

void Node::setData(value_type setData) {
data = setData;
}

string Node::getData() {
return data;
}

void setNext(Node n) {
next = n; //'next' was not declared in this scope error.
//Does this assignment statement even work in this scenario?
}

Node * Node::getNext() {
return next;
}

Node.h:

#ifndef NODE_H
#define NODE_H

class Node {
private:
typedef std::string value_type;

value_type data;
Node* next;

public:
Node();
void setData(value_type setData);
value_type getData();
void setNext(Node n);
Node * getNext();
};

#endif

非常感谢任何帮助,谢谢大家。

最佳答案

你有这一行:

void setNext(Node n) {

你可能想要这个:

void Node::setNext(Node n) {

关于c++ - 错误 : 'object' was not declared in this scope (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com