Array of classes within class definition

This is a discussion on Array of classes within class definition within the C++ Programming forums, part of the General Programming Boards category; Does anyone know how to get this code to work? It compiles fine in Visual Studio, but not in Borland. ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Array of classes within class definition

    Does anyone know how to get this code to work? It compiles fine in Visual Studio, but not in Borland. I need this to work in Borland.
    All I want to do is locally decare an array of classes within a class, and initalise them to their relevant values. However, in Borland I keep getting these errors:

    [C++ Error] x.h(21): E2034 Cannot convert 'A' to 'A[3]'
    [C++ Error] x.h(28): E2176 Too many types in declaration

    Code:
    class A
    {
      public:
        A(int i) : m_i(i) {}
    
      protected:
        int m_i;
    };
    
    class B
    {
      public:
        B() : m_A(A(1), A(2), A(3)) {}
    
      protected:
        A m_A[3];
    }
    Any help would be greatful
    Be a leader and not a follower.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I don't think you can initialize arrays in the initializer list. I'm not exactly sure why. I've run into this same problem before. You'll need to overload an equals operator if you want this to work.
    Code:
    class B
    {
      public:
        B() {m_A[0] = 1; m_A[1] = 2; m_A[2] = 3;}
    
      protected:
        A m_A[3];
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Oh damn it. I was just declare the buggers dynamically.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Array of classes within class definition
    By subdene in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2005, 09:41 AM
  4. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 04:58 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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