0

I have this basic code here: it's a Node that's part of a List, and each Node holds Data

#ifndef NODE_H_
#define NODE_H_
#include "Data.h"

namespace std {

class Node {
public:
    Node();
    Node(Data* aItem);
    virtual ~Node();
    virtual Node* getNext();
    virtual void setNext(Node * linkedNode);
    virtual Data* getData();
    virtual void setData(Data * item);
private:
    Node *next;
    Data *item;
};

} /* namespace std */

#endif /* NODE_H_ */

For "Data *item;", as you can see at the top I have included Data.h, is there a reason why its saying Data does not name a type?

Error log:

..\Node.h:17:11: error: expected ')' before '*' token
  Node(Data* aItem);
           ^
..\Node.h:21:13: error: 'Data' does not name a type
     virtual Data* getData();
             ^
..\Node.h:22:26: error: 'Data' has not been declared
     virtual void setData(Data * item);
                          ^
..\Node.h:25:2: error: 'Data' does not name a type
  Data *item;

Edit: added Data.h

#ifndef DATA_H_
#define DATA_H_

#include "List.h"

namespace std {

class Data {

private:
    int ID;
    List* listOne;
    List* listTwo;

public:
    Data();
    Data(int aID);
    virtual ~Data();
    virtual int getID();
    virtual void setID(int aID);
    virtual List* getListOne();
    virtual List* getListOne();
};

} /* namespace std */

#endif /* DATA_H_ */

List.h:

#ifndef LIST_H_
#define LIST_H_
#include "Node.h"
#include "Data.h"

namespace std {

class List {
private:
    std::Node *top;
public:
    List();
    virtual ~List();
    virtual Node* getTop();
    virtual void add(Node * linkedNode);
    virtual void remove(Node * linkedNode);
};

} /* namespace std */

#endif /* LIST_H_ */
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Please post Data.h. – Code-Apprentice Mar 25 '17 at 04:16
  • 2
    p.s. You should not declare your own types in the `std` namespace. Either use the global namespace or, better yet, create your own namespace. – Code-Apprentice Mar 25 '17 at 04:16
  • I have updated OP with the code, and unfortunately I am required to use std namespace by my instructor – user2946986 Mar 25 '17 at 04:20
  • Your instructor should be shot. – Code-Apprentice Mar 25 '17 at 04:25
  • Try changing `Data` to `std::Data`. – Code-Apprentice Mar 25 '17 at 04:25
  • I think the issue is, I have another class List which is including Nodes. But Nodes are including Data, and Data includes List, so it might be some weird inheritance cycle, what would be a good way to implement this sort of setup? – user2946986 Mar 25 '17 at 04:29
  • Yes, circular dependencies will cause issues unless handled properly. And do ask the instructor did they *really* mean your code should be in std or did you misunderstand what they said about using it. – Sami Kuhmonen Mar 25 '17 at 04:31
  • Please post a complete code example. You should post enough code to reproduce the exact error message you are getting and no more. I want to be able to copy and paste your code and compile it myself. The current version of your code is missing the List.h file. – Code-Apprentice Mar 25 '17 at 04:33
  • The instructor told us to just use std namespace for everything, i'm assuming to keep it low level still, and how would I fix the circular dependency issue? Since List needs Nodes, but Data needs to also hold Lists for other information – user2946986 Mar 25 '17 at 04:34
  • To fix the circular dependencies, you need to learn about forward declarations. – Code-Apprentice Mar 25 '17 at 04:34
  • I updated the OP with my list.h, Stackoverflow was throwing the too much code not enough text so had to edit the OP a bit – user2946986 Mar 25 '17 at 04:39
  • Okay, I get the same error now. I suggest you google "forward declarations c++" to find more information about how to fix the problem. – Code-Apprentice Mar 25 '17 at 04:40
  • The above link will help. Note that even though the original question is about a function declaration, the same issues apply to class declarations. The accepted answer shows an example using a class. – Code-Apprentice Mar 25 '17 at 04:42
  • How would you pick which way to forward declare? Would I forward Node to List or forward List to Data? – user2946986 Mar 25 '17 at 04:43
  • 1
    To the compiler, It does not matter. You just need at least one forward declaration to break the cycle. However, if you want to be safe and avoid some headaches, you can just do every forward declaration. – Code-Apprentice Mar 25 '17 at 04:50

0 Answers0