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

multi dimernsion array with classes

can someone help check my code? i m trying to create multi dimension array with classes.
it will not compile and i do not get the error message. thanks alot.

#include <iostream>
using namespace std;

const int MAX = 5, MAX2 = 3;

class notebook {
      int num;
public :
       notebook ();
       void set_num( int n ) { num = n; }
       int get_num() { return num; }
};

int main()
{
    notebook Num_Of_Notebook[ MAX ][ MAX2 ] = {};
    int unitNumber = 0;
    
    for (int i = 0; i < MAX; i++)
    cout << "Notebook Model " << i + 1; 
        for (int j = 0; j < MAX2; j++) {
        cout << "Branch " << j + 1;
        Num_Of_Notebook[ i ][ j ].set_num( unitNumber ); }
        
    cin.get();
    cin.ignore();
    return 0;
}
chern4ever
Light Poster
42 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

The error on line 23 says variable i has not been declared. The reason is that you have to use { and } braces in the loop starting at line 19.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Some changes:

#include <iostream>
using namespace std;

const int MAX = 5, MAX2 = 3;

class notebook {
      int num;
public :
       notebook (); //this is wrong. Either write some constructor or don't write it (use default)
       void set_num( int n ) { num = n; }
       int get_num() { return num; }
};

int main()
{
    notebook Num_Of_Notebook[ MAX ][ MAX2 ] = {};
    int unitNumber = 0;
    
    for (int i = 0; i < MAX; i++) //missing bracket!
    cout << "Notebook Model " << i + 1; 
        for (int j = 0; j < MAX2; j++) {
        cout << "Branch " << j + 1;
        Num_Of_Notebook[ i ][ j ].set_num( unitNumber ); }
        
    cin.get();
    cin.ignore();
    return 0;
}
Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

i haven add my constructor code inside. thanks for the help. =)

chern4ever
Light Poster
42 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You