Dev178
Dev178

Reputation: 39

error: no member function declared in class

I keep getting the following errors from my code:

(line 58) error: no 'std::string Person::Modify_Person(Person)' member function declared in class 'Person' In function 'int main()':

(line 113) error: 'Modify_Person' was not declared in this scope

Here is the code:

#include <iostream>
#include <string>

using namespace std;

void PassByByValue(int num2){

  cout << "You are in PassByValue()" << endl;

  num2++;

}
class Person{
    int age;
    string name;
    int height;
    int weight;
public:

    Person(){

    }

    Person(string name){
        this->name=name;
    }

    string getName(){
    return this->name;
    }

    void setAge(int age){
    this->age=age;
    }


    void setName(string name){
    this->name=name;
    }

    void setHeight(int height){
    this->height=height;
    }

    void setWeight(int weight){
    this->weight=weight;
    }


    ~Person(){

    }

};



string Person::Modify_Person(Person example){

    example.getName()="Jessica";

    return example.getName();
}

void PassByRef(int& num3){

cout << "You are in PassByRef()" << endl;

num3=50;

cout << "inside PassByRef() pNum is: " <<num3<<endl;

}

int main()
{
   int num1;
   int* pNum;

    num1=3;
    *pNum=5;

    PassByByValue(num1);

    cout << "num1= " <<num1 <<endl;

    PassByRef(*pNum);

    cout << "outside PassByRef() in main() pNum is: " <<pNum<<endl;

    PassByByValue(*pNum);

    double* DblePtr;

    DblePtr= new double;

    *DblePtr=12.0;

    cout<< "DblePtr: "<< &DblePtr;

    delete[] DblePtr;

    cout<< "DblePtr: "<< &DblePtr;

    Person human;
    human.setName("Kate");
    human.setAge(27);
    human.setHeight(100);
    human.setWeight(100);

    Modify_Person(human);

    cout << "Modify_Person returns: " << Modify_Person(human) <<endl;

    cout << "name should be Jessica: " << human.getName() << endl;

    return 0;
}

Upvotes: 3

Views: 26705

Answers (2)

R Sahu
R Sahu

Reputation: 206577

The function

string Person::Modify_Person(Person example) {

   example.getName()="Jessica";

   return example.getName();
}

has the following problems.

  1. Use of string Person::Modify_Person(Person example) { ... } to define a function is valid only of Modify_Person is declared as a member function of the class. Since, it is not, you just need a global function.

    string Modify_Person(Person example) {
    ...
    }
    
  2. The function cannot modify the object in the calling functions since the argument gets passed by value. No matter what you do to example, the value of the object that was used to call the function remains unchanged in the calling function. If you want to see any changes made to example to be visible in the calling function, you need to accept the argument by reference.

    //                         |
    //                         v
    string Modify_Person(Person& example) {
    ...
    }
    
  3. The line

    example.getName()="Jessica";
    

    in the function does not modify the name of example. It is equivalent to saying:

    string temp = example.getName();
    temp = "Jessica";
    

    Hence, the line below that returns the name of example will simply return the name of example, and not "Jessica", which I believe is counter to your expectation.

    That line needs to be changed to:

    example.setName("Jessica");
    

Here's what the function should look like:

string Modify_Person(Person& example) {
   example.setName("Jessica");
   return example.getName();
}

Upvotes: 1

DeepCoder
DeepCoder

Reputation: 840

You cannot declare a member function outside a class in C++. To fix this, add a corresponding member function declaration to your class:

class Person{
...
public:
string Modify_Person(Person);
};

Then your code will work. Also, a suggestion: don't define constructors and destructors if they are empty; allow the compiler to generate them for you. If you are intending to disable move constructors etc. by doing this, write Person() = default; to have the compiler generate a default implementation.

Upvotes: 3

Related Questions