Generic Stack to store addresses

Hi there ,
I have tried to store addresses within a generic stack and then pop the addresses on the command prompt .
Here is my attempt :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//user defined datatype
struct addr {
    string name ;
    string streetno ;
    string pincode ;
};

//global function to get address
addr getData(){
    addr obj ;
    cout << "Enter userName , street no and pincode ";
    cin >> obj.name >> obj.streetno >> obj.pincode ;
    return obj ;
}


Here is what I have done within main:
1
2
3
4
5
6
7
8
9
10
11
12
 stack<addr> array4 ; //array of addresses

  //push values on stack 
  array4.push(getData());
  array4.push(getData());
  array4.push(getData());

  //pop values from the array
  cout << "ARRAY OF ADDRESSES " <<  endl ;
  for(int i=0 ; i<3 ; i++){
      cout << array4.pop() << endl ;
  }


I got many errors including "no match for << operator.."
So , I tried to overload the << operator to display data from addr
1
2
3
4
//override the << operator
string operator << (addr obj){
    return obj.name + " " + obj.pincode + " " + obj.streetno ;
}


This stack works for integers , characters , strings but I also want to make it work for user defined datatypes such as structures .
Here is one of the errors :
D:\CPP\GenericClass.cpp|120|error: no match for 'operator<<' in 'std::cout << stack<StackType>::pop() [with StackType = addr]()'|.
Any kind of help will be appreciated ...thanks

The member function pop has return type void. So you have to use member function top to get a value of an element of the stack and only after that to use pop.

1
2
3
4
std::ostream & operator << ( std::ostream &os, const addr &obj )
{
    return ( os << obj.name <<  " " << obj.pincode << " " << obj.streetno );
}


1
2
3
4
5
while ( !array4.empty() )
{
      cout << array4.top() << endl ;
      array4.pop();
}
Last edited on
void stack::pop ( ) if you want the top element, use the top() method.

1
2
//I suppose this is inside addr
string operator << (addr obj)
review operator overloading, that makes no sense.
1
2
addr me, neighbour;
me << neighbour; // returns a strings, using only the status of neighbour 
I think that you want std::ostream& operator<<(std::ostream& out, const addr &obj); //stand alone function
Simply replace every cout<</*anything*/.pop(); with cout<</*that same "anthing" as before*/.top(), /*again that "anything"*/.pop();
I have written a small program to check operator overloading
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
#include<iostream>
using namespace std ;

typedef struct
{
    char name[80] ;
    int age ;
}person;

//overload << operator
ostream & operator << (ostream &out ,const person &obj)
{
    out << obj.name << " " << obj.age ;
    return (out) ;
}

//overload >> operator
istream & operator >> (istream &in , const person &obj){
    in >> obj.name >> obj.age ;
    return in ;
}

int main()
{
    person raman ;
    cout << "Enter person's info ";
    cin >> raman ;
    cout << raman ;
    return 0 ;
}

The >> operator gives error :
no match for operator >> in " in >>obj->person::name"
Any suggestions here ..
You defined the passed object as const, which is not true since you actually want to use the input to change the object.
That's right @expor..thanks
Still I get two errors in the generic stack
Here is the pop() definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//definition of pop
template <class StackType> StackType stack<StackType>::pop()
{
    if(top == -1)
    {
        cout << "Stack is empty";
        return (StackType)0 ;  /*This line gives the error when addresses are popped from stack */
    }
    else
    {
        StackType item = stck[top] ;
        if(top == 0)
        {
            top = -1 ;
        }
        else
        {
            top = top - 1 ;
        }
        return item;
    }
}


I have not defined any top function , it is a variable .
If top = -1 , it means stack is empty
if top = sizeOfArray -1 , it means stack is full.
Also , the error is generated by the code within main by the following line
 
cout << array4.pop() << endl ;


Errors are as follows:
1. no matching function for call to address::address(int)
2. no match for operator in std::cout << stack<StackType>::pop [with StackType = address]()

Any suggestions about that ?

I have 1 word for you: std::stack
The std::stack works like a charm ...thanks @viliml
However , I still don't understand why my generic stack doesn't work.
Your adress class doesn't have a constructor that takes an int parameter, or at least one that takes JUST an int parameter
1
2
3
4
5
struct addr {
    string name ;
    string streetno ;
    string pincode ;
};

No constructor from int, just default, copy and (possibly, if your compiler allows it) move constructors.
Last edited on
Topic archived. No new replies allowed.