-2
class Email
{
public:
  //Email(){} //If I added this, this problem is resolved. But I am not sure this is good solution.
  Email(EmailBuilder builder){
     ...
  }

  public class EmailBuilder {
     EmailBuilder() {...};
     EmailBuilder SetAddress(...) {...};
     EmailBuilder SetSubject(...) {...};
     EmailBuilder SetBody(...) {...};
     Email Build() {...};
  }
};  

class EmailSender
    {
    public:
       ...
       SendEmail(const Email& mail)
       {
           mEmail = mail;           }
    private:
       Email mEmail; //error : emailsender.h(8): error C2512: 'Email': no appropriate default constructor available
    }


    int main()
    {
        Email mail = new Email.Builder
                            .SetAddress("me@mail.com")
                            .SetSubject("C++ builders")
                            .SetBody("I like this API, don't you?")
                            .Build();
        ...
        EmailSender emailSender;
        ...
        emailSender.SendEmail(mail);
        ...
    }

It is just pseudocode.

I want to copy parameter(=mail) to local object(=mEmail).

But I cannot it.

If I added default constructor(Email(){}), this problem is resolved. But I am not sure this is good solution.

Could you give me more good solution?

sensolama
  • 23
  • 4
  • 1
    What errors do you get? What is `Aobject`? What members does it contain? Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [edit] your question to include a [mcve] (with emphasis on the *minimal* part), and a copy-paste (as text!) of the full and complete errors you get with comments in the code where you get them. – Some programmer dude Apr 07 '20 at 01:20

1 Answers1

0

Your EmailSender class contains an Email object named mEmail. This mEmail object of course needs to be initialized (constructed).

The problem is that mEmail needs to be constructed before the body of the EmailSender constructor runs, and the compiler can only default-construct it using the non-existent default constructor, leading to your error.

The solution is to explicitly initialize (construct) mEmail in the EmailSender constructor initializer list:

EmailSender(Email const& email)
    : mEmail{ email }  // Initialize (construct) using the copy-constructor
{
    // Empty body
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, But If EmailSender class is singleton, I think your solution is not available. EmailSender() : mEmail( make empty Email object ) How about this? It is working. But I am not sure whether it is reasonable code. Please let me know your opinion. – sensolama Apr 08 '20 at 04:15
  • @sensolama Then I sense a problem in your *design*. If `EmailSender` should be a singleton that can be used to send multiple different emails, then it shouldn't really have its own `mEmail` member object. Instead it should have a `Send` function that takes a (reference) to an `Email` object. – Some programmer dude Apr 08 '20 at 04:29
  • Thanks. Actually, currently I use "struct Email { ... };" to resolve simply this problem. (like C data structure). But I hope I want to resolve this issue using C++ language with good design. – sensolama Apr 08 '20 at 04:47