954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array of objects declared in a class

Hi everyone,

I'm having issues incorporating something required by my assignment. I am supposed to create a class that has 2 private data members. One should be an array of 10 Database (Database being the name of the other class) objects. The other should be an integer that assigns the number of databases actually stored in the array.

This is what I tried:

#ifndef DATABASE_H
#define DATABASE_H
#include "database.h"

class DatabaseDB
{
     private:
             const int ARRAY_SIZE = 10;
             Database DatabaseArray[ARRAY_SIZE];
};


I am getting an error that reads "C++ forbids initialization of member ARRAY_SIZE" and "error making ARRAZY_SIZE static". How can I properly make these two private data members with no errors? Thank you.

randrum1707
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This compiles for me.

class Database
{
   private:
     static const int num=10;
     int v[num];
};
int main(){
  Database db;
  return 0;
}

Just remember that the static means that is only one copy of this number for all classes.

histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 

Thanks, but unless I'm reading it wrong I don't see an array of objects in that code histrungalot. I need an array of Database objects to be defined privately in the class. Am I missing something?

randrum1707
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

int or object should be the same. Try putting the static in and see if it complies for you code

class foo
{
   private:
      int things; 
};
class Database
{
   private:
     static const int num=10;
     foo v[num];

};

int main(){
  Database db;
  return 0;
}
histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 

Works great, thanks!

randrum1707
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You