can get int but not string

hello everybody,

in my program i have a class witch take two parameter before for intialization for stuff(string name, int id) and when thrying to print the string name and int id of my class i m priting only the int without string.

here is my code please help:

here in my stuffList.cc
// function which take stuff item as parameter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void stuffList::addMembers(stuff item)
{
   elementList.push_back(item);
}

//fucntion to print stuffProperties
void stuffList::printProperties()
{
   vector<stuff>::iterator put;

   for ( put = elementList.begin(); put!= elementList.end(); ++put)
   {
      cout<<(*put).getStuffId()<< endl ;
      cout << (*put).getStuffName() << endl;
   }

}


in my main i have this :

1
2
3
4
5
6
7
8
9
10
11
12
   stuff fs(dad,34);// intialization of staff
   stuff f2(mam,21);
   stuff f3(onkle,12);
   stuff f4(tante, 24);

   stuffList staffList;
   staffList.addMembers(fs);// inserting in the list
   staffList.addMembers(f2);
   staffList.addMembers( f3);
   staffList.addMembers(f4);

   staffList.printProperties();// printing the List 


and here are my getters

1
2
3
4
5
6
7
8
9
 int stuff::getStuffId()
{
   return stuffId;
}

string stuff::getStuffName()
{
   return stuffName;
}

why i can get int but not string?
i dont know why i cannot print the stuffName. please help
thank in advance
the printing looks ok

this part is a bit unclear:

1
2
3
4
stuff fs(dad,34);// intialization of staff
   stuff f2(mam,21);
   stuff f3(onkle,12);
   stuff f4(tante, 24);


could u show how your class stuff looks like, the whole shebang? especially where u initialize 'dad','mam' , etc.

Last edited on
yes i can

here is my stuff.hh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STUFF_H
#define STUFF_H
#include<string>

using namespace std;

class stuff{

      int stuffId;
      string stuffName;
   public:
      stuff(string stuffName,int stuffId);//constructor
      stuff();
      string getStuffName();
      int getStuffId();
      void printProp();

};
#endif 


and here is the implementation :

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>
#include<string>
#include"stuff.hh"

using namespace std;

stuff::stuff ()
{

}

stuff::stuff(string stuffNamefp,int stuffIdfp)
{
   stuffName = stuffNamefp;
   stuffId = stuffIdfp;
}
 int stuff::getStuffId()
{
   return stuffId;
}

string stuff::getStuffName()
{
   return stuffName;
}
void stuff::printProp ()
{
   cout << stuffId << " and " << stuffName << endl;
}
 


thanks for showing interest
So what are the values of the variables dad, mam, tante, and onkle?
You don't show their declarations or initializations.

As a matter of good programming technique:

1) Never ever use namespaces in header files;
2) getStuffName(), getStuffId(), and printProp() should be const
member functions;
3) Pass strings by const reference instead of by value.


what do you means by the value of the variables do you mean this :

1
2
3
4
5
6
7
8
9
   string dad;
   string mam;
   string onkle;
   string tante;

   stuff fs(dad,34);
   stuff f2(mam,21);
   stuff f3(tante,12);
   stuff f4(onkle, 24);


i m posting this theard and i will go to retouch my codes
thanks for advice
Yes. The strings dad, mam, onkle, tante are all default constructed to an empty string, so they are in fact being printed.

You probably want

1
2
3
string dad( "dad" );
string mam( "mam" );
// etc. 

Topic archived. No new replies allowed.