简体   繁体   English

错误:未在此范围内声明“对象”(C ++)

[英]Error: 'object' was not declared in this scope (C++)

I'm trying to write a linked list of Node objects, where each Node contains: a string - data , and a pointer - next . 我正在尝试编写Node对象的链接列表,其中每个Node包含:字符串data和指针next To manage the list (eg: add/remove nodes), I have Node objects: head , curr and temp . 为了管理列表(例如:添加/删除节点),我有Node对象: headcurrtemp I'm trying to work out how to link up each node to the previous one when new data is added to the list, but I am having trouble doing so. 我正在尝试找出将新数据添加到列表时如何将每个节点链接到上一个节点,但是这样做很麻烦。 The error is occuring in Node.cpp when I am trying to link the new node n to the curr nodes next pointer. 当我尝试将新节点n链接到curr节点的next指针时,在Node.cpp中发生了错误。

LinkedList.cpp: LinkedList.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: 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

Node.cpp: Node.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: 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

Any help would be greatly appreciated, thanks guys. 谢谢大家的任何帮助。

You've got this line: 你有这行:

void setNext(Node n) {

You probably want this instead: 您可能想要这样:

void Node::setNext(Node n) {

The function void setNext(Node n); 函数void setNext(Node n); is declared in class Node . class Node声明。 So in Node.cpp , you must define the function with void Node::setNext(Node n); 因此,在Node.cpp ,必须使用void Node::setNext(Node n);定义函数void Node::setNext(Node n); . The symbol :: will show the function's scope. 符号::将显示函数的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM