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
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.
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.
Related
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)
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.
Use std::string, not just string.
I have to write a linked list for a college assignment, I've followed my instruction to a T as far as I can see.
I can't figure out why I'm getting the errors, It has something to do with the pointers declared in my listOfDouble class,
I'v searched through the answers on stack relating to the error message and a few posts suggested it could be an issue with my #include, and that I should be declaring the class instead: class ListOfDoubles;
I've tried replacing my includes to no avail.
I'm stumped on this, bare in mind I am a student, so my mistake could end up being trivial.
Edit: It was trivial.
ListOfDoubles.h
#ifndef LISTOFDOUBLES_H
#define LISTOFDOUBLES_H
class ListOfDoubles{
public:
ListOfDoubles();
~ListOfDoubles();
void insert(double);
void displaylist();
double deleteMostRecent();
private:
DoubleListNodePtr head;
DoubleListNodePtr temp;
DoubleListNodePtr newNode;
};
#endif
DoubleListNode.h
#ifndef DOUBLELISTNODE_H
#define DOUBLELISTNODE_H
class DoubleListNode{
friend class ListOfDoubles;
public:
DoubleListNode(double);
private:
double data;
DoubleListNode *next;
};
typedef DoubleListNode* DoubleListNodePtr;
#endif
Implementation
#include "ListOfDoubles.h"
#include "DoubleListNode.h"
#include<iostream>
using std::cout;
ListOfDoubles::ListOfDoubles()
:head(NULL){
};
DoubleListNode::DoubleListNode(double data)
:data(data){
};
void ListOfDoubles::insert(double data){
newNode = new DoubleListNode(data);
newNode->next = head;
head = newNode;
};
void ListOfDoubles::displaylist(){
DoubleListNodePtr temp = head;
while (temp != NULL){
cout << temp->data;
temp = temp->next;
}
};
double ListOfDoubles::deleteMostRecent(){
};
ListOfDoubles::~ListOfDoubles(){
delete head;
delete temp;
delete newNode
};
main
#include "ListOfDoubles.h"
#include "DoubleListNode.h"
#include<iostream>
int main()
{
ListOfDoubles list1;
list1.insert(25);
list1.displaylist();
system("pause");
return(0);
}
Errors
1> main.cpp
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(13): error C2146: syntax error : missing ';' before identifier 'head'
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(14): error C2146: syntax error : missing ';' before identifier 'temp'
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(15): error C2146: syntax error : missing ';' before identifier 'newNode'
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
You're using DoubleListNodePtr in ListOfDoubles.h while it's defined in DoubleListNode.h, hence the compiler doesn't know what type it is ("missing type specifier"). Include DoubleListNode.h in ListOfDoubles.h.
As mentioned in other answer
1.You need to include DoubleListNode.h in ListOfDoubles.h
2.You are missing ";" on line #34 in implementation file.
delete newNode
should be
delete newNode;
I want to create a list of stacks in C++ but the compiler gives me some error messages:
#include <list>
#include <stack>
class cName
{
[...]
list<stack> list_stack;
[...]
}
Errors:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
std::stack is a template, you need to use it with template arguments. For sample:
class cName
{
typedef int ConcreteType;
std::list<stack<ConcreteType> > list_stack;
^^^^ use it with real type
};
Stacks are also templated, so it should be
list<stack <...> > list_stack;
If you want your stack to handle just one type, for example int, change stack in your code to int:
list<int> list_stack;
Otherwise you should create your own template type instead of using stack:
template <class T>
class List
{
list<T> list_stack;
T top();
void push(T v);
};
This code is not compiling.
What modification can I do to achieve the desired result?
ClassOne.h
#ifndef _CLASS_ONE_
#define _CLASS_ONE_
#include <string>
#include "ClassTwo.h"
class ClassTwo;
class ClassOne
{
private:
string message;
friend ClassTwo;
ClassTwo m_ClassTwo;
public:
ClassOne();
void Display();
};
#endif
ClassTwo.h
#ifndef _CLASS_TWO_
#define _CLASS_TWO_
#include <string>
#include "ClassOne.h"
class ClassOne;
class ClassTwo
{
private:
string message;
friend ClassOne;
ClassOne m_ClassOne;
public:
ClassTwo();
void Display();
};
#endif
ClassOne.cpp
#include "ClassOne.h"
#include "ClassTwo.h"
#include <iostream>
ClassOne :: ClassOne()
{
std::cout<<"ClassOne()...called\n";
this->m_ClassTwo.message = "ClassOne - Message\n";
}
void ClassOne :: Display()
{
std::cout<<this->m_ClassTwo.message;
}
ClassTwo.cpp
#include "ClassTwo.h"
#include "ClassOne.h"
#include <iostream>
ClassTwo :: ClassTwo()
{
std::cout<<"ClassTwo()...called\n";
this->m_ClassOne.message = "ClassTwo - Message\n";
}
void ClassTwo :: Display()
{
std::cout<<this->m_ClassOne.message;
}
main.cpp
#include "ClassOne.h"
#include "ClassTwo.h"
int main()
{
ClassOne one;
one.Display();
ClassTwo two;
two.Display();
}
Error messages
1 error C2146: syntax error : missing ';' before identifier 'message'
2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
4 error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
5 error C2146: syntax error : missing ';' before identifier 'message'
6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8 error C2039: 'message' : is not a member of 'ClassTwo'
9 error C2039: 'message' : is not a member of 'ClassTwo'
10 error C2146: syntax error : missing ';' before identifier 'message'
11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
13 error C2079: 'ClassOne::m_ClassTwo' uses undefined class 'ClassTwo'
14 error C2146: syntax error : missing ';' before identifier 'message'
15 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
17 error C2039: 'message' : is not a member of 'ClassOne'
18 error C2039: 'message' : is not a member of 'ClassOne'
19 error C2146: syntax error : missing ';' before identifier 'message'
20 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
21 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
22 error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
23 error C2146: syntax error : missing ';' before identifier 'message'
24 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
25 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
You cannot, ever, compile that code. You have described a system where type A contains type B, and type B contains type A. Therefore, both type A and B recursively and infinitely contain themselves, which is impossible. You must fundamentally re-architect your code to eliminate this problem.
Also, your friend syntax is wrong.
As said, you need to forward declare at least one of ClassOne or ClassTwo.
You ClassOne.h could therefor look like:
#ifndef _CLASS_ONE_
#define _CLASS_ONE_
#include <string>
class ClassTwo;
class ClassOne
{
private:
string message;
ClassTwo* m_ClassTwo;
public:
ClassOne();
void Display();
};
#endif
As you can see, we declare ClassTwo, but do not include it. We basically only tell the compiler that yes, we do have a ClassTwo, but we don't really care what it contains right now.
Also look at your ClassTwo member, this is now a pointer. The reason is that a member would require the compiler to know the size of the object, which we currently have no clue what is. Therefor you need either a pointer or a ref.
Next, in your ClassOne.cpp, would will need to include ClassTwo.h, to get the functions and size of that class.
A few things though:
With a forward declaration you can not inherit from ClassTwo, use the methods of the forwarded class in the header file (How would the compiler know which methods exists?) Define functions or methods using the forwarded class, that is you have to pass by reference or pass a pointer.
1) Use forward declarations:
add
class ClassTwo;
to ClassOne.h
and
class ClassTwo;
to ClassTwo.h
2) You need to create your member variables (or at least one of them) dynamically, using operator new, or if you wish, with one of the smart-pointers, like boost::shared_ptr from boost library or std::shared_ptr from standard library if you use C++11.
I am working on a college project, where I have to implement a simple Scrabble game.
I have a player class (containing a Score and the player's hand, in the form of a std::string, and a score class (containing a name and numeric (int) score).
One of Player's member-functions is Score getScore(), which returns a Score object for that player. However, I get the following error on compile time:
player.h(27) : error C2146: syntax error : missing ';' before identifier 'getScore'
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(27) : warning C4183: 'getScore': missing return type; assumed to be a member function returning 'int'
player.h(35) : error C2146: syntax error : missing ';' before identifier '_score'
player.h(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
player.h(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Here's lines 27 and 35, respectively:
Score getScore(); //defined as public
(...)
Score _score; //defined as private
I get that the compiler is having trouble recognizing Score as a valid type... But why? I have correctly included Score.h at the beginning of player.h:
#include "Score.h"
#include "Deck.h"
#include <string>
I have a default constructor for Score defined in Score.h:
Score(); //score.h
//score.cpp
Score::Score()
{
_name = "";
_points = 0;
}
Any input would be appreciated!
Thanks for your time,
Francisco
EDIT:
As requested, score.h and player.h:
http://pastebin.com/3JzXP36i
http://pastebin.com/y7sGVZ4A
You've got a circular inclusion problem - relatively easy to fix in this case.
Remove the #include "Player.h" from Score.h.
See this question for an explanation and discussion of what you'd need to do if Score actually used Player.
As for the compiler errors you're getting, that's how Microsoft's compiler reports the use of undefined types -you should learn to mentally translate them all into "Type used in declaration not defined".
Your problem is the recursive include: Score.h is trying to include Player.h, and Player.h is trying to include Score.h. Since the Score class doesn't seem to actually be using the Player class, removing #include "Player.h" from Score.h should solve your problem.
You have a circular dependency problem : Score.h includes Player.h and Player.h includes Score.h.
Do solve this problem, delete your include to Player.h in Score.h and define class Player; if you need to use it in Score class.