vectors and classes in a constructor

In the following program, the class 'Card' has a vector of Class1's, and an object of Class1 inside of it. In Card's constructor, I've been able to assign the other variables easily, but have run into errors whenever trying to construct class1_object and class1_vector. I feel like there is probably some sort of method for doing this, and am hoping that somebody here may have the answer I'm looking for:

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
#include <cstdlib>
#include <iostream>

#include <vector>
#include <string>

using namespace std;


class Class1
{
     int var01;
}

class Card
{
      public:
           
           string suit;
           int number;
           
           Class1 class1_object
           
           vector<Class1*> class1_vector; 
           
           //Constructor
           Card( string sui = "", int num = 0 )
           {
            suit = sui;
            number = num;  
            }
};

//... 
Class1 is unusable in the code you posted. The only things you can do with it are construct it with no arguments and copy it around. If you make var01 public, you can assign to it like this:

1
2
Class1 c;
c.var01 = 5;


You really should write a constructor for Class1 as well. Right now all you can do is declare it as above with no arguments.
SheerSt,

Did you mean like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
Card( std::string sui = "", int num = 0 )
{
	suit = sui;
	number = num;
	for (auto i = 0; i < 10; ++i)
		class1_vector.at(i) = new Class1; // fill with 10 Class1 objects
}

~Card()
{
	for (short i = 0; i < 10; ++i)
		delete class1_vector.at(i);
}


As for Class1 object inside the class Card the object is constructed for you for free, no aditional action is required according to your Class1 declaration.

If you can be more specific as what is you goal here maybe someone can help u.
I think I know exatly what the problem is! try adding a semicolon(';') to the end of class Class1! And ofcourse do what telion said, it's true!
Last edited on
Topic archived. No new replies allowed.