need help w/ syntax of member class plz!

This is a discussion on need help w/ syntax of member class plz! within the C++ Programming forums, part of the General Programming Boards category; Okay, I've got two classes: Class1, and Class2. Class1 has no arguments in its constructor, Class2 has one integer argument. ...

  1. #1
    Unregistered
    Guest

    Question need help w/ syntax of member class plz!

    Okay, I've got two classes: Class1, and Class2. Class1 has no arguments in its constructor, Class2 has one integer argument.

    How can I declare an instance of Class2 within Class1?

    The code for Class2 cannot be modified, because I want it to be completely independent of Class1.

    Heres what I have so far...

    --------------------------------------------------------------
    class Class2
    {
    private:

    ...private members...

    public:

    Class2(int);

    ...public members...
    };

    Class2::Class2(int)
    {
    [-some member integer-] = int;
    }

    class Class1
    {
    private:

    Class2 className(25); //<-----error here

    ...private members...

    oublic:

    ...public members...

    };
    --------------------------------------------------------------

    I get an error on the line of the declaration of Class2.
    ( error C2059: syntax error : 'constant' )

    I had a feeling that the class2 declaration should be this:
    ------> Class2 className(int);
    But then somewhere (class1 constructor?) the class2 declaration would need an integer to create an instance.

    I've been tryin a lot of stuff but i dont know the syntax. If anyone knows the syntax, that'd be great... just remember, the code for class2 is in a headerfile, and should be totally independant of class1.

    -GhostAgent

  2. #2
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    eh.. i believe you are looking for something like this:

    Code:
    class class2
    {
     public:
     //blah
     class2(int var);
     private:
     //blah
    };
    
    class class1
    {
     public:
     anObject = class2(/*some number*/);
     //blah
     private:
     //blah
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21