error in my GENERIC STACK IMPLEMENT



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef ISTACK_H
#define ISTACK_H
#include<iostream>
using namespace std;
template<typename S>
class Stack
{
      
      public:
      Stack(const int);
        bool  push(S);
        bool pop();
        bool isFull();
        bool isEmpty();
        int getLength();
        int getIndex();
      private:
             S *stackPtr;
             int top; 
             S nElement; 
            
        
      };
template<typename S>
int Stack<S>::getLength(){
    return top+1;
}
template<typename S>
int Stack<S>::getIndex(){return top;}
template<typename S>
Stack<S>::Stack(const int nsize):nElement(nsize),top(-1),stackPtr(new int[nsize])
 {
            
 }

 


template<typename S>
 bool Stack<S>::isFull()
   {
     if(top==nElement-1)
        {
        return  true;
        }
       return  false;               
   }
   
   template<typename S>
 bool Stack<S>::push( S pushValue){
                     if(!isFull())
                        {
                        stackPtr[++top]=pushValue;
                        
                        return true;
                        }
                        return false;
                     }//end push
   template<typename S>                  
  bool Stack<S>::pop()
    {
    if(!isEmpty())
       {
                  
        cout<<stackPtr[top--];
       
         return true;       
       }
   
        return false;
        
}

template<typename S>
bool Stack<S>::isEmpty()
  {
      if(top==-1)
         {
           return true;
         }    
       return false;           
}
#endif 
when i run it display error

C:\Users\cpluscplsu\Desktop\C++ PROJECTS\stackobj.h In constructor `Stack<S>::Stack(int) [with S = double]':
6 C:\Users\cpluscplsu\Desktop\C++ PROJECTS\main.cpp instantiated from here
34 C:\Users\cpluscplsu\Desktop\C++ PROJECTS\stackobj.h cannot convert `int*' to `double*' in initialization
C:\Users\cpluscplsu\Desktop\C++ PROJECTS\Makefile.win [Build Error] [main.o] Error 1

hope to here from my helper thanks
It seems that in your code you are trying to instantiate Stack with template argument of type double. For example

Stack<double> s( 10 );

However in your constructor definition you allocate memory for pointer to int which may not be directly assigned to pointer to double

Stack<S>::Stack(const int nsize):nElement(nsize),top(-1),stackPtr(new int[nsize])
{

}

that is stackPtr has type double * while you are trying to initialize it with pointer to int new int[nsize]

You have to change the constructor the following way

Stack<S>::Stack(const int nsize):nElement(nsize),top(-1),stackPtr(new T[nsize])
{

}
Last edited on
Thanks for the help omg i was just confused that you very much.
You defined an object of type Stack<student>. This means that the template argument has type student. Member function push is declared as

bool push(S);

that is it accepts an argument of type student. However you call it passing an argument of type student *

varD.push(d);

d has type student *

So either you should define the Stack as having template argument as student * or use an object of type student instead of student * as the argument in the call

varD.push(d);
Last edited on
Thanks
Now working fine for those who may have similiar problem


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

#ifndef ISTACK_H
#define ISTACK_H
#include<iostream>
using namespace std;
template<typename S>
class Stack
{

public:
Stack(int);
bool push(S&);
bool pop();
bool isFull();
bool isEmpty();
int getLength();
int getIndex();
S getItem();
private:
S *stackPtr;
int top;
int nElement;
S Item;


};
template<typename S>
int Stack<S>::getLength(){
return top+1;
}
template<typename S>
int Stack<S>::getIndex(){return top;}
template<typename S>
Stack<S>::Stack(int nsize):nElement(nsize),top(-1),stackPtr(new S[nsize])
{

}
template<typename S>
S Stack<S>::getItem(){
return Item;
}


template<typename S>
bool Stack<S>::isFull()
{
if(top==nElement-1)
{
return true;
}
return false;
}

template<typename S>
bool Stack<S>::push( S &pushValue){
if(!isFull())
{
stackPtr[++top]=pushValue;

return true;
}
return false;
}//end push
template<typename S>
bool Stack<S>::pop()
{
if(!isEmpty())
{

Item=stackPtr[top--];

return true;
}

return false;

}

template<typename S>
bool Stack<S>::isEmpty()
{
if(top==-1)
{
return true;
}
return false;
}
#endif




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
#include"iostream"
#include "stackobj.h"
#include <string>
using namespace std;

struct student
{
int age;
char* name;
};
int main()
{
Stack<student> varD(3);
student d,c;
d.age=12;
d.name="Johnson Obaro";
varD.push(d);
varD.pop();

c=varD.getItem();
cout<<c.name;

cout<<"\n";
system("pause");
return 0;
}

Topic archived. No new replies allowed.