I have just started learning c++. I am trying to play with classes and objects in this code. The code is simply about taking the data contents of an array from the user and then as an output show the data contents of the array back.

#include <iostream>using namespace std;class array{public:int ar[5];void putData();void disp();}array :: void putData(){for(int i=0;i<5;i++){cin>>ar[i];}}array :: void disp(){for(int i=0;i<5;i++){cout<<ar[i];<<endl;}}int main(){array m;m.putData();m.disp();return 0;}
2

Best Answer


array :: void putData()

Whoops...

It's supposed to be:

void array :: putData()

Or, rendered more clearly:

void array::putData()

It's because the full name of the function you're defining is array::putData; you accidentally butchered your return type (void) by writing the array:: part in the wrong place.

You need to define functions this way

void array::putData(){for(int i=0;i<5;i++){cin>>ar[i];}}