issue with classes in different file

0 Royson Mendonca · September 7, 2015
Kindly help me below is the code

this is my main file


#include <iostream>
#include <string>
#include "person.h"

using namespace std;

int main()
{

string b;
string c;

cout<<"enter the name";
getline(cin,b);

person obj1(b);

obj1.getdata();

cout << obj1.getdata();

}



below are the classes file

classes declaration
#ifndef PERSON_H
#define PERSON_H
#include <string>

using namespace std;


class person
{

private:
    string a ;


    public:
        person(string x);
         void setdata(string m);
        int getdata();


};

#endif // PERSON_H


and below the function execution 

#include <iostream>
#include <string>
#include "person.h"

using namespace std;

person::person(string x)
{


  setdata(x);
}
person::void setdata(string m)
{


    a=m;
}

person::int getdata()
{


    return a;

}


I am getting an error saying
expected unqualified id before void

expected unqualified id before int

Post a Reply

Replies

Oldest  Newest  Rating
0 Alan Masimba Walker · September 7, 2015
That's because if you are writing the body of a function which a member of a class, it goes - 
Data_Type Class::FunctionName(); 

So you put  'person::voice setdata', when the correct format is : 

void person::setdata(string m)
  • 1

C++

107,111 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator