Reputation: 127
I am working on a project that requires a list(singly) of stacks. I have made a class called "stacklist" that is just that, a list of stacks. I want the class to have a stack pointer that will point to the head of the list. However, I keep getting an error whenever I try to build the class.
#ifndef STACKLIST_H
#define STACKLIST_H
#include <stack>
//source: Data Structures Using C++: Linked List Implementation Part II (List Class)
//url: http://www.youtube.com/watch?feature=endscreen&NR=1&v=7vZo17iv1zQ
class stacklist
{
public:
stacklist();
void addToHead(const int&);
void printList();
void insert(const int&);
private:
stack<int>* head; //THIS IS WHERE I AM HAVING THE PROBLEM
int size;
};
#endif // STACKLIST_H
The error message is "error: expected ';' before '<' token"
Any insight that can be provided would be highly appreciated.
Upvotes: 0
Views: 493
Reputation: 13320
If you're using std
libraries, you must put std::
before the std object or place a using namespace std
at the beggining of your source file:
#include <stack>
using namespace std; // This line brings std stuff into the current namespace.
Or:
std::stack<int>* head;
If your file is a header, it's better the first one than the second one because the using
directive will be spreaded into all the files that includes StackList.h
This is no mentioned on the question, but I think is worth to say: Is quite dangerous to use pointers into classes, because you must take care of it into all constructors (default and copy) and take care on destructor too.
Revise your class dessign and think if you can use a std::stack
instance instead of std::stack
pointer.
Upvotes: 0
Reputation: 227370
The standard library stack
lives in the std
namespace, so you need to qualify the name appropriately:
std::stack<int>* head;
You should consider using an std::list<std::stack<int>>
instead of your class.
Upvotes: 6
Reputation: 3451
Perhaps you should say std::stack
std::stack<int>* head;
instead stack<int>* head;
Upvotes: 1
Reputation: 476930
The correct name of the template is std::stack
.
If all you want is a list of stacks, you could always use std::list<std::stack<int>>
, by the way (or std::forward_list
if you only need forward traversal). But even if you pursued your own design, there's probably no reason why you can't make the stack a direct class member.
Upvotes: 5