How do I make a Instance of a class in a seperate class if it needs to be prototyped

as an example of what I want it to do:
1
2
3
4
5
6
7
8
9
10
11
class Part{
 private
 public
  Part(int num);
};

class PartContainer{
 private
  Part parta(45);
 public
};


basically, I don't know how to make the instance parta, since it wont let me give it 45 when I make it since its in a class.. if I make it in the class function, it complains "‘parta’ was not declared in this scope" when I try to use it in the class
Initialization of members is done in the initialization list:
1
2
3
4
5
6
7
class PartContainer
{
  public:
    PartContainer() : parta(45) {}
  private:
    Part parta;
};
Last edited on
I tried that, but when I prototype it, it says that "candidate expects 1 arguments, 0 provided" referring to the prototype of parta, since I did not pass it the integer
Last edited on
I tried that
since I did not pass it the integer

Which one? Did you or did you not?

You need to do it in all constructors of PartContainer if Part has no default constructor.
maybe I'm not understanding what you meant by
"PartContainer() : parta(45) {}"
, I use a little bit different syntax, so I assumed you wanted me to pass parta to the initializer, and declare it? but it seems to think that the prototyping of parta is me declaring it >.<
Line 9 tries to initialize a Part object, rather than declare one. Initialization is done inside the class constructor, not inside the class declaration part.

Hint: If you keep (forward-)declarations and actual definitions separate (like you seem to have done), you should not see any numbers in the declaration area.
I know, but when I try to initialize parta, it thinks im declaring it, and errors me that class Part expected an integer. instead of waiting for me to declare it later.

Im stuck, so if you want to see my actual code, ill give it >.<

http://s000.tinyupload.com/?file_id=19643404033141378459
Last edited on
It shouldn't expect an integer. Did you remove the ()?
Last edited on
the .h code is now:

1
2
3
4
5
6
7
8
9
10
11
12
class Part{
 private
 public
  Part(int num);
};

class PartContainer{
 private
  Part parta;
 public
  PartContainer();
};


and the .cpp linked to the .h is now

1
2
3
4
5
6
7
Part::Part(int num){
 //do something
};

PartContainer::PartContainer(){
 Part parta(45);
};


the errors:
.cpp:(line of class Part) candidate expects 1 arguments, 0 provided
.h:(line of Part's function) candidate expects 1 argument, 0 provided
Last edited on
*sigh*
Read the linked chapter excerpt.
Where at? iv been reading from a book, and it dident shed much light on how to do this XD
Last edited on
It looks like what I wanted to do.. but I don't understand ": size(sz) {}" can I get an explanation?
Topic archived. No new replies allowed.