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;
}

Recommended Answers

All 3 Replies

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.

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;
}

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

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.