This might be really simple or impossible, but what I need to do is somehow take what the user inputs in the client code, and set one of my variables in my constructor to the variable in the client code.

This is my client code

int main()
{

        StackType<int> stack;

        int num, item, valid =1, size;

        while (valid != 0)
        {
                cout << endl << "How many integers will you input: ";
                cin >> stack.StackType(size);

                if (size <= MAX_SIZE)
                {
                        valid = 0;
                }
                else
                {
                        cout << endl << "Not a valid input, stack can only hold up to 10 integers" << endl;
                        valid = 1;
                }
                cout << endl;
        }

This is my class coding

#ifndef STACK_H
#define STACK_H
using namespace std;
#include <iostream>

const int MAX_SIZE = 10;

class FullStack
{};

class EmptyStack
{};

template <class ItemType>
class StackType
{
        public:
                StackType(ItemType s);
                ~StackType();
                StackType(const StackType<ItemType> &right);
                void Clear();
                bool IsFull() const;
                bool IsEmpty() const;
                void Push(ItemType item);
                ItemType Pop();
                ItemType Top();
                void Print();
                StackType<ItemType>& operator=(const StackType<ItemType> &right);

        private:
                ItemType *items;
                int stackSize;
                int top;
};

#endif

// Stack.cpp
template <class ItemType>
StackType<ItemType>::StackType(ItemType s)
{
        top = -1;
        stackSize = s;
        items = new int[stackSize];
}

As you can see I need to set variable stackSize to what was input in the main program... any help or suggestions?

Recommended Answers

All 5 Replies

line 11 of main() is definitely wrong. YOu can't use cin like that. Not sure what you need to do there other than cin >> size;

when I do that, just use cin>> size, I get this error

prog4.cpp: In function `int main()':
prog4.cpp:8: error: no matching function for call to `StackType<int>::StackType()'
Stack.h:77: note: candidates are: StackType<ItemType>::StackType(const StackType<ItemType>&) [with ItemType = int]
Stack.h:42: note:                 StackType<ItemType>::StackType(ItemType) [with ItemType = int]

If I need to put whole Stack.h code let me know

size is declared as an int on line 6 so the error you posted can not be the result of cin >> size; . Repost the code where that error occured.

template <class ItemType>
StackType<ItemType>::StackType(ItemType s) // line 42
{
        top = -1;
        stackSize = s;
        items = new int[stackSize];
}

Stack.h:42: note: StackType<ItemType>:: StackType(ItemType) [with ItemType = int]

template <class ItemType>
StackType<ItemType>::StackType(const StackType<ItemType> &right) // line 77
{
        stackSize = right.stackSize;
        top = right.top;
        items = new int [stackSize];

        for (int x = 0; x < stackSize; x++)
        {
                items[x] = right.items[x];
        }
}

Stack.h:77: note: candidates are: StackType<ItemType>:: StackType(const StackType<ItemType>&) [with ItemType = int]


CLIENT CODE
StackType<int> stack; // line 8
prog4.cpp:8: error: no matching function for call to `StackType<int>:: StackType()'

Here is a tutorial that is almost like what you are trying to do. Study this tutorial and you will probably see your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.