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));
}
4
  • 1
    I'm not sure I have ever seen a link to imgur as compiler errors on this site. Commented Aug 13, 2020 at 19:11
  • 5
    Please include the errors as text, inside the question (not through a link) TIA Commented Aug 13, 2020 at 19:11
  • 1
    You have not implemented either constructor (declared but not defined). Commented Aug 13, 2020 at 19:13
  • In C++ take arguments as const whenever possible, especially as a reference. string t makes a copy. const string& t does not. Commented Aug 13, 2020 at 19:16

2 Answers 2

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
};
Sign up to request clarification or add additional context in comments.

Comments

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;
}

4 Comments

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.
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 Simple. Use the initializer list to construct all the member variables.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.