terminate called after throwing instance of std bad_alloc

I cant seem to find the error with my code.... I dont think im using too much memory.

Here is my class declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  class course
{
private:
    string name;
    int maxStudents;
    int numStudents;
    string * names;
public:
    course();
    course(string fileName);
    course(string className, int maxNum, int actualNum, vector<string> students);
    course(const course & object);
    friend ostream & operator<< (ostream & ostr, const course & object);
    void operator+= (const string newName);
    void operator-= (const string dropName);
    course operator+ (const course& object);
    course operator= (const course& sum);
};


Here is my deep copying constructor:

1
2
3
4
5
6
course::course(const course & object)
{

    names = new string [object.maxStudents];
    names=object.names;
}
I don't think it's the source of your problems, but your copy constructor is not correct and leaks memory. If your class had a destructor like it ought to, you'd be deleting the same memory twice which would give you another problem to deal with.

You use vector in the parameters for one of your constructors. Why are you not using it within the class instead of trying to manually manage your memory?
i am supposed to take all the elements of the vector and copy it into a array (which is in the course class)... here is my destructor ( i just added it):

1
2
3
4
~course()
    {
         delete[] names;
    }
Last edited on
Your "deep copying constructor:" is not doing a deep copy!?

Andy
yeah thanks. i got it now.
Topic archived. No new replies allowed.