-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;
}
Blake
  • 40
  • 9
  • 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? – molbdnilo Nov 10 '16 at 16:18
  • @molbdnilo, may be it just posts random questions on SO? ;) – SergeyA Nov 10 '16 at 16:22
  • The following errors are given: missing type specifier - int assumed. Line: 17 'data': unknown override specifier Line: 17 – Blake Nov 10 '16 at 16:23
  • Maybe you want to change `Node(string value);` to `Node(std::string value);` in the header. And also add `#include ` to the header before your class declaration. – drescherjm Nov 10 '16 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 `` 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. – WhozCraig Nov 10 '16 at 16:24
  • see `using namespace std` – Bruno Ferreira Nov 10 '16 at 16:24
  • `using namespace std` is in the cpp file not the header. Well at least the code we see. – drescherjm Nov 10 '16 at 16:25
  • 1
    @BrunoFerreira and then [forget you ever thought about `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – WhozCraig Nov 10 '16 at 16:26
  • @WhozCraig indeed `using namespace std` is terrible. I myself rather continue `using std::whatever` :) – Bruno Ferreira Nov 10 '16 at 16:31
  • Can anyone also tell my why "Data is not a member of node"? – Blake Nov 10 '16 at 16:57

1 Answers1

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;
}
Community
  • 1
  • 1
speed488
  • 172
  • 10
  • [Or maybe `#pragma once` instead of include guards](http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards) – Bruno Ferreira Nov 10 '16 at 16:37
  • 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. – Blake Nov 10 '16 at 16:40
  • ***It suggests string is not a component of the std library.*** Did you replace `string` with `std::string` in the header and cpp file? – drescherjm Nov 10 '16 at 16:43
  • Did you replace string with std::string in the header and cpp file? Yes I did and it works now. – Blake Nov 10 '16 at 16:59