-3

I am trying to create a inventory linked list where the user can add a product(id,info,price,count) and then to store the object in a linked list. The issue I am having is with the Node class giving error "missing type specifier - int assumed. Note: C++ does not support default-int" and so on for each statement in the Node Class.

#ifndef INVENTORY_H
#define INVENTORY_H

#include<iostream>
#include <string>       
#include <iomanip>
using namespace std;

 class Node
{
public:
    Node(){}
    Node(const Inventory& theData, Node *theLink)
        : data(theData), link(theLink){}
    Node* getLink() const { return link; }
    Inventory getData() const { return data; }
    void setData(const Inventory& theData)
    {
        data = theData;
    }
    void setLink(Node *theLink) { link = theLink; }
private:
    Inventory data;
    Node *link;     //pointer that points to next node
};

class Inventory
{
public:
    Inventory();
    void addPart(int, string, int, int);
    void findPart(int);
    void toBinary();
    void quit();
private:
    int id;
    int price;
    string partInfo;
    int partCount;

    Node* first;
    Node* last;
    int count;
};
#endif

And the errors are:

1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2143: syntax error : missing ',' before '&'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2146: syntax error : missing ';' before identifier 'getData'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): warning C4183: 'getData': missing return type; assumed to be a member function returning 'int'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C2143: syntax error : missing ',' before '&'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C2146: syntax error : missing ';' before identifier 'data'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'Thedata' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'theLink' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2614: 'Node' : illegal member initialization: 'data' is not a base or member
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2065: 'data' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'data' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'theData' : undeclared identifier
Nignig
  • 9
  • 4

2 Answers2

1

You're just looking at a standard case of the compiler not knowing what your custom types are because of the order you have defined them. Even if you forward declare a class (put "class Inventory;" somewhere up above), you can't use it for certain things. Declaring a member variable that is not a pointer is one of them.

However, if you invert the definitions (Inventory before Node) and forward declare Node ("class Node;"), your code will compile because you only have Node* in your Inventory class.

Check this answer for more details: https://stackoverflow.com/a/553869/128581

As pointed out, this is not the best design if this code is not strictly for learning. STL containers cover most common data structures (doubly linked and singly linked lists are two of them). stl::list and stl::forward_list respectively.

Josh
  • 12,602
  • 2
  • 41
  • 47
0

The C++ compiler reads the source code from top to bottom. When it gets to line 13, it hasn’t heard of an Inventory type. It thinks Inventory must therefore be the parameter name, and gets really confused.

The solution to this is to switch the order of the Node and Inventory classes, and to pre-declare the Node class before the start of the Inventory class, by inserting the following line

class Node;

before the start of class Inventory.

Switching the order has Inventory defined before Node, which is important because the compiler needs to know everything about an Inventory to construct a Node, since Nodes contain Inventorys. The extra line tells the compiler that there is a Node class, but not anything about what it is; this way you can use pointers to Nodes (or anything else which doesn’t require knowing the layout of a Node), but can’t do anything else with them until they’re fully defined. Since Inventory only uses pointers, this shouldn’t be a problem.


However, as described I don’t see why Inventory needs the node pointers at all; it seems like your Inventory class is what you called a product in your English description, and a product shouldn’t need to know about the rest of the data. You also might want to just use std::forward_list instead of trying to implement your own linked list type.

Daniel H
  • 7,223
  • 2
  • 26
  • 41