Thread: Linked List problems

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Question Linked List problems

    Hey, I'm starting to program a linked list application, so I am playing around with creating Nodes to familiarize myself with them. I created a very simple program how I could practice creating a new node and pointing the previous node pointer to the new node, but it won't compile. Here is my code:

    myLL.h
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    #include <string>
    
    struct Node
    {
        std::string nvalue;
        Node *ptr;
    }
    
    #endif
    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "myLL.h"
    
    5: int main(int argc, char *argv[])
    6: {
    7:    Node myNode;
    8:    myNode.nvalue = "Hello";
    9:
    10:    system("PAUSE");
    11:    return 0;
    }
    Error:
    Code:
    6 main.cpp new types may not be defined in a return type 
    6 main.cpp extraneous `int' ignored 
    6 main.cpp `main' must return `int' 
    6 main.cpp return type for `main' changed to `int' 
    Makefile.win [Build Error]  [main.o] Error 1
    I don't see where the compiler is getting the "extraneous int" and why it does not think that main.cpp is not returning an int.

    What I am trying to do: each Node structure has a string nvalue and a pointer to a node ptr. The user will be able to input as many strings as he/she wants, and for each new string the program will create a new node, assign nvalue to the string that was just inputted, and update the previous node ptr to point to the new node. Of course, I have not implemented all of this yet.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    #include <string>
    
    struct Node
    {
        std::string nvalue;
        Node *ptr;
    };
    
    #endif
    Missing semicolon. Sometimes an error elsewhere hundreds of lines before the printed error message will actually be the real cause of the problem.

    [edit]I just realized that this is C++ code. This is the C Board, not the C++ Board. This post needs to be moved.[/edit]
    Last edited by hk_mp5kpdw; 10-10-2007 at 05:33 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    First of all what you are writing isn't C at all... next time, please post in the right section (!).

    And yeah.. there's a semicolon missing.. and note that in C if you define a struct that way
    Code:
    struct myStruct
    {
       int i;
    };
    you must declare a variable this way
    Code:
       ...
       struct myStruct ss;
       ...

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ops, sorry about the wrong forum.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM