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

Oct 19, 2011 at 12:43pm
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
Oct 19, 2011 at 12:52pm
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 Oct 19, 2011 at 1:23pm
Oct 19, 2011 at 12:57pm
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 Oct 19, 2011 at 12:58pm
Oct 19, 2011 at 1:03pm
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.
Oct 19, 2011 at 1:09pm
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 >.<
Oct 19, 2011 at 1:26pm
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.
Oct 19, 2011 at 1:41pm
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 Oct 19, 2011 at 1:47pm
Oct 19, 2011 at 2:08pm
It shouldn't expect an integer. Did you remove the ()?
Oct 19, 2011 at 2:13pm
Last edited on Oct 19, 2011 at 2:14pm
Oct 19, 2011 at 2:20pm
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 Oct 19, 2011 at 2:21pm
Oct 19, 2011 at 2:21pm
*sigh*
Read the linked chapter excerpt.
Oct 19, 2011 at 2:22pm
Where at? iv been reading from a book, and it dident shed much light on how to do this XD
Oct 19, 2011 at 2:24pm
Last edited on Oct 19, 2011 at 2:25pm
Oct 19, 2011 at 2:33pm
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.