Why the following code shows error?

Why the following code showing error?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;
class rangpur
{
  public:
  char message[6] = {'h', 'e', 'l', 'l', 'o'}, friday[] ={'h', 'e', 'l', 'l', 'o','\0'}, sunday[]= "Promod Kumer Dey";
  void display()
  {
  cout<<message[2]<<" "<<sunday<<endl;
  }
};
int main()
{
    rangpur ob;
    ob.display();
    return 0;
}


error:
1
2
7:87: error: too many initializers for 'char [0]'
7:100: error: initializer-string for array of chars is too long [-fpermissive]
Last edited on
conio.h is from the 1990s era, DOS operating system days. Its not available on all compilers and was never part of c++, its a third party library.
Even if I exclude conio.h then also it shows error!
perhaps you could copy the error message and tell us what it says?
Error
1
2
7:87: error: too many initializers for 'char [0]'
7:100: error: initializer-string for array of chars is too long [-fpermissive]
Last edited on
Within a class, the size of arrays have to be stated. [] is not allowed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

class rangpur
{
public:
	char message[5] {'h', 'e', 'l', 'l', 'o'};
	char friday[6] {'h', 'e', 'l', 'l', 'o','\0'};
	char sunday[17] {"Promod Kumer Dey"};
	void display()
	{
		cout << message[2] << " " << sunday << endl;
	}
};

int main()
{
	rangpur ob;
	ob.display();
	return 0;
}



l Promod Kumer Dey

Topic archived. No new replies allowed.