Thread: Help On Objects, Classes, and separate Compilation.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    7

    Help On Objects, Classes, and separate Compilation.

    I'm hoping someone here can help me with this situation:

    Code:
    class A
    {
    public:
    A(int a, int b);
    };
    I need to have an object of class A that doesn't have a default constructor in another class, B:

    Code:
    class A; //This is in a separate header file
    
    class B
    {
    private:
    A a;
    };
    The problem is that it won't compile without a default constructor. I'm not allowed to define a default constructor, and the A object in class B has to be private so I can't initialize A a in public.

    I also can't change the prototype in the interface to something like
    A(int a = 0, int b = 0);
    since one of the requirements is that if an object of class A is declared in main, it must not compile due to not having a default constructor.

    So what can I do to make class B work and compile?

    Another question I have is why is this valid:

    Code:
    class A; //#include "A.h" is in the implementation file so it compiles.
    class B
    {
    private:
    A* a;
    };
    But not this:
    Code:
    class A;
    
    class B
    {
    private:
    A a;
    };
    This is for a project that I probably won't be able to turn in on time, but I care more about how to do this right than turning it in for full points. I'll be thankful for any help given.
    Last edited by Rex Imperator; 04-09-2013 at 09:13 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Rex Imperator
    So what can I do to make class B work and compile?
    Use the initialiser list of B's constructor, e.g.,
    Code:
    class B
    {
    public:
        B() : a(1, 2) {}
    private:
        A a;
    };
    Quote Originally Posted by Rex Imperator
    Another question I have is why is this valid:
    (...)
    But not this:
    You don't need the class definition to declare a pointer to an object of that class, hence a forward declaration will do. However, if you want to actually have an object of that class, then you need to know how it is defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    On why it works if you declare a pointer, but not an object - when it invokes B's constructor (even the default one), it needs to know how to create each member of B. It can create an A* easily (it creates the pointer only, which points to nothing). It can't create an A, because it doesn't know which constructor to use.

    You can create a constructor and pass an initialization list:

    Code:
    class A; //This is in a separate header file
    
    class B
    {
    public:
        B: a(0, 0)
        {
            // You can do any other constructor-related things here.
        }
    private:
        A a;
    };
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Drat, beaten to the punch! I agree with what laserlight said
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    7
    Ah, it worked! Thank you both, =]!

    I think I need to study up on pointers. Lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initalising Objects in Separate File
    By KingTravis1986 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2012, 04:26 PM
  2. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 08:02 AM
  3. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  4. Problem with separate compilation
    By Clyde in forum C++ Programming
    Replies: 16
    Last Post: 05-18-2002, 09:43 AM
  5. separate compilation
    By PutoAmo in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 09:29 AM