Dynamically allocating class object

Continuing off post I made earlier in this forum I'm trying to implement a program where the user can enter Title, Authors, and Publication of Books. I need to use 'new' so I can get practice with new and delete operations. I plan to implement this as a function later. I'm confused to why its not working because the commented code works and I believe they are similar aside from that now I want user entered information.

Full code: http://codeviewer.org/view/code:51e3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  int main()
{
//    const Date pub_date(1,2,2003);
//    const Author alice("Alice", "Kray");
//    const Author jaina("Jaina", "Proudmore");
//    const vector<Author> authors = {alice,jaina};
//
//    const Book children_book("Kids Tales", authors, pub_date, true);
//    cout << children_book.to_string();


    string title;
    string first_name;
    string last_name;
    int MM;
    int DD;
    int YYYY;
    cout << "Enter the title of the book.\n";
    getline(cin,title);
    cout << "Enter the author of the book. (First name followed by last name)\n.";
    cin >> first_name >> last_name;
    cout << "Enter the publication date of the book. (MM DD YYYY) If a month ony has 1 digit, enter only that digit.\n";
    cin >> MM >> DD >> YYYY;

    const Date pub_date(MM,DD,YYYY);
    const Author firstauthor (first_name,last_name);
    const vector<Author> authors {firstauthor};

    new const Book newbook1(title, authors, pub_date,true); //error arises here.



}
Last edited on
I've tried looking at examples online. But all I've come to is this.
Not sure where to go from here.
1
2
3
int *pointer = new Book(title, authors, pub_date,true);
//cannot convert Book* to int* in initialization
Last edited on
Why int* ??

Book *pointer = new Book(title, authors, pub_date,true);

Plus remember to delete after you're done with the object.

Andy
Ohh... I was under the impression that pointers always needed to be of a numerical data type. I don't know why I kept thinking that. Thanks again Andy.
Topic archived. No new replies allowed.