template

I have a stack and I have to put a value in it, but I do not understand where I'm wrong.

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

#define max 1000

using std::cout;
using std::endl;

template <typename T>
class stack
{
    public:
        stack(){vf=0;};
        void push(T);
        int pop();
        void print();
    protected:
    private:
        T v[max];
        int vf;
};


-------------------stack.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stack.h"
template<typename T>
void stack<T> :: push ( T x)
{
    if(vf==max) throw ("stack full");
    v[vf++]=x;
}
template<typename T>
int stack<T> :: pop ()
{
    if( !vf ) throw ("stack empty");
    return vf--;
}
template<typename T>
void stack<T> :: print ()
{
    int d=vf;
    while(d)
        cout<<v[d--]<<" ";
    cout<<endl;
}

----------------------------------main:
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
#include <iostream>
#include "stiva.h"
#include<string>
#include <fstream>
#include<cstdlib>

#define smax 1000

using namespace std;

ifstream fin("s1.in");

template<typename T> class stack;

int main()
{
    string s[smax],f,nr;
    stack<int> st;
    int i,n,poz,c;
    fin>>n;
    for(i=1;i<=n;i++)
        getline(fin,s[i],'\n');
    for(i=1;i<=n;i++)
    {
        poz=s[i].find(" ");
        f=s[i].substr(0,poz);
        if(f=="PUSH")
        {
                nr=s[i].substr(poz+1);
                c=atoi(nr.data());
                st.push()<int> st(c);// <-error here:`enter code here`
            //error: no matching function for call to 'stack<int>::push()'
            //note: candidate is:
            //note: void stack<T>::push(T) [with T=int]
            //note: candidate expests 1 argument, 0 provided
            //error: expected primary-expression before 'int'
        }
    }
    fin.close();
    return 0;
}

--------------------------------s1.in:
4
PUSH 5
PUSH 3
PUSH 4
PUSH 10

Last edited on
st.push()<int> st(c);// <-error here:`enter code here`
needs to be
st.push(c);
Hello anonim,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.


First "#include <iostream>" should not be in the or any header file.


".cpp" files should start with the "#include" statements.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space "std::" and then to learn what is in the "std::" name space now while it is easy.

In your "#define", "const" or a "constexpr" it is better to use capital letters to let you know it has a constant value and can not be changed.

I like to define the ifstream near where it is use, in the beginning of main or the function it is used in. This way you do not have input or output streams hanging around where they are not needed. Although there is nothing wrong with what you have done it is just a suggestion.

Hope that helps,

Andy
Topic archived. No new replies allowed.