Constructor Cannot Read Data

I created a main, and a constructor that looks like

Main:
1
2
3
4
5
6
7
8
9
10
#include "Book.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    Book("Reading", 56);
}


And, assuming the .h file is correct, the constructor in the .cpp file looks like:
1
2
3
4
5
6
7
8
#include "Book.h"


Book::Book(string title, int pages)
{
    bookTitle = title;
    bookPages = pages;
}


I ran the program, and it crashed. So I debugged it, and found that when reading the strings, I got an error of "error reading characters of string" and debugging some more got me some weird numbers like x00000 or something.

However, if in the main I do something like:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Book.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string a = "Reading";
   int b = 56;
    Book(a, b);
}


Then I don't get an error and it works fine. What can I do so that the first method works because that is the way I need to get it to work?
Last edited on
this worked fine>
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
#include <iostream>
#include <string>

using namespace std;

class Book {
	string bookTitle;
	int bookPages;
	
public:
	Book () {};
	Book (string title, int pages);
	void show () { cout << bookTitle << " " << bookPages;	}	
};

Book::Book(string title, int pages)
{
    bookTitle = title;
    bookPages = pages;
}

int main()
{
	string a = "Reading";
	int b = 56;
	Book b1("writing", 60);
	b1.show();
   
	return 0;
}
That's really strange. My code looks identical to that in relation to the constructor and data fields (although my data fields are private). And when I debug I get an error on reading the strings in, and int comes out to a gigantic negative number.
Bump, can anyone else offer any insight why this is going on?
Please post the contents of Book.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <string>
#include <iostream>

using namespace std;

class Book
{

public:
    Book();
    Book(string, int);

    string getBookTitle() const;
     int getPages() const;

    ~Book();

private:
    string bookTitle;
    int bookPages;
};
Last edited on
Is there anything wrong with my code on why it can't read the strings?
your 3rd snippet:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Book.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string a = "Reading";
   int b = 56;
    Book(a, b); /// wrong, create an object like Book obj1(a, b);
}
Book(a,b) is valid C++. It creates a temporary object that gets destroyed sometime before going out of scope.

DreamTime, try recompiling all your files. I suspect you changed the declaration of Book at some point so book.cpp and main.cpp think it is different.
Int main(), rather than creating the book using Book(a, b); try using Book book1{a, b};, or skip the individual variables and use Book book1{ "Reading", 56 };
Last edited on
Topic archived. No new replies allowed.