Initializing Constructors

(solved)
Last edited on
in the second constructor Club::Club(Club &c)
you did not allocate memory for members
(clear)
Last edited on
when you have an idea compile it and if you find problems ask.
What do you think ?
(clear)
Last edited on
How about this:

1
2
3
4
5
6
7
8
Club::Club(Club &c)
{
      numMembers = c.numMembers;
      clubName = c.clubName;

      members = new string [numMembers];
      members = c.members;
}



Consider this:

1
2
    int n = 6 ;
    n = 7 ; 


What happens to the initial value of n on line 2?
Apply that to what you've done with your members variable in your code.
Last edited on
there's problem with members

1
2
3
4
5
6
Club::Club()
{
    clubName = "";
    numMembers = 0;
	members = new string [numMembers];
}


means you are going to allocate and array of 0 strings
members = new string [0];

for the second constructor

1
2
3
4
5
6
7
8
Club::Club(Club &c)
{
      numMembers = c.numMembers;
      clubName = c.clubName;

      members = new string [numMembers];
      members = c.members;
}



members is an arrays, it should be like this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Club::Club(Club &c)
{
	numMembers = c.numMembers;
	clubName = c.clubName;

	members = new string [numMembers];

	for (int i = 0; i < numMembers; i++)
	{
		members[i] = c.members[i];
	}

	clubName = c.clubName;
}



you need to think again of the design of the class!
So I should not initialize numMembers to zero?
I tried 6, just as a test, and I still have the same runtime error.
Last edited on
I compiled and run your code. There's no runtime error;
the problem is somewhere else.
Last edited on
I compiled and run your code. There's no runtime error;


How did you compile and run Cynthia401's code when there is nothing to run?
No, the runtime error occurs in the ACTUAL code that all of this must be pasted into.
I realize that there is STILL something wrong with my copy constructor, so I still need help.
closed account (j3Rz8vqX)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Club
{
public:
	Club();
	Club(Club &c);
	Club(string cname);
	Club(string cName, int nMember, string *aMember);//Added
//private: (Lazy@get_accessors@this)
	string *members;
	int numMembers;
	string clubName;
};

//Empty Constructor
Club::Club()
{
    clubName = "";
    numMembers = 0;
    //members = new string [numMembers];    //don't use new, unless necessary =O
}

//Copy Constructor
Club::Club(Club &c)
{
    numMembers = c.numMembers;
    members = c.members;//This works for me
    clubName = c.clubName;
}

//Pre-initialized Constructor
Club::Club(string cName, int nMember, string *aMember){
    clubName = cName;
    numMembers = nMember;
    members = aMember;//This works for me
}

//A simple function to test the data of an object type-Club
void print(const Club &a){
    cout<<"Club Name: "<<a.clubName<<endl;
    cout<<"Number of Members: "<<a.numMembers<<endl;
    for(int i = 0; i < a.numMembers; i++){
        cout<<"Member #"<<i<<": "<<a.members[i]<<endl;
    }
}

int main()
{
    string clubName="The only club";
    int numberOfMembers = 4;
    string members[] = {"Hydrogen","Oxygen","Nitrogen","Carbon"};
    Club c(clubName,numberOfMembers,members);
    Club d(c);
    print(c);
    print(d);
    return 0;
}


The above worked for me.
Topic archived. No new replies allowed.