C ++ Passing an object to another object?

I don't know if I missed something, but I can't figure out how to do it and couldn't find the answer online.

Suppose I have two classes: class A and class B. (stored in separate files)

Class A has a setName () function that sets a variable inside an object of class A.

Class B has a setOtherName () function that sets the value of the name of an object of class A.

So I set setOtherName () up like this:

void setOtherName(ClassA& cla)
{
*cla.setName("foobar");
}

      

then my main script looks like this:

Class A burger;
Class B fries;
fries.setOtherName(*burger);

      

this doesn't work in my orignal script, i get the following error:

error: no matching function for call to 'ClassB::setOtherName(ClassA*&)

      

Any help is appreciated! (sorry for any confusion)

Actual code: main.cpp:

#include <iostream>
#include "quests.h"
#include "player.h"
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    quests GameQuests;
    player Player;
    GameQuests.quest1(Player);
    Player.main();

    return 0;
}

      

quests.cpp:

#include "quests.h"
#include "player.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void quests::quest1(player& charact){
    cout << "By the way, what was your name?" << endl;
    person1=4;
    system("pause");
    charact->setName();
}

      

+3


source to share


3 answers


Your function implementation setOtherName

must be signed

void ClassB::setOtherName(ClassA& cla)

      

You need to indicate what it is included in ClassB

. In your class definition, ClassB

be sure to include

void setOtherName(ClassA&);

      



Also, since your variable burger

is of type ClassA

and not type ClassA*

, there is no need to dereference the variable when passing it to a function. Call it like

fries.setOtherName(burger);

      

You also dereferenced the variable incorrectly cla

. This object is passed by reference, not by pointer, so there is no need for dereferencing.

+4


source


Why are you deregulating the burger? You told the compiler to expect class A by reference, not by pointer.

Try:

fries.setOtherName(burger);

      

Also get rid of the asterisk on setOtherName.



void setOtherName(ClassA & cla)
{
   cla.setName("foobar");
}

      

EDIT: Wrote an example program that I think you are trying below.

#include <iostream>
#include <string>

class Burger
{
public:
    Burger(){}
    void setName(std::string name){ m_name = name; }
    std::string getName(){ return m_name; }
private:
    std::string m_name;
};

class Fries
{
public:
    Fries(){}
    void setOtherName(Burger & burger){ burger.setName("FryBurger"); }
private:
};

int main()
{
    Burger A;
    Fries B;

    B.setOtherName(A);
    std::cout << A.getName() << std::endl;
    return 0;
}

      

+2


source


You should read about pointers and link. This is how your function should look like

void setOtherName(ClassA& cla)
{
  cla.setName("foobar");
}

      

There is no need to respect something that is not a pointer.

ClassA burger;
ClassB fries;
fries.setOtherName(burger);

      

again, you don't need to play the burger as it is not a pointer.

If the burger was created like this:

ClassA* burger = new ClassA();

      

and function

void setOtherName(ClassA& cla)

      

took the link, you had to play the burger

fries.setOtherName(*burger);

      

+2


source







All Articles