0

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 .

  • 3
    `CAddress CPerson::getAddress()` returns a copy of `this->Address`, thus it is a temporary object. Make the daclaration `const CAddress &CPerson::getAddress()`. – 273K Oct 16 '20 at 00:01
  • 1
    @S.M. That looks like an answer :) – cigien Oct 16 '20 at 00:05
  • @cigien This question is not worth an answer, it is a duplicate of [Error: taking address of temporary \[-fpermissive\]](https://stackoverflow.com/questions/16481490/error-taking-address-of-temporary-fpermissive) – 273K Oct 16 '20 at 00:08
  • @S.M. If you think it's a dupe, then could you vote to close it as such? It would avoid redundant answers being posted. – cigien Oct 16 '20 at 00:18
  • It's actually not the same thing , same idea yea but different perspective w/e.Concerning the issue , for example here if I change the declaration to *const CAddress &CPerson* then it will be invalid since the array CAddress is not const ? –  Oct 16 '20 at 00:21
  • 1
    Getting the class to do what you want is not the issue, but what do you *want* to do in `main`? Why are you storing pointers to members of objects? – cigien Oct 16 '20 at 00:24
  • @S.M. I agree that the question you name has answers that apply here despite it not being a verbatim duplicate. – bitmask Oct 16 '20 at 00:31
  • @cigien to print a sort of a list . 'Why are you storing pointers to members of objects?' Actually I was given the main.cpp and I'm asked to finish the other parts ( tutorial not class) . I too didn't know the reason why we're storing pointers to members of objects .I guess the goal here was to work around it. –  Oct 16 '20 at 00:36
  • I solved it actually there is no need to write const . 'CAddress &CPerson::getAddress()' will suffice . –  Oct 16 '20 at 00:53
  • @Upgrade Exactly same question or context isn't relevant for duplicates. The point is that the answer(s) answer your quesiton in a sufficient manner. Consider this when researching a specific problem here. – πάντα ῥεῖ Oct 16 '20 at 00:55
  • @πάνταῥεῖ No they don't. All 3 answers show how to change the call site. – cigien Oct 16 '20 at 00:56
  • @cigien OK, I missed that point. Though the quesiton looks a bit like _"I want a pony"_ in that light. _@S.M._ is probably right. – πάντα ῥεῖ Oct 16 '20 at 00:57
  • The requirements in `main` are strange. They require you to write a type that hands out non-const references to its members. You seem to have found the solution, but it's not a good idea in general. – cigien Oct 16 '20 at 01:00

0 Answers0