Hi guys,

Ok, so I'm a bit stuck. I'm creating a program that has 3 classes. An object of one of the classes needs to be made up from 2 objects from one of the other classes.. and I can't figure out how to do it.

for example, if I have:

class Room{
private:
char name[];
int length;
// etc etc
public:
void some_function

and the I have..

class House{

..whos attributes are 2 'Rooms'. How would I do this?

If I try..

class House[
private:
Room 1;
Room 2;
// etc

..I get a compilation error telling me that 'Room' does not name a type!

I need to be able to send the parameters to the 'House' constructor, that will allow me to create 2 objects of type 'Room' within the class House.

Any help would be v.gratefully received!

Thanks in advance.

Dani AI

Generated

Short expert note tied to the existing replies: composition (a class containing other class instances) is the normal C++ approach. correctly flagged the bad member identifier in the original sample, and is right that it can be done. The key points that were not shown yet are (1) the type Room must be known before House is declared (or included), and (2) non‑default constructible members must be initialized in the House constructor initializer list.

A minimal, idiomatic pattern (different from the original snippet) looks like this:

#include <string>

class Room {
public:
    Room(const std::string& n, int l) : name(n), length(l) {}
private:
    std::string name;
    int length;
};

class House {
    Room living;
    Room bedroom;
public:
    // accept Rooms by reference (no extra copies)...
    House(const Room& r1, const Room& r2) : living(r1), bedroom(r2) {}

    // ...or construct the two Room members from primitives
    House(const std::string& n1, int l1, const std::string& n2, int l2)
      : living(n1, l1), bedroom(n2, l2) {}
};

Troubleshooting checklist (common causes of "X does not name a type" or similar errors): ensure Room is defined or its header is included before House if storing by value; use a forward declaration (class Room;) only if House holds pointers or references to Room; watch for syntax errors earlier in the file (missing semicolon after a class, wrong braces/brackets); prefer std::string over incomplete C-style arrays for member strings; and initialize non-default members in the initializer list. For variable counts of rooms prefer std::vector<Room>, and for polymorphic/dynamic ownership use smart pointers (C++11+).

Recommended Answers

All 5 Replies

Would you mind posting the entire code?..

>>Room 1;
Did not you learn that variables start with alphabets and _?

>Did not you learn that variables start with alphabets and _?
How did I not notice that :).. Well spotted.

Hi - sorry, don't have the code to hand. This was a project i was working on in school. I just wanted to know if it was possible to create 2 objects of a class within the constructor of a separate class... ?

Yes it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.