So basically I'm having the error : taking address of temporary and I didn't exactly know why.
I tried to simplify the code to just focus on the issue itself . The error occurs on the adress array.
main.cpp has these lines below :
CPerson Student1("Test1", "street1.", "15a", 12045, "Berlin", 15, 9, 1989);
CPerson Student2("Test2", "street2", "27", 29031, "Milan", 27, 5, 1991);
CPerson Student3("Test3", "street3", "3-5", 12345, "Paris", 3, 11, 1987);
CPerson Student4("Test4", "street4", "23", 19885, "Tokyo", 19, 7, 1985);
CPerson *Studenten[4] = { &Student1, &Student2, &Student3, &Student4 };
CAddress *Adressen[4] = { &(Student1.getAddress()), &(Student2.getAddress()),
&(Student3.getAddress()), &(Student4.getAddress()) }; //Error occurs here
caddress.hpp :
class CAddress
{
public:
CAddress() = default;
void print() const;
friend class CPerson;
~CAddress(){};
private:
std::string Street;
std::string HouseNr;
unsigned Zipcode;
std::string City;
};
CPerson.hpp
class CPerson
{
public:
CPerson() = default;
CPerson(std::string m_Name, std::string m_Street,
std::string m_HouseNr, unsigned m_Zipcode,
std::string m_City, int m_Day, int m_Month, int m_Year);
CAddress getAddress();
void print() const;
~CPerson(){};
private:
unsigned ID;
std::string Name;
CAddress Address;
CDate Birthday;
};
CPerson.cpp
CPerson::CPerson(
string m_Name,
string m_Street,
string m_HouseNr,
unsigned m_Zipcode,
string m_City,
int m_Day, int m_Month, int m_Year)
{
Name = m_Name;
Address.Street = m_Street;
Address.HouseNr = m_HouseNr;
Address.Zipcode = m_Zipcode;
Address.City = m_City;
Birthday.Day = m_Day;
Birthday.Month = m_Month;
Birthday.Year = m_Year;
}
CAddress CPerson::getAddress()
{
return this->Address;
}
I tried to delete the pointer on the array and the address on the elements of the array and it worked correctly but what I actually want is to work around it and leave the main as it is .