0
class A
{
public:
    string  name;

    float   length;

    float   weight;

    bool    isEnabled;
};

When I'm doing push_back() on the vector of this class, it works for the first time, but doesn't on the subsequent push_back() calls. Could it be because of the string member? If so, why?

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79

2 Answers2

1

I tried out your code.

vector<Aclass>  aClass;
aClass.push_back(Aclass("John Doe ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe2 ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe3 ", 6.2 , 220  , true) );
aClass.push_back(Aclass("John Doe4 ", 6.2 , 220  , true) );

But could not replicate your error.

This code pushes back four times.

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Aclass
{
public:
    string  name;
    double   length;
    double   weight;
    bool    isEnabled;

    Aclass(){}
    ~Aclass(){}

    Aclass(string  _name, double   _length, double   _weight, bool    _isEnabled )  //constructor
    {
        name = _name;
        length = _length;
        weight = _weight;
        isEnabled = _isEnabled;

        cout<<" \n";
        cout<<"Name   : "<<name<<" \n";
        cout<<"length : "<<length<<" \n";
        cout<<"weight : "<<weight<<" \n";
        cout<<" \n";
    }



};



int main()
{ 

    vector<Aclass>  aClass;
    aClass.push_back(Aclass("John Doe ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe2 ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe3 ", 6.2 , 220  , true) );
    aClass.push_back(Aclass("John Doe4 ", 6.2 , 220  , true) );

    cout<<" \n";


return 0;
}

Output :

Name   : John Doe
length : 6.2
weight : 220


Name   : John Doe2
length : 6.2
weight : 220


Name   : John Doe3
length : 6.2
weight : 220


Name   : John Doe4
length : 6.2
weight : 220


Press any key to continue
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
-3

Why would the string have anything to do with it ?

Have you done #include <string> ? You haven't mentioned whether your class has a copy constructor. It goes something like this:

class A {
public:
        A() //default constructor
        A(const A&) //copy constructor
};

By default, c++ will copy all public members, but you need to have a copy constructor in order to use STL containers and push_back()

viraj
  • 1,784
  • 4
  • 35
  • 52
  • 1
    I can only test with MSVC 2005 here, but it works without explicit constructors. – Mr Lister Jan 03 '12 at 14:26
  • Works for me too, but since the OP gives no mention as to the exact error that he's getting, or the platform/compiler that he's using, this seemed the most likely problem. – viraj Jan 03 '12 at 14:30
  • @MrLister it's not only MSVC 2005 it also works in [other compilers](http://ideone.com/ERYYO). That's because the default copy constructor is enough for this class. (I'm assuming std::string obviously) – João Portela Jan 03 '12 at 14:45
  • 2
    Not an answer but just poor guesswork and advice. They way the OP has declared the class, it should work fine with the compiler generated copy constructor – parapura rajkumar Jan 03 '12 at 14:48