C++ Class file linking -


i still new c++, , having trouble making class file , linking main program , implementation file.

here specification file:

#ifndef bookclass_h #define bookclass_h #include<string>  using namespace std;  class book {     private:         string title;       //a string holds book’s title         string author;      //a string holds book’s author         string publisher;   //a string holds book’s publisher         string isbn;        //a string holds book’s isbn number         double price;       //a double holds book’s price         int year;           //an int holds year when book published         int numinstock;     //an int holds number of copies of book     public:         book();                     //constuctor ---- overwritten storebook if getbookinfo operates         void getbookinfo(book &);   //gets information book , calls storebook within store information variables         void storebook(string booktitle, string authorname, string bookpublisher, string bookisbn, double bookprice, int bookyear, int booksinstock);         void displaybookinfo();     //displays contents of bookclass member variables         void checkoutbook();        //subtracts 1 numinstock member variable, tests make sure numinstock not 0         void returnbook()           //adds 1 numinstock member variable         { numinstock++; };         string gettitle()           //returns value in title         { return title; };         int getnuminstock()         //returns value in numinstock         { return numinstock; }; }; #endif // !bookclass_h 

and here implementation file:

#include<iostream> #include<string> #include"bookclass.h"  using namespace std;   //********************************************************************************************************************************************************************* //constuctor - constuctor assign generic values class, used troubleshooting , overwritten if program operates //********************************************************************************************************************************************************************* book::book() {     std::cout << "made constructor!\n\n";     title = "empty";     author = "empty";     publisher = "empty";     isbn = "empty";     price = 0.00;     year = 0000;     numinstock = 0; }  //********************************************************************************************************************************************************************* //asks user enter information 1 book, invokes member function storebook store information in bookclass variable. //********************************************************************************************************************************************************************* void book::getbookinfo(book &book) {     string booktitle, authorname, bookpublisher, bookisbn;     double bookprice;     int bookyear, booksinstock;      cout << "enter book title: ";     getline(cin, booktitle);     cout << "enter author: ";     getline(cin, authorname);     cout << "enter publisher: ";     getline(cin, bookpublisher);     cout << "enter isbn-10 including dashes(ex. 0-00-000000-0): ";     getline(cin, bookisbn);     cout << "enter price: ";     cin >> bookprice;     cout << "enter year: ";     cin >> bookyear;     cout << "enter quantity in stock: ";     cin >> booksinstock;     book::storebook(booktitle, authorname, bookpublisher, bookisbn, bookprice, bookyear, booksinstock); }  //********************************************************************************************************************************************************************* //stores values taken user input class variables //********************************************************************************************************************************************************************* void book::storebook(string booktitle, string authorname, string bookpublisher, string bookisbn, double bookprice, int bookyear, int booksinstock) {     title = booktitle;     author = authorname;     publisher = bookpublisher;     isbn = bookisbn;     price = bookprice;     year = bookyear;     numinstock = booksinstock; }  //********************************************************************************************************************************************************************* //displays contents of bookclass member variables //********************************************************************************************************************************************************************* void book::displaybookinfo() {     cout << "title: "          << title          << "\nauthor: "          << author           << "\npublisher: "          << publisher          << "\nisbn-10: "          << isbn          << "\nprice: "          << price          << "\nyear: "          << year          << "\nquantity in stock: "          << numinstock; }  //********************************************************************************************************************************************************************* //subtracts 1 numinstock member variable, tests make sure numinstock not 0 //********************************************************************************************************************************************************************* void book::checkoutbook() {     if(numinstock <= 0)     {         cout << "error: copies checked out. please select title.\n";         return;     }     else         numinstock--; } 

and main program:

#include"bookclass.h" #include"bookmain.cpp"  using namespace std;  int main() {     book book1;     cout << "before getbookinfo\n";     getbookinfo(book1);     cout << "\nafter getbookinfo\n";      system("pause");     return 0; } 

i sure don't need include string class each file, if don't many more errors. when try run compiler error stating:

error c2352: 'book::storebook' : illegal call of non-static member function

when comment out call (line 38 of implementation file) , run again see if can getbookinfo function this:

1>library test function.obj : error lnk2005: "public: void __thiscall book::checkoutbook(void)" (?checkoutbook@book@@qaexxz) defined in bookmain.obj

1>library test function.obj : error lnk2005: "public: void __thiscall book::displaybookinfo(void)" (?displaybookinfo@book@@qaexxz) defined in bookmain.obj

1>library test function.obj : error lnk2005: "void __cdecl getbookinfo(class book &)" (?getbookinfo@@yaxaavbook@@@z) defined in bookmain.obj

1>library test function.obj : error lnk2005: "public: void __thiscall book::storebook(class std::basic_string,class std::allocator >,class std::basic_string,class std::allocator >,class std::basic_string,class std::allocator >,class std::basic_string,class std::allocator >,double,int,int)" (?storebook@book@@qaexv?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@000nhh@z) defined in bookmain.obj

i know reference variable being passed getbookinfo function not being used (the teacher wants function pass argument), because i'm not sure it, don't see being issue errors. can please advise doing wrong? here part of assignment including format getbookinfo function, should separate rest of implementation file?

member functions
• void storebook(string booktitle, string authorname, string bookpublisher, string bookisbn, double bookprice, int bookyear, int booksinstock)
o stores parameters bookclass member variables
• void displaybookinfo() displays contents of bookclass member variables
• void checkoutbook() subtracts 1 numinstock member variable; tests make sure numinstock not 0
• void returnbook() adds 1 numinstock member variable
• string gettitle() returns value in title
• int getnuminstock() returns value in numinstock
2. create bookmain.cpp test bookclass.
functions:
• void getbookinfo (bookclass &);
o asks user enter information 1 book, invokes member function storebook store information in bookclass variable.
test class , function main program:

thank in advance advice!

book::storebook() member-function. not static function, wrong:

book::storebook(booktitle, authorname, bookpublisher, bookisbn, bookprice, bookyear, booksinstock);

that can changed book.storebook(...).

do not #include"bookmain.cpp". causing linking errors. have clean , rebuild.


Comments