2 dimesional arrays inside the class

Hi everybody, I want to declare a two dimensional array inside a class as a private member but I get an error message.Do you have any suggestions ? :

#include<iostream>
using namespace std;
class Plane{

private:
char PlaneStruct[8][7] ={{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G'}};

public:
void DisplayPlane();

};
Plane :: DisplayPlane(){
for (int i = 0; i < 8; ++i)
for(int j = 0; j < 7; ++j){
cout << PlaneStruct[i][j] ;
return 0;
}

}
int main(){

DisplayPlane();
system("pause");
return 0;
}
1) Data initialization is not allowed in a class definition. Put this in the constructor.

2) The definition of DisplayPane() doesn't return anything. You need to define that, also don't return 0 for a void function.

3) To use DisplayPane() in main, you need to define an object of the class.

Here is what I believe you meant to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream> 
using namespace std; 

class Plane
{ 
private: 
	char PlaneStruct[8][7]; 
public:
	Plane();// Constructor
	void DisplayPlane(); 
};

Plane::Plane()
{
	char Row[7] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 7; j++)
			PlaneStruct[i][j] = Row[j];
}

void Plane::DisplayPlane()
{ 
	for (int i = 0; i < 8; ++i) 
	{
		for(int j = 0; j < 7; ++j)
			cout << PlaneStruct[i][j]; 
		cout << endl;
	}
	return; 
} 
int main(){ 
	Plane PlaneA;
	PlaneA.DisplayPlane(); 
	system("pause"); 
	return 0; 
}
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
Press any key to continue . . .
Last edited on
This was exactly what I wanted to write.
Thank you very much for your statements and the corrected code !
Hello again! here is one more code I am concerned with.This time I want to create a class Student (with constructors ets.). After all I want my ShowObj function to write the object to the file, but after I compile & run the programme I don't get my.txt output file, do you have any suggestions ? The code works
fine with cout instead of out_stream.

#include<iostream>
#include<fstream>
using namespace std;

class Student{
private:
int studentNo ;
string Address;
int age ;
public:
Student(int no, string Saddress, int Sage);
Student(int no, string Saddress);
Student(int no);
Student();
void SetAllData(int no, string Saddress, int Sage);
void SetNoandAddress(int no, string Saddress);
void SetNo(int no);
void SetAddress(string Saddress);
friend void ShowObj (Student obj);
};

Student::Student(int no, string Saddress, int Sage){
studentNo = no;
Address = Saddress ;
age = Sage ;
}
Student::Student(int no, string Saddress){
studentNo = no;
Address = Saddress;
age =0;
}
Student::Student(int no){
studentNo = no;
Address = "N/A";
age = 0;
}

void Student:: SetAllData(int no, string Saddress, int Sage){
studentNo = no;
Address = Saddress ;
age = Sage ;
}

void Student:: SetNoandAddress(int no, string Saddress){
studentNo = no;
Address = Saddress;
}
void Student::SetNo(int no){
studentNo = no;
}
void Student:: SetAddress(string Saddress){
Address = Saddress;
}


void ShowObj (Student Theobj){
ofstream out_stream;
out_stream.open("c:\\my.txt");
if (out_stream.fail()){
cout << "\n!!!Out_stream has failed !!!" << endl;
exit(1);

}
out_stream << endl;
out_stream << "========================================" << endl;
out_stream << "Student no:" << Theobj.studentNo << endl;
out_stream << "Student address: " << Theobj.Address << endl;
out_stream << "Student's age : " << Theobj.age << endl;
out_stream << "========================================" << endl;
out_stream.close();
return;
}

int main(){

Student Murad (12322,"1cijdksjd,80,40", 23);
ShowObj(Murad);
Murad.SetNo(22);
ShowObj(Murad);
Murad.SetAddress("Blackfoot 22. USA");
ShowObj(Murad);



system("pause");
return 0;
}
How interesting!

I tried this out with:
out_stream.open("c:\\my.txt");
But then tried
out_stream.open("c:\\temp\\my.txt");
This worked!

I'm using windows 8 (regrettably) and I'm guessing that there is some sort of protection on the root of the C drive.
Last edited on
Topic archived. No new replies allowed.