push back data in a vector inside a class

Sep 4, 2012 at 12:30am
I have to use a vector of a class and inside that class I have a vector of another class.! I came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class package
{
    public:

    class aservice
    {
       public:
       int serviceID;
       string othername;
    };
    int ID;
    string name;
    vector<aservice> bservice;

    void putData(string name1, int serviceID1)
    {
        bservice.push_back(aservice(serviceID1,name1));
    };

};


I want to call the putData from the main file, so when I have the data I can put it inside the vector of "bservice". I don't know how can I define the void putData correctly. I am getting an error of:

error: no matching function for call to 'diagnosis::aservice::aservice(int&, std::string&)'|
Last edited on Sep 4, 2012 at 12:32am
Sep 4, 2012 at 12:36am
It thinks that the aservice(serviceID1,name1) is a function that hasn't been defined.

What I would do is write
1
2
bservice.push_back(aservice.serviceID1);
bservice.push_back(aservice.name1);
though I don't believe ServiceID1 and name1 are parts of the class aservice.

1
2
3
4
5
#include<brain>
#define Poster_Needs_Help true
if(Poster_Needs_Help)
   cout<<"Help me please!"<<endl;
return fail;
Sep 4, 2012 at 12:54am
The "aservice" is a class that I defined it inside the main class. I tried to push_back the data one at a time, but I think because it is a vector, it didn't work.
Sep 4, 2012 at 12:56am
Right about aservice being a class. Why don't you make it into a string and make everything happier again?
Sep 4, 2012 at 1:13am
What I am trying to do is to read the records from a file and then assign the relevant data to each part of the package. I have a lots of records that belongs to one package (i have different packages) and the basic info would not change (like ID and name). But there are services that I want to grab them from the records and put them into that vector (bservices). The service has different information inside it like name of service, service ID, and date of service. So I cannot store all of the data in a string, I have to do some processes on that vector.
Last edited on Sep 4, 2012 at 1:15am
Sep 4, 2012 at 1:41am
If you want to be able to create aservice objects by doing aservice(serviceID1,name1) you have to define a aservice constructor that takes an int as first argument and std::string as second argument.

In C++11 you can create the aservice object by doing aservice{serviceID1,name1} without having a constructor.

Another way is to first create the aservice object and then set the data members before calling push_back.
1
2
3
4
aservice obj;
obj.serviceID = serviceID1;
obj.othername = name1;
bservice.push_back(obj);

Sep 4, 2012 at 2:17am
Thanks. That worked.
Last edited on Sep 4, 2012 at 2:46pm
Topic archived. No new replies allowed.