0

https://imgur.com/gallery/pEw8fBs https://pastebin.com/xvWFHrTU

My errors are posted above..I don't understand what's wrong with this code..I'm trying to construct a book with a date object inside of it. Please help, this is my frist post as well!! :)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }

class Date {
    int y, m, d;

public:
    Date(int d, int m, int y);
};

class Book {
    string title;
    string author;
    string isbn;
    Date date;

public:
    Book(string t, string a, string id, Date d);
};

int main() {
    Book Charlie("charlie", "gates", "333H", Date(1, 2, 3));
}
bipll
  • 11,747
  • 1
  • 18
  • 32

2 Answers2

1

Both of your constructors are declared but not defined

class Date {
    int y, m, d;

public:
    Date(int _d, int _m, int _y) : y(_y), m(_m), d(_d) {} // definition
};

class Book {
    string title;
    string author;
    string isbn;
    Date date;

public:
    Book(string t, string a, string id, Date d)
    : title(t), author(a), isbn(id), data(d) {} // definition
};
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Your problem will be solved if you declare a default constructor for Date:

#include <iostream>
#include <string>

using namespace std;

class Date {
    int y, m, d;

public:
    // Default constructor which will be used in 'Book'
    Date() {}
    // Don't semicolon here, just two braces required
    Date(int d, int m, int y) {}
};

class Book {
    string title;
    string author;
    string isbn;
    // Default constructor called here
    Date date;

public:
    // You can code everything inside the braces now
    // NOTE 2: Default constructor called here
    Book(string t, string a, string id, Date d) {}
};

int main() {
    Book Charlie("charlie", "gates", "333H", Date(1, 2, 3));

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Except the non-default constructor disregards the inputs, surely those are meant to initialize the class members. And the OP should [prefer initialization lists instead of the constructor body](https://stackoverflow.com/questions/9903248/initializing-fields-in-constructor-initializer-list-vs-constructor-body). – Cory Kramer Aug 13 '20 at 19:16
  • what if I didn't want to use the default constructor? how would I go about validating each variable in the date through the book? – Cluelesshint Aug 13 '20 at 19:23
  • @Cluelesshint Simple. Use the initializer list to construct all the member variables. – Rohan Bari Aug 13 '20 at 19:27
  • @RohanBari I tried this..when I try and create the object above the date isn't stored correctly with the default constructor..i'm so lost on how to get it to not use default construct – Cluelesshint Aug 13 '20 at 20:44