I'm working on saving objects to a file and reading them back out. Right now I'm trying to do them one at a time to see if I can do and avoid duplicate objects. I'm getting this error: Exception thrown at 0x00915B18 in who.exe: 0xC0000005: Access violation writing location 0xDDDDDDDD
I know that this is because I'm not doing something with a pointer correctly but I'm lost and can't quite figure it out, current code is:
#include<iostream>
#include "Book.h"
#include "InventoryBook.h"
#include "SoldBook.h"
#include<string>
#include<fstream>
using namespace std;
void saveBook(Book);
Book returnBook();
void main() {
Book novel;
InventoryBook shelf;
SoldBook gone;
novel.setAuthor("Joe");
novel.setISBN("1234567788");
novel.setPublisher("Me");
novel.setTitle("Joe vs. the Volcano");
saveBook(novel);
Book rBook = returnBook();
cout << rBook.getAuthor();
system("Pause");
}
void saveBook(Book saved) {
ofstream myfile;
myfile.open("bookInventory.txt", ios::app);
myfile.write((char*)&saved, sizeof(saved));
}
Book returnBook() {
ifstream myfile;
myfile.open("bookInventory.txt", ios::in);
Book novel;
Book newBook;
myfile.read((char*)&novel, sizeof(novel));
newBook.setAuthor(novel.getAuthor());
return newBook;
}
I know that the save book function works and that I run into my issues with the returned book object. I'm just not able to see where this goes wrong. I'm a little fuzzy on pointers.
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include<iostream>
using namespace std;
class Book{
private:
string ISBN;
string bookTitle;
string authorName;
string publisher;
public:
Book(string, string, string, string);
Book();
//destructor
~Book();
//Mutators
void setTitle(string);
void setISBN(string);
void setAuthor(string);
void setPublisher(string);
//accessors
string getTitle();
string getISBN();
string getAuthor();
string getPublisher();
};
#endif
#include "Book.h"
Book::Book() {
ISBN = "";
bookTitle = "";
authorName = "";
publisher = "";
}
Book::Book(string newISBN, string newTitle, string newAuthor, string newPub) {
ISBN = newISBN;
bookTitle = newTitle;
authorName = newAuthor;
publisher = newPub;
}
Book::~Book()
{
}
void Book::setISBN(string newISBN) {
ISBN = newISBN;
}
string Book::getISBN() {
return ISBN;
}
void Book::setTitle(string newTitle) {
bookTitle = newTitle;
}
string Book::getTitle() {
return bookTitle;
}
void Book::setAuthor(string newAuthor) {
authorName = newAuthor;
}
string Book::getAuthor() {
return authorName;
}
void Book::setPublisher(string newPub) {
publisher = newPub;
}
string Book::getPublisher() {
return publisher;
}
Related
Iam new to c++ trying to learn about namespaces and class.
I have 2 classes here Both are in different files.
The first one is
#include<iostream>
using namespace std;
namespace project{
class Student{
string name,address,enrolmentDate ,usn;
int hours;
public:
Student(char* nusn, char* nname, char* naddress, char* ndate,int nhours){
usn = nusn;
name = nname;
address = naddress;
enrolmentDate = ndate;
hours = nhours;
}
Student(){
getData();
}
void getData(){
std::cout << "Enter Usn,Name,Address,EnrolmentDate,Hours\n";
std::cin >> usn >> name >> address >> enrolmentDate >> hours;
}
string getName(){
return name;
}
string getAddress(){
return address;
}
string getenrolmentDate(){
return enrolmentDate;
}
int getHours(){
return hours;
}
string getUsn(){
return usn;
}
};
}
The Second class is
#include<iostream>
using namespace std;
namespace project{
class CourseRegistration{
string usn, courseId ;
int hours, grade;
public:
CourseRegistration(project::Student obj, string courseId, int nhours, int ngrade){
usn = obj.getUsn();
this->courseId = courseId;
hours = nhours;
grade = ngrade;
}
};
}
The First class Compiles fine.But the second class gives an error.
The error is near that Student Object.
Course.cpp:10:38: error: expected ‘)’ before ‘obj’
CourseRegistration(project::Student obj, string courseId, int nhours, int ngrade){
How Do i Correct It?
I do not see any evidence you've included the definition of Student in "the second file." Your class should be delclared in a header (.h) file with the implementation in a source (.cpp) file. Your compiler is likely complaining because it doesn't know that Student is a class.
Student.h
#include<iostream>
using namespace std;
namespace project {
class Student {
private:
string name,address,enrolmentDate, usn;
int hours;
public:
Student( char*, char*, char*, char*, int );
Student();
void getData();
string getName();
string getAddress();
string getenrolmentDate();
int getHours();
string getUsn();
}
}
Student.cpp
#include "Student.h"
namespace project {
Student::Student( char* nusn, char* nname, char* naddress, char* ndate,int nhours ) {
usn = nusn;
name = nname;
address = naddress;
enrolmentDate = ndate;
hours = nhours;
}
Student() {
getData();
}
void getData() {
std::cout << "Enter Usn,Name,Address,EnrolmentDate,Hours\n";
std::cin >> usn >> name >> address >> enrolmentDate >> hours;
}
string getName() {
return name;
}
string getAddress() {
return address;
}
string getenrolmentDate() {
return enrolmentDate;
}
int getHours() {
return hours;
}
string getUsn() {
return usn;
}
}
CourseRegistration.h
#include "Student.h"
#include<iostream>
using namespace std;
namespace project{
class CourseRegistration {
private:
string usn, courseId ;
int hours, grade;
public:
CourseRegistration( Student, string, int, int );
}
}
CourseRegistration.cpp
#include<CourseRegistration.h>
namespace project {
CourseRegistration::CourseRegistration( Student obj, string courseId, int nhours, int ngrade ) {
usn = obj.getUsn();
this->courseId = courseId;
hours = nhours;
grade = ngrade;
}
}
I am quite new to the concept of class relationships and wrote a program to test it out. However, it is giving me some errors.
There are 5 header files for classes University,Dept,Teacher,Student and Course and a .cpp file involved. I have implemented composition between University and Dept and bidirectional association between Dept and Student, Teacher, Course
uni.h
#pragma once
//#include"dept.h"
class University
{
public:
University(string,int);
void setDepartmentName(string,int);
Dept* getDeptAddress(int);
~University();
private:
Dept* departments;
int totalDepts;
string name;
};
University::University(string name1,int num) :name(name1)
{
departments = new Dept[num];
totalDepts=num;
}
void University::setDepartmentName(string name,int depNo)
{
departments[depNo].setName(name);
}
Dept* University::getDeptAddress(int i)
{
return &(departments[i]);
}
University::~University()
{
delete[] departments;
}
dept.h
#pragma once
//class Student;
//class Teacher;
//class Course;
#include"course.h"
#include"teacher.h"
#include"student.h"
class Dept
{
public:
Dept();
string getName();
void setName(string);
~Dept();
private:
Student** students;
Course** courses;
Teacher** teachers;
string name;
int noOfStudents, noOfTeachers, noOfCourses;
};
Dept::Dept()
{
}
string Dept::getName() {
return name;
}
void Dept::setName(string name1) {
name = name1;
}
Dept::~Dept()
{
}
course.h
#pragma once
class Dept;
//#include"dept.h"
class Course
{
public:
Course(string, string);
void assignDept(Dept*);
string getDeptName();
~Course();
private:
Dept* department;
string code;
string name;
};
Course::Course(string code1, string name1) :code(code1), name(name1)
{}
void Course::assignDept(Dept * dep)
{
department = dep;
}
string Course::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Course::~Course()
{}
student.h
#pragma once
#include"dept.h"
class Student
{
public:
Student(string,string);
void assignDept(Dept*);
string getDeptName();
~Student();
private:
Dept* department;
string rollNo, name;
};
Student::Student(string rNo,string name1):name(name1),rollNo(rNo)
{}
void Student::assignDept(Dept *dep)
{
department = dep;
}
string Student::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Student::~Student()
{
}
teacher.h
#pragma once
//#include"dept.h"
class Teacher
{
public:
Teacher(string);
void assignDept(Dept*);
string getDeptName();
~Teacher();
private:
Dept* department;
string name;
};
Teacher::Teacher(string name1) :name(name1)
{}
void Teacher::assignDept(Dept *dep)
{
department = dep;
}
string Teacher::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}
Teacher::~Teacher()
{
}
source.cpp
#include<iostream>
using namespace std;
#include<string>
#include"dept.h"
#include"course.h"
#include"teacher.h"
#include"student.h"
#include"uni.h"
int main()
{
University u1("FAST",3);
u1.setDepartmentName("CS", 0);
u1.setDepartmentName("EE", 1);
u1.setDepartmentName("CV", 2);
Student s1("l144049", "Syed Abdul Wahab");
Course c1("cs1", "ITC");
Teacher t1("abc");
c1.assignDept(u1.getDeptAddress(0));
t1.assignDept(u1.getDeptAddress(1));
s1.assignDept(u1.getDeptAddress(2));
cout << c1.getDeptName()<<endl;
cout << t1.getDeptName() << endl;
cout << s1.getDeptName() << endl;
cin.get();
return 0;
}
However, if i #include 'dept.h' in student.h, course.h and teacher.h, it gives me errors on 'Dept' namely 'identifier 'Dept' is undefined'.
Any help would me greatly appreciated!
The problem is you have a circular dependency: teacher.h includes dept.h which includes teacher.h. This can't work.
To fix it, use "forward declarations" in your header files, and move your implementations to .cpp files.
I have created a class called Book and it can have many Books. But I want to create a class shelf which can contain only 10 of Books if it is greater than than 10 ten it should print Error Message! But I cannot think of a way to make class shelf. So far I have done this :
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
string bookName;
int pNum;
public:
Book();
Book(string tempName, int tNum){
setName(tempName);
setPageNum(tNum);
}
void setName(string bName){
bookName = bName;
}
void setPageNum(int tempNum){
pNum = tempNum;
}
string getName(){
return bookName;
}
int getPageNum(){
return pNum;
}
};
class Shelf{
public:
Book nBook[10];
void addbook();
void Book::addbook(Book nBook[10])
{
for(int i = 0; i<10; i++)
nBook[i] = nBook[i].setName(string bName)
}
};
int main(){
Book math = Book("math", 500);
Book abcd = Book("abcd", 501);
cout << English.getName() <<" "<<English.getPageNum()<<endl;
cout << German.getName() <<" "<<German.getPageNum()<<endl;
}
You can use the standard vector class to store the Book objects in the shelf.
In the example below, the book object will be copied when added to the Shelf
#include <vector>
using namespace std;
class Shelf{
public:
vector<Book> books;
bool addbook(Book book)
{
if(books.size() > 10)
{
return false;
}
else
{
books.push_back(book);
return true;
}
}
};
There are many ways to implement this. Note the default constructor of class Book. Let me know if you have any questions.
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
string bookName;
int pNum;
public:
Book(){
}
Book(string tempName, int tNum){
setName(tempName);
setPageNum(tNum);
}
void setName(string bName){
bookName = bName;
}
void setPageNum(int tempNum){
pNum = tempNum;
}
string getName(){
return bookName;
}
int getPageNum(){
return pNum;
}
};
class Shelf{
public:
Book nBook[10];
int numberOfBooks;
void addbook(Book);
Shelf()
{
numberOfBooks = 0;
}
bool addBook(Book newBook)
{
if (numberOfBooks == 10)
{
return false;
}
else
{
nBook[numberOfBooks] = newBook;
numberOfBooks++;
return true;
}
}
};
int main(){
Book English = Book("math", 500);
Book German = Book("abcd", 501);
Shelf bookShelf = Shelf();
bookShelf.addBook(English);
}
Hello everyone I had a problem while running the following program in the last loop it only print three 0 without displaying any result I am not sure what is wrong with the program,perhaps it has something to do with the constructor, however when I write my getter method in the second last loop with setter method it display the correct result. any help will be appreciated.
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include<string>
using namespace std;
class Employee
{
private:
string name;
int idnumber;
string department;
string position;
public:
Employee(string ,int ,string ,string);
void setName(string );
void setDepartment(string);
void setPosition(string);
void setIDNumber(int);
string getName() const
{
return name;
}
string getDepartment() const
{
return department;
}
string getPosition() const
{
return position;
}
int getIDnumber() const
{
return idnumber;
}
};
#include "Employee.h";
#include <iostream>
#include <iomanip>
using namespace std;
Employee::Employee(string name ,int IDNumber ,string department ,string position)
{
name=name;
idnumber=IDNumber;
department=department;
position=position;
}
void Employee::setDepartment(string Department)
{
department=Department;
}
void Employee::setName(string Name)
{
name=Name;
}
void Employee::setPosition(string Position)
{
position=Position;
}
void Employee::setIDNumber(int Number)
{
idnumber=Number;
}
int main()
{
string name;
int IDNumber;
string department;
string position;
const int Item=3;
Employee info1(" ",0," "," ");
Employee info2(" ",0," "," ");
Employee info3(" ",0," "," ");
Employee Info[Item]={info1,info2,info3};
}
for(Employee element:Info)
{
cout<<"please enter your name "<<endl;
cin>>name;
element.setName(name);
cout<<element.getName()<<endl;
cout<<"please enter your department "<<endl;
cin>>department;
element.setDepartment(department);
cout<<element.getDepartment()<<endl;
cout<<"please enter your position"<<endl;
cin>>position;
element.setPosition(position);
cout<<element.getPosition()<<endl;
cout<<"please enter your IDNumber"<<endl;
cin>>IDNumber;
element.setIDNumber(IDNumber);
cout<<element.getIDnumber()<<endl;
}
for(Employee element:Info)
{
cout<<element.getName()<<setw(8)<<element.getDepartment()
cout<<setw(8)<<element.getPosition()<<setw(8)<<element.getIDnumber()<<endl;
}
}
for(Employee element:Info)
element is a copy of an element from Info array. You are busy filling this copy, then it gets discarded. Contents of Info remain unchanged, with empty strings all around.
Make it
for(Employee& element:Info)
Note the ampersand.
Here is my Class code:
class LibItem
{
public:
virtual void PrintDetails() = 0;
void setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
{
Title = setItemTitle;
Author = setItemAuthor;
ReleaseDate = setItemReleaseDate;
Copyright = setItemCopyright;
Genre = setItemGenre;
Status = setItemStatus;
}
void setTitle(string TitleName)
{
Title = TitleName;
}
string getTitle()
{
return Title;
}
void setReleaseDate(string date)
{
ReleaseDate = date;
}
string getReleaseDate()
{
return ReleaseDate;
}
void setAuthor(string AuthorName)
{
Author = AuthorName;
}
string getAuthor()
{
return Author;
}
void setCopyright(string CopyrightDetails)
{
Copyright = CopyrightDetails;
}
string getCopyright()
{
return Copyright;
}
void setGenre(string GenreDetails)
{
Genre = GenreDetails;
}
string getGenre()
{
return Genre;
}
void setStatus(string StatusDetails)
{
Status = StatusDetails;
}
string getStatus()
{
return Status;
}
private:
string Title;
string ReleaseDate;
string Author;
string Copyright;
string Genre;
string Status;
};
I am wanting to place this into a .h file and a .cpp file.
Is the below code correct?
LibItem.cpp:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "LibItem.h"
virtual void LibItem::PrintDetails() = 0;
void LibItem::setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
{
Title = setItemTitle;
Author = setItemAuthor;
ReleaseDate = setItemReleaseDate;
Copyright = setItemCopyright;
Genre = setItemGenre;
Status = setItemStatus;
}
void LibItem::setTitle(string TitleName)
{
Title = TitleName;
}
string LibItem::getTitle()
{
return Title;
}
void LibItem::setReleaseDate(string date)
{
ReleaseDate = date;
}
string LibItem::getReleaseDate()
{
return ReleaseDate;
}
void LibItem::setAuthor(string AuthorName)
{
Author = AuthorName;
}
string LibItem::getAuthor()
{
return Author;
}
void LibItem::setCopyright(string CopyrightDetails)
{
Copyright = CopyrightDetails;
}
string LibItem::getCopyright()
{
return Copyright;
}
void LibItem::setGenre(string GenreDetails)
{
Genre = GenreDetails;
}
string LibItem::getGenre()
{
return Genre;
}
void LibItem::setStatus(string StatusDetails)
{
Status = StatusDetails;
}
string LibItem::getStatus()
{
return Status;
}
};
//---------------------------------------------------------------------------
#pragma package(smart_init)
LibItem.h
//---------------------------------------------------------------------------
#ifndef LibItemH
#define LibItemH
class LibItem
{
public:
virtual void PrintDetails();
void setDetails(string, string, string, string, string, string);
void setTitle(string);
void setReleaseDate(string);
string getReleaseDate();
void setAuthor(string);
string getAuthor();
void setCopyright(string);
string getCopyright();
void setGenre(string);
string getGenre();
void setStatus(string);
string getStatus();
private:
string Title;
string ReleaseDate;
string Author;
string Copyright;
string Genre;
string Status;
};
//---------------------------------------------------------------------------
#endif
Next, if I am wanting to use this .h and .cpp file in a main function, what is the code needed to be able to do this? What are the include statements needed?
Not correct:
virtual void LibItem::PrintDetails() = 0;
The =0 should be inside the class definition (in the header).
To use the class, you need to #include "LibItem.h".
Also, in the header:
#include <string>
and replace occurences of string with std::string.
string parameters should be passed by reference:
void setReleaseDate(const string& date)
instead of
void setReleaseDate(string date)