0

I am trying to make a simple linked list. Everything was going fine, and then all of a sudden, a massacre of errors. I have no clue what I changed to break my code. This is my file that is getting some of the errors:

#pragma once

#include <string>

#include "Node.h"

class LinkedList
{
private:
    Node *head;
public:
    LinkedList();
    ~LinkedList();
    void AddNode(int);
    string GetList();  //missing ';' before identifier 'GetList'
    bool Contains(int);
    void Remove(int);
};

It claims that I am missing a semi-colon on the line above string GetList();, or so it looks...but obviously I am not. That exact error is:

Error   1   error C2146: syntax error : missing ';' before identifier 'GetList' c:\...\linkedlist.h 15  1   ProjectName

The other error on that line is:

Error   2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\...\linkedlist.h 15  1   ProjectName

But it is identified as a string return type.

In LinkedList.cpp, this is the GetList() method:

string LinkedList::GetList(){
    string list;
    Node *currentNode = head;
    while (currentNode->next_node){
        currentNode = currentNode->next_node;
        list += currentNode->get_value() + " ";
    }
    return list;
}

It all appears good there, but in the method header, I am getting the following 2 errors:

Error   4   error C2556: 'std::string LinkedList::GetList(void)' : overloaded function differs only by return type from 'int LinkedList::GetList(void)' c:\...\linkedlist.cpp   28  1   ProjectName

Error 5 error C2371: 'LinkedList::GetList' : redefinition; different basic types c:...\linkedlist.cpp 28 1 ProjectName

I have gone so far as to create a new project and copy and paste all of my files back in, but that had no effect. I had successfully run GetList() in this program previously.

Does anyone know what in the world is going on here? My IDE is lying to me! (Visual Studio Community 2013 Update 4)

Evorlor
  • 7,263
  • 17
  • 70
  • 141

2 Answers2

4

You have using namespace std; somewhere in LinkedList.cpp, but not in LinkedList.h. That's why in the class definition it doesn't know you're referring to std::string when you write string.

I'd recommend to stop using using namespace std; to avoid this kind of problems.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • You suggest typing std:: every time I use the std namespace? – Evorlor Mar 01 '15 at 03:35
  • 1
    @Evorlor Yes, that's the recommended way in C++. – Emil Laine Mar 01 '15 at 03:35
  • 1
    @Evorlor See [the answers here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) for a more in-depth explanation. – Emil Laine Mar 01 '15 at 03:38
  • think its safe to use using namespace std; in my main.cpp? – Evorlor Mar 01 '15 at 03:44
  • 1
    `using namespace std;` is never always safe. Using unqualified names can always induce programming errors when the programmer refers to a different name by accident (as you just did). Referring to all names in the namespace std prefixed with `std::` reduces the number of these accidents. – Emil Laine Mar 01 '15 at 03:49
  • understood. thanks. one last question if u dont mind...what about using "using std::cout;" and "using std::cin;" and "using std::endl;" in my main.cpp? would that be safe? – Evorlor Mar 01 '15 at 03:51
  • Using `using` statements for just `cout`, `cin` and `endl` is probably acceptable, since it's really unlikely that there would be other objects with the same names. And especially if you use those a lot in your code. – Emil Laine Mar 01 '15 at 03:57
3

Use std::string, not just string.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Awesome! I don't know how I ever would've figured it out (despite it being so simple) without your help. Terrible error messages. Thank you!! – Evorlor Mar 01 '15 at 03:34
  • 2
    @Evorlor: The error messages actually make sense. The compiler cannot know that `string` is a type when it has no idea what it is. You confused it, and it cannot read your mind. You say that the function has "string return type" but "string" does not exist: it is "std::string". All the compiler can do is to report the way in which the grammar has been broken. Over time, with experience, you will learn to think the way the compiler does and then you will intuitively understand the messages. – Lightness Races in Orbit Mar 01 '15 at 03:36
  • @LightnessRacesinOrbit thanks for the words of encouragement :-) and ya, that makes sense – Evorlor Mar 01 '15 at 03:37