bool_is_cool
bool_is_cool

Reputation: 11

Why does this Constructor with no arguments seem to be a problem for this code?

I can't compile this code. It seems that the problem is with the constructor of the Friends class, but I can't understand where the problem is.

I tried to remove it and it compiled successfully, but the default constructor doesn't seem to work as well.

Here is what the compiler shows:

UnNamed.cpp: In constructor ‘Friends::Friends()’:
UnNamed.cpp:92:13: error: no matching function for call to ‘Friend::Friend()’
   Friends() {
             ^
UnNamed.cpp:52:3: note: candidate: Friend::Friend(std::__cxx11::string, std::__cxx11::string)
   Friend (string n, string c): Person (n, c) {
   ^~~~~~
UnNamed.cpp:52:3: note:   candidate expects 2 arguments, 0 provided
UnNamed.cpp:41:7: note: candidate: Friend::Friend(const Friend&)
 class Friend: public Person {
       ^~~~~~
UnNamed.cpp:41:7: note:   candidate expects 1 argument, 0 provided
UnNamed.cpp:41:7: note: candidate: Friend::Friend(Friend&&)
UnNamed.cpp:41:7: note:   candidate expects 1 argument, 0 provided

Here is the code:

#include <iostream>
#include <string>
using namespace std;

struct Date{
  int d;
  int m;
  int y;
};


class Person{

protected:
  string name;
  string surname;

public:
  Person(){
    name = "xxx";
    surname = "xxx";
  }

  Person (string n, string c) {
    name = n;
    surname = c;
  }
  string get_surname() {
    return surname;
  }
  void print (ostream& f_out) const {
    f_out << name << " " << surname;
  }

};




class Friend: public Person {

private:
  Date bdate;
  string email;

  bool bissextile( int a ) {...}

  bool check_date() {...}

public:
  Friend (string n, string c): Person (n, c) {
    email = "xxx";
    bdate.d = 1;
    bdate.m = 1;
    bdate.y = 1;
  }

  void set_date (int d, int m, int y) {...}

  void set_email (string e) {
    email = e;
  }

  void print ( ostream& f_out) {
    f_out << name << " " << surname << " " << bdate.g << "/" << bdate.m <<"/" << " " << bdate.a << "/" << " " << email;
  }


};



class Friends {

private:

  Friend friend_list[100];
  int i;

public:

  Friends() {
    i = 0;
  } 

  void add(Friend a) {
    string err = "Out of space";
    if ( i == (100 - 1) )
      throw err;

    friend_list[i] = a;
    i++;
  }


  void print (ostream& f_out)  {
    for (int j = 0; j < i; j++)
      friend_list[j].print(f_out);
  }

};

Upvotes: 0

Views: 631

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96649

Each constructor of a class (class Friends in this case) has to initialize each field of that class.

Since Friends() doesn't have a member initializer list, each field is default-initialized (by default).

For classes, default-initialization just means calling the default constructor, and since Friend doesn't have a default constructor (it's not generated automatically since you have a custom constructor), the compiler doesn't know how to initialize Friend friend_list[100];.

On the other hand, if you don't provide Friends(), the compiler attemps to generate one for you. It fails to do so for the same reason, but for implicitly generated constructors this doesn't result in a error. Rather, Friends() gets deleted (the compiler will tell you it's deleted if you try use it).

Upvotes: 3

Related Questions