add int to string

Hi.I have to write a class with string and static int member,and to add int to string in the default constructor,so int to be an identifier(for class Bird- Bird#1,Bird#2..) .String is the name of the class.I try to use stringstream to add int to string but it doesn't work correct.

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class Bird
{
      string s;
      static int i;
      public:
             Bird(string str="Bird #"){i++;stringstream ss;ss>>i;
                         s=str+ss.str();}
             Bird& operator=(const Bird& ob){if(this==&ob)
                                         return *this;
                                         s=ob.s;
                                         return *this;}
              Bird(const Bird& ob);
              friend ostream& operator<<(ostream& os,const Bird& ob);     
};
 int Bird::i=0;
ostream& operator<<(ostream& os,const Bird& ob){os<<ob.s;
                                            return os;}
int main()
{
    Bird a;
    cout<<a<<endl;
    Bird b;
    cout<<b<<endl;
    Bird c;
    cout<<c<<endl;
    getchar();   
}
You can write to the stringstream the same way you write to cout.
 
ss << str << i;
Š¢hanks.Now it is OK.
Topic archived. No new replies allowed.