views:

98

answers:

5
class Newstring
{
public:
    Newstring();
    void inputChar ( char);
    void display ();
    int length ();
    void concatenate (char);
    void concatenate (Newstring);
    bool substring (Newstring);
    void createList ();
    Node * getHead (); // error
private:
    struct Node
    {
     char item;
     Node *next; 
    };
    Node *head;

};

I am getting a syntax error : missing ';' before '*' on the declaration for my getHead function (yes I can't think of a better name). The purpose of this function is to return the head pointer.

+1  A: 
Node * getHead()

Compiler is not able to get the definition of Node when getHead() is encountered.

  struct Node
    {
        char item;
        Node *next; 
    };

Put the definition of Node before it is used.

class Newstring
{
private:
    struct Node
    {
        char item;
        Node *next; 
    };
    Node *head;
public:
    Newstring(); ...
    Node * getHead ();
aJ
+3  A: 

Declare Node before you use it.

peterchen
+2  A: 

You have to declare the Node struct above getHead();

class Newstring
{

public:
    struct Node
    {
        char item;
        Node *next; 
    };
    Newstring();
    void inputChar ( char);
    void display ();
    int length ();
    void concatenate (char);
    void concatenate (Newstring);
    bool substring (Newstring);
    void createList ();
    Node * getHead (); // error
private:

    Node *head;

};
Viktor Sehr
Is there a way to do it by keeping the struct private?
Brandon
Define in private block.
aJ
Brandon> you can just declare the structure, added an answer to show how.
Klaim
+2  A: 

To answer Brandon about keeping the struct in private, or keeping the current code while adding a declaration, the way is :

class Newstring
{
    struct Node; // here the declaration
public:

    Newstring();
    void inputChar ( char);
    void display ();
    int length ();
    void concatenate (char);
    void concatenate (Newstring);
    bool substring (Newstring);
    void createList ();
    Node * getHead (); // error
private:
    struct Node
    {
        char item;
        Node *next; 
    };
    Node *head;

};
Klaim
+1  A: 

Another way would be to forward declare Node by placing a struct before Node

    :
    void createList ();
    struct Node * getHead ();
private:
    struct Node
    {
        :
epatel
This both keeps coding convention and makes the code clearer
Daniel Goldberg