I really do not know what is wrong with this function

This is function is exactly as it is in the book of DS Malik & Savitch but I get this error:
1
2
3
:\Martin\MalikChapter7\std_libcmpt225LinkedStack.cpp:87:18: error: qualified-id in declaration before '(' token
 bool Stack::empty() const


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>
#include<string>
#include<stack>
#include<cstddef>

using namespace std;
typedef string ItemType;

class Stack {
public:
	// Creates an empty stack.
	Stack();

	bool empty() const;   // declared like this in the text book
//defined like this:
bool Stack::empty() const
{
    return (head == NULL);
}
The closing }; of the class is missing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>
#include<string>
#include<stack>
#include<cstddef>

using namespace std;
typedef string ItemType;

class Stack {
public:
	// Creates an empty stack.
	Stack();

	bool empty() const;   // declared like this in the text book
}; // Note
//defined like this:
bool Stack::empty() const // This function should be in .cpp file
{
    return (head == NULL);
}
You may define it as inline:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>
#include<string>
#include<stack>
#include<cstddef>

using namespace std;
typedef string ItemType;

class Stack {
public:
	// Creates an empty stack.
	Stack();

	//bool empty() const;   // declared like this in the text book
//defined like this:
bool Stack::empty() const
{
    return (head == NULL);
}
}; // Note 
Thanks coder777 the empty function has been sorted out.
1
2
3
4
    bool Stack::empty()
    {
            return ( head == NULL);
     } 


Now I am battling with the push() function.
1
2
3
4
5
6
7
8
9
10
void push(const ItemType &x)
{   
     ItemType *newNode;
     newNode = new ItemType;
     assert(newNode != NULL);
     newNode-> info = x;
     newNode->next = head;
     head = newNode;
} // end push 
     


I get these errors:
1
2
3
4
    ItemType has no member named 'info'
    ItemType has no member named 'next'
    head was not named in this scope
Those should be self-explanatory. You've defined newNode to be a pointer to an ItemType, and you've defined ItemType to be std::string.

std::string doesn't have members called next or info. In fact, nothing in any code you've shown us has members with those names.

Were you supposed to implement some class that has those members? A class that represents some kind of node, maybe?

Also, nothing in any code you've shown us defines anything called head.
Last edited on
Topic archived. No new replies allowed.