C++ Pass an object into another object?
Solution 1
The implementation of your setOtherName
function should have the signature
void ClassB::setOtherName(ClassA& cla)
You need to specify that it is included in ClassB
. Within your class definition of ClassB
, make sure to include
void setOtherName(ClassA&);
Furthermore, since your variable burger
is of type ClassA
and not of type ClassA*
, there is no need to dereference the variable upon passing it into the function. Call it like
fries.setOtherName(burger);
You have also incorrectly dereferenced the variable cla
. That object is passed by reference, not pointer, so there is no need to dereference.
Solution 2
Why are you derefrencing 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 a sample program of what I think you are trying to do 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;
}
Sam Tubb
I have basic knowledge of: C++ Java Atari Basic And am intermediately skilled in: Python Batch Programming “Give a man a program, frustrate him for a day. Teach a man to program, frustrate him for a lifetime.” - Muhammad Waseem
Updated on June 04, 2022Comments
-
Sam Tubb almost 2 years
I don't know if I've missed something, but I can't seem to figure out how to make this work, and couldn't find the answer online.
Lets say I have a two classes, Class A, and Class B. (stored in separate files)
Class A has a function setName() that sets a variable within a Class A object.
Class B has a function setOtherName() that sets the value of a Class A object's name.
So I set setOtherName() up like so:
void setOtherName(ClassA& cla) { *cla.setName("foobar"); }
then my main script looks like so:
Class A burger; Class B fries; fries.setOtherName(*burger);
this does not work in my orignal script, I get the following error:
error: no matching function for call to 'ClassB::setOtherName(ClassA*&)
Any help is aprreciated! ( 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(); }
-
Sam Tubb almost 9 yearsMy original code is different from the example, I'll update with my actual code to see if that changes anything, because this still isn't working :/