-1

I am trying to create a Linked List to download a txt file and use the linked list to handle the file line by line. When handling the downloaded Linked List operations will be performed on it such as a text editor would. I am encountering some problems however. It seems the "Node(string value)" section of the code has something wrong with it even though the original Node() declaration with no arguments passes. I am unable to figure out quite what it is.

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;
}
10
  • 2
    Please be more specific than "some problems" and "something wrong". Does it not compile? Does it crash? Does it print "0x3434" all over your terminal? Does it call the police and report that you've gone missing? Commented Nov 10, 2016 at 16:18
  • @molbdnilo, may be it just posts random questions on SO? ;) Commented Nov 10, 2016 at 16:22
  • The following errors are given: missing type specifier - int assumed. Line: 17 'data': unknown override specifier Line: 17 Commented Nov 10, 2016 at 16:23
  • 1
    The problem is there is no string. There is std::string. Related, never, ever, build a header-dependency chain in a source file's include list. If header X.h doesn't work until the source file including it includes Y.h before it, you did it wrong. As written even if you fixed what I mentioned Node.h is useless unless <string> is included by the source cpp file first. Properly include-guard your headers and have them pull in what you need (and nothing more) for that header. Commented Nov 10, 2016 at 16:24
  • 1
    @BrunoFerreira and then forget you ever thought about using namespace std;. Commented Nov 10, 2016 at 16:26

1 Answer 1

2

Your #include <string> should be in your header file since it is you are using the std::string type with your methods.

Since it is not recommended to add using namespace in header files (see this answer for more info), declare your string types with the namespace : change your string value to std::string value

Your files will look like this (compile test was done with GCC)

Also you should put an include guard in your header file

Example:

// 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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

It suggests string is not a component of the std library. Also I already have #ifndef NODE_H #define NODE_H and #endif.. they are just not what I'm asking for.
It suggests string is not a component of the std library. Did you replace string with std::string in the header and cpp file?
Did you replace string with std::string in the header and cpp file? Yes I did and it works now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.