Why the following code shows error?

Sep 17, 2021 at 3:13am
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 Sep 17, 2021 at 8:48am
Sep 17, 2021 at 3:34am
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.
Sep 17, 2021 at 3:38am
Even if I exclude conio.h then also it shows error!
Sep 17, 2021 at 3:44am
perhaps you could copy the error message and tell us what it says?
Sep 17, 2021 at 8:49am
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 Sep 17, 2021 at 8:50am
Sep 17, 2021 at 9:03am
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.