2016-11-10 162 views
-1

我想创建一个链接列表来下载一个txt文件,并使用链表来逐行处理文件。处理下载的链接列表操作时,将对其执行操作,例如文本编辑器。 但是我遇到了一些问题。看起来即使没有参数的原始Node()声明通过,代码段的“节点(字符串值)”部分也有问题。我无法弄清楚它是什么。C++链接列表

Node.h

class Node 
{ 
public: 
    Node(); 
    Node(string value); 
    void setNext(Node *nextNode); // Allows the user to set where the "next" pointer of a node points 

    friend class LinkedList; 
private: 
    string data; // Data box 
    Node* next; // Pointer box 
}; 

Node.cpp

# include <string> 
# include "Node.h" 

using namespace std; 

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

Node::Node(string value) 
{ 
    data = value; 
    next = NULL; 
} 

void Node::setNext(Node *nextNode) // Allows the user to set where the "next" pointer of a node points 
{ 
    this->next = nextNode; 
} 
+2

请比 “一些问题” 和 “不对劲” 更具体。它不会编译?它会崩溃吗?它是否在您的终端上打印“0x3434”?它报警并报告你失踪了吗? – molbdnilo

+0

@molbdnilo,可能只是在SO上发布随机问题? ;) – SergeyA

+0

给出以下错误: 缺少类型说明符 - int假定。行:17 'data':未知的重写说明符行:17 – Blake

回答

2

#include <string>应该在你的头文件,因为它是你正在使用的std::string类型与方法。

由于不建议在头文件(见this answer获取更多信息),与命名空间声明的字符串类型添加using namespace:改变你的string valuestd::string value

你的文件看起来像这样(编译试验与海湾合作委员会完成)

你也应该把一个包括后卫在你的头文件

例子:

// some_header_file.h 
#ifndef SOME_HEADER_FILE_H 
#define SOME_HEADER_FILE_H 
// your code 
#endif 

Node.h

#include <string> 

class Node 
{ 
public: 
    Node(); 
    Node(std::string value); 
    void setNext(Node *nextNode); // Allows the user to set where the "next" pointer of a node points 

    friend class LinkedList; 
private: 
    std::string data; // Data box 
    Node* next; // Pointer box 
}; 

Node.cpp

#include "Node.h" 
#include <cstddef> // For NULL 

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

Node::Node(std::string value) 
{ 
    data = value; 
    next = NULL; 
} 

void Node::setNext(Node *nextNode) // Allows the user to set where the "next" pointer of a node points 
{ 
    this->next = nextNode; 
} 
+0

[或者'也许'#pragma once'而不是包含守卫](http:// stackoverflow。com/questions/1143936/pragma-once-vs-include-guard) –

+0

它建议字符串不是std库的一个组件。 另外我已经有 #ifndef NODE_H #define NODE_H 和#endif ..他们只是不是我要求的。 – Blake

+0

***它建议字符串不是std库的一个组成部分。***你是否在头文件和cpp文件中用'std :: string'替换了'string'? – drescherjm