How should this be set up?

I am building a stack class on top of my list class. I can't do it to save my life. Here is my list class, so can someone show me how to combine the two. And also, what is the point of doing so, if I can't access my list methods from the stack objects?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Class declarations
class list
{
public:
    // Typedefs and member constants
    typedef char value_type;
    typedef std::size_t size_type;
    static const size_type CAPACITY = 1000;

    // Constructor functions
    list();
    list(list&);

    // Modification member functions
    void insertBefore(value_type);
    void insertAfter(value_type);
    void replace(value_type);
    void erase();
    void clear();
    void reverse();
    void swap(list& swapList);

    // Constant member functions
    bool is_empty();
    void front();
    void end();
    void prev();
    void next();
    int getPos();
    void setPos(int);
    value_type getElement();
    size_type size();

    // Friend functions
    friend ostream& operator <<(ostream& outs, list& a);
    friend void operator +(list a, list b);

  private:
    value_type myList[CAPACITY];// Array to hold list
    size_type mySize;           // Keeps track of how large the array is
    size_type pos;              // Current position within array
};


Here is the stack definition that I currently have, but it doesn't work. I keep getting a ton of errors that say "reference to stack is ambiguous."


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class stack
{
public:
    // Typedefs and member constants
    typedef size_t size_type;
    typedef char value_type;
    static const size_type CAPACITY = 1000;

    // Constructors
	stack();                    // Starts a new empty stack}
	stack(stack& stackToCopy);  // Copies a stack when declaring new stack

	// Modification member functions
	void push(const value_type& entry);
	void pop();

	// Constant member functions
	value_type top() const;     // Returns top value in array
	size_type size() const;     // Returns size of array
	bool is_empty() const;      // Checks to see if array is empty

private:
	list myStack;	            // Partially filled array
};


From here, what do I do? How do I use both in a program?
You can consider interfaces of std::stack and std:: list.
I think you incorrectly defined for example member function of the list front. It shall return a reference to the first element of the list.

For example yoiu could declare it the following way

value_type & front();
const value_type & front() const;

Now the member function top() of the stack can be defined as

1
2
3
4
5
6
7
8
9
value_type & top()
{
   return ( myStack.front() );
}

const value type & top() const
{
   return ( myStack.front() );
}
Last edited on
Thank you so much for responding! I will try that, thanks so much. Can you tell me why these function definitions are giving me a "reference to stack is ambiguous" error? What am I doing wrong? (I've since deleted all the code within them to start over, but still get the errors, so problem must be in the definitions themselves, I just don't understand what I did wrong. It's exactly how I've done them before...)
size is the only one that doesn't give that error. But it does say 'size_type' does not name a type. I don't understand that either, since I did a typedef for it...
So frustrated!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// *************************************************************************
stack::stack()
{

}

// *************************************************************************
stack::stack(stack& stackToCopy)
{

}
// *************************************************************************
void stack::push(const value_type& entry)
{
    if (size() < CAPACITY)
    {

    }
}

// *************************************************************************
void stack::pop()
{
    if (size() != 0)
    {

    }
}

// *************************************************************************
list stack::top()
{
    if (size() != 0)
    {

    }
}

// *************************************************************************
size_type stack::size()
{

}

// *************************************************************************
bool stack::is_empty()
{

}
Topic archived. No new replies allowed.