Thread: Pointer and Polymorphism help.

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    I didn't look at the whole thing, but these lines caught my attention
    Quote Originally Posted by Skyy View Post
    Code:
    	
    	Holding *holdPtr[5];
    	
    	for (int i=0;i<5;i++){
    		/*holdPrt[i] = new Holding(); can't do that */
    		*holdPrt[i] = getInfo();  //need to figure out how to declare holdPrt
    First the line you should be able to do the line you commented out, but it isn't necessary. The next line shouldn't compile.
    holdPrt is data type Holding**
    holdPrt[i] is data type Holding* (or *& I guess)
    *holdPrt[i] is data type Holding
    getInfo(); returns type Holding*
    So that line should look like
    holdPrt[i] = getInfo();

    Time for nit picky stuff
    in getInfo()
    return 0;
    Would be better as.
    return NULL;
    That makes it more obvious you are giving back a NULL pointer.

    I guess this is a matter of taste, but you could write the other return much more succinctly as.
    return new Book(titleTemp,authorTemp,callNumTemp);

    One more thing you wouldn't happen to be using Dev-C++ would you?

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    *holdPrt[i] = getInfo();  //need to figure out how to declare holdPrt
    The problem with that is that holdPtr is an array of pointers which haven't been initialized, so you are trying to dereference an uninitialized pointer which will lead to bad things™.
    What you actually want to do is assign the pointer getInfo returns to the array. Then there's no actual need to dereference, so get rid of the "*".

    And just for your pleasure, because I'm picky :
    http://www.research.att.com/~bs/bs_faq2.html#whitespace
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    Thanks guys, I fixed the *holdPtr problem and took advantage of your nit-picking, keep it coming by the way. “Knowledge comes from experience, perfection comes from criticism”.

    But now I’m back to one of my original problems (like that in post 5), where it enters a forced break after inputing all the data and at this line(below) in book.cpp. I don’t really have much confidence in this one since I’m just going by examples off the net (no teacher’s notes on this):
    Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) : Holding(title, *callNumber) {

    Related code...

    Book.cpp
    Code:
    Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) :  Holding(title, *callNumber) {
        	std::string author;
    author = authorTemp;
    }
    Book.h
    Code:
    extern std::string author;  //NEW NEW
    class Book : public Holding {
    public:
    	Book(std::string titleTemp, std::string authorTemp, int callNumTemp);
    	~Book();
    	void print(/*ostream &*/); //ostream is req for project
    };
    #endif
    Holding.cpp
    Code:
    Holding::Holding(std::string tempTitle, int callNumTemp){ 
          title = tempTitle;
    	*callNumber = callNumTemp;
    }
    Holding.h
    Code:
    class Holding{   
    protected:
    	std::string title; //should this be extern?  Tried but error.
    	int* callNumber; 
    public:
         Holding(std::string titleTemp, int callNumTemp);
    	 Holding();
         virtual ~Holding();
         virtual void print(/*ostream &*/) = 0;  
    	 //~Holding();
    };
    #endif
    Holding.cpp
    Code:
    Holding::Holding(std::string tempTitle, int callNumTemp){ 
       	title = tempTitle;
    	*callNumber = callNumTemp;
    }

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) :  Holding(title, *callNumber) {
        std::string author;
        author = authorTemp;
    }
    This creates a temporary string called author and assigns authorTemp to it. Then the string goes out of scope again. Hardly what you want.

    Code:
    extern std::string author;  //NEW NEW
    class Book : public Holding {
    public:
    	Book(std::string titleTemp, std::string authorTemp, int callNumTemp);
    	~Book();
    	void print(/*ostream &*/); //ostream is req for project
    };
    Why do you think you need that global string? Does each Book have an author or all Books authored by the same person?

    Code:
    Holding::Holding(std::string tempTitle, int callNumTemp){ 
        title = tempTitle;
        *callNumber = callNumTemp;
    }
    This again dereferences an uninitialized pointer that doesn't point anywhere in particular. Why do you think callNumber needs to be a pointer in the first place?

    I mean, the assignment is perfectly simple. You declare the three classes, each with the needed data members and a constructor to set those members and a method to print the data, and then you write a main (together with some helper functions) that does what you are asked to do.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    All suggestions applied. But there is something else wrong... As much as I enjoy learning from this thread , I hope this is the last time I ask for help on this. It seems so close...

    Quote Originally Posted by anon View Post
    I mean, the assignment is perfectly simple. You declare the three classes, each with the needed data members and a constructor to set those members and a method to print the data, and then you write a main (together with some helper functions) that does what you are asked to do.
    To answer your extern question, I decided to use 'extern' everywhere I could since I had not learned it in class and wanted to practice. Changed.

    You're right, it is simple. I was expecting to get a simple answer on Saturday to this thread and be done that night... I had no idea how far off I was, I assumed Pointers were going to big part of this since the last project was all about pointers. However, now; with the simplified program, I'm still getting an error.

    This time it's "Unhandled exception at 0x779a42eb in Polymorphism Lab.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0043ec10.." which is the worst yet since I can't get ANY info, no variable info, I don't even know exactly where it breaks... (I assume at the book or holding constructor since it happens after input the data.

    // Book.cpp
    Code:
    #include "book.h"
    using namespace std;
    Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) :  Holding(title, callNumber) {
    	author = authorTemp;
    }
    Book::~Book(){
    	//delete [] author;        
    }
    void Book::print(/*ostream &out*/){
            //cout << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
            //csis << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;   
    }
    // Book.h
    Code:
    #ifndef _BOOK_H
    #define _BOOK_H
    //#include <stdlib.h>
    //#include <iostream>
    //#include <fstream>
    
    #include "holding.h"  //commented out above since holding.h has them.
    
    class Book : public Holding {
          
    protected:
    
    public:
    	std::string author;  
    	Book(std::string titleTemp, std::string authorTemp, int callNumTemp);
    	~Book();
    	void print(/*ostream &*/); //ostream is req for project
    // but for the time being, ignoring to fix other problem.
    };
    #endif
    I also changed holdPtr[] to class Holding in library and library.h since I'll need it for another format other than book (recording). I've changed it back to Book to test but still get the same error.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This does not say much...
    Do you know what the Call stack is? You can use it to backtrack to what function called what function. So you can use it to see which function called the function that threw the exception. That would be very helpful if you could point out the code.
    The current code says pretty much nothing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    Quote Originally Posted by Elysia View Post
    This does not say much...
    Do you know what the Call stack is? You can use it to backtrack to what function called what function. So you can use it to see which function called the function that threw the exception. That would be very helpful if you could point out the code.
    The current code says pretty much nothing.
    I'm not sure what I'm looking at, in fact, I had to Google Call Stack to figure out what you're talking about. Yellow text identifies the stack frame where the execution pointer is currently located. (yellow arrow)

    Code:
     	kernel32.dll!76b242eb() 	
     	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
     	kernel32.dll!76b242eb() 	
    >	msvcr90d.dll!_nh_malloc_dbg_impl(unsigned int 
    nSize=1744959730, int nhFlag=-529697949, int nBlockUse=1, const char * 
    szFileName=0x00000003, int nLine=1569128, int * errno_tmp=0x00000000)  Line 239
     + 0x19 bytes	C++
     	0017f174()	
     	msvcr90d.dll!_CallSettingFrame(unsigned long funclet=1570956, unsigned long 
    pRN=256, unsigned long dwInCode=1569292)  Line 73	Asm
     	msvcr90d.dll!_CallCatchBlock2(EHRegistrationNode * pRN=0x0017f88c, const 
    _s_FuncInfo * pFuncInfo=0x67cedd40, void * handlerAddress=0x67c823bd, int 
    CatchDepth=0, unsigned long NLGCode=256)  Line 512 + 0x11 bytes	C++
     	msvcr90d.dll!CallCatchBlock(EHExceptionRecord * pExcept=0x0017f490, 
    EHRegistrationNode * pRN=0x0017f88c, _CONTEXT * pContext=0x0017f4b0, const 
    _s_FuncInfo * pFuncInfo=0x67cedd40, void * handlerAddress=0x67c823bd, int 
    CatchDepth=0, unsigned long NLGCode=256)  Line 1465 + 0x19 bytes	C++
     	msvcr90d.dll!CatchIt(EHExceptionRecord * pExcept=0x0017f490, 
    EHRegistrationNode * pRN=0x0017f88c, _CONTEXT * pContext=0x0017f4b0, void * 
    pDC=0x0017f464, const _s_FuncInfo * pFuncInfo=0x67cedd40, const _s_HandlerType * 
    pCatch=0x67cedd00, const _s_CatchableType * pConv=0x68075bd4, const 
    _s_TryBlockMapEntry * pEntry=0x67cedd78, int CatchDepth=0, EHRegistrationNode * 
    pMarkerRN=0x00000000, unsigned char IsRethrow=0)  Line 1275 + 0x25 bytes	C++
     	msvcr90d.dll!FindHandler(EHExceptionRecord * pExcept=0x0017f490, 
    EHRegistrationNode * pRN=0x0017f88c, _CONTEXT * pContext=0x0017f4b0, void * 
    pDC=0x0017f464, const _s_FuncInfo * pFuncInfo=0x67cedd40, unsigned char 
    recursive=0, int CatchDepth=0, EHRegistrationNode * pMarkerRN=0x00000000)  Line 774 
    + 0x32 bytes	C++
     	msvcr90d.dll!__InternalCxxFrameHandler(EHExceptionRecord * 
    pExcept=0x0017f490, EHRegistrationNode * pRN=0x0017f88c, _CONTEXT * 
    pContext=0x0017f4b0, void * pDC=0x0017f464, const _s_FuncInfo * 
    pFuncInfo=0x67cedd40, int CatchDepth=0, EHRegistrationNode * 
    pMarkerRN=0x00000000, unsigned char recursive=0)  Line 524 + 0x26 bytes	C++
     	msvcr90d.dll!__CxxFrameHandler3(EHExceptionRecord * pExcept=0x0017f88c, 
    EHRegistrationNode * pRN=0x0017f4b0, void * pContext=0x0017f464, void * 
    pDC=0x0017f88c)  Line 311 + 0x1f bytes	C++
     	ntdll.dll!76e19b99() 	
     	ntdll.dll!76e19b6b() 	
     	ntdll.dll!76dfd205() 	
     	ntdll.dll!76e199f7() 	
     	kernel32.dll!76b242eb() 	
     	kernel32.dll!76b242eb() 	
     	kernel32.dll!76b242eb() 	
     	msvcr90d.dll!_unlock(int locknum=8)  Line 376	C
     	msvcr90d.dll!_unlockexit()  Line 808 + 0x7 bytes	C
     	msvcr90d.dll!_CxxThrowException(void * pExceptionObject=0x0017f81c, const 
    _s__ThrowInfo * pThrowInfo=0x68075bb8)  Line 161	C++
     	msvcr90d.dll!operator new(unsigned int size=3452816848)  Line 64	C++
     	msvcp90d.dll!std::_Allocate<char>(unsigned int _Count=3452816848, char * 
    __formal=0x00000000)  Line 43 + 0x9 bytes	C++
     	msvcp90d.dll!std::allocator<char>::allocate(unsigned int _Count=3452816848)  Line
     151 + 0xb bytes	C++
     	msvcp90d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> 
    >::_Copy(unsigned int _Newsize=3452816845, unsigned int _Oldlen=0)  Line 2103 + 
    0x12 bytes	C++
     	msvcp90d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> 
    >::_Grow(unsigned int _Newsize=3452816845, bool _Trim=false)  Line 2133 + 0x13 
    bytes	C++
     	msvcp90d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> 
    >::assign(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & 
    _Right=<Bad Ptr>, unsigned int _Roff=0, unsigned int _Count=4294967295)  Line 1065 + 
    0xe bytes	C++
     	msvcp90d.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> 
    >::basic_string<char,std::char_traits<char>,std::allocator<char> >(const 
    std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Right=<Bad 
    Ptr>)  Line 734	C++
     	Polymorphism Lab.exe!Book::Book(std::basic_string<char,std::char_traits<char>,std::allocator<char> 
    > titleTemp="niun", std::basic_string<char,std::char_traits<char>,std::allocator<char> > 
    authorTemp="noi", int callNumtemp=222)  Line 8 + 0x6a bytes	C++
     	Polymorphism Lab.exe!getInfo()  Line 46 + 0x88 bytes	C++
     	Polymorphism Lab.exe!main()  Line 19 + 0x5 bytes	C++
     	Polymorphism Lab.exe!__tmainCRTStartup()  Line 582 + 0x19 bytes	C
     	Polymorphism Lab.exe!mainCRTStartup()  Line 399	C
     	kernel32.dll!76b24911() 	
     	ntdll.dll!76dfe4b6() 	
     	ntdll.dll!76dfe489()

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you look down the call stack, you will find this function:
    Polymorphism Lab.exe!Book::Book(std::basic_string<char,std::cha r_traits<char>,std::allocator<char>
    > titleTemp="niun", std::basic_string<char,std::char_traits<char>,std: :allocator<char> >
    authorTemp="noi", int callNumtemp=222) Line 8 + 0x6a bytes C++
    Which is the last function called before the error occurs.
    So, double-click that function and post the code, along with the line that causes the error. Then people will be able to help you more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    This is the function:
    Code:
    Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) :  Holding(title, callNumber) {
    author = authorTemp;
    }
    I believe the problem is in how I'm calling this. The inputs being sent to it are correct (what I typed) but neither the Holding nor the Book::Book here are doing anything, I've tried adding code to change author to "" or title to "" (in Holding) but neither lines run at all and the values for both remain <bad ptr>.

    I don't know if this line calls Holding or the code within in so here is Holding:
    Code:
    Holding::Holding(std::string tempTitle, int callNumTemp){ 
       	title = tempTitle;
    	callNumber = callNumTemp;
    }
    And I call it with this:
    Code:
    holdPrt[i] = getInfo();
    Which returns like so...
    Code:
    return new Book(titleTemp,authorTemp,callNumTemp);
    Last edited by Skyy; 12-11-2008 at 04:00 PM.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you mind giving the full source so I can debug it?
    I don't know what is causing it at this point, but if I had the source, I could easily find it.

    And, again, just a thought... you are dereferencing holdPtr, which was an array of points, which were uninitialized before... did you initialize them?
    And if getInfo returns a pointer (I hope it does?), then how does holdPtr look like? If you dereference the pointer before assignment, then you should get a compile error...
    Last edited by Elysia; 12-11-2008 at 03:32 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    Quote Originally Posted by Elysia View Post
    And, again, just a thought... you are dereferencing holdPtr, which was an array of points, which were uninitialized before... did you initialize them?
    Typo, my error. Changed above. Below is all the code. Recording.cpp/.h can be ignored since I made them after I ran into this problem and they are the same as Book but with two more variables.

    Code:
    // library.cpp
    
    #include <sstream>
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include "library.h"
    #include <string>
    Holding *getInfo();
    using namespace std;
    ofstream csis;
    
    int main(){
        csis.open("csis.dat");
    	
    	Holding* holdPtr[5];
    	
    	for (int i=0;i<5;i++){
    		holdPtr[i] = getInfo();
    	}
    	for (int i=0;i<5;i++){
    		holdPtr[i]->print();
    	}
        csis.close();  
    }
    
    Holding *getInfo(){
        int callNumTemp;
    	char choice;
    	std::string titleTemp;
    	std::string authorTemp;
        std::string formatTemp;  
    	std::string performerTemp;
    	cout << "Enter holdings to be stored in a list: " << endl;
    	cout << "Enter B for book, R for recordings: ";
    	cin >> choice;
    	std::cin.ignore(); //need so NOT to skip title input...
    
    	if (choice == 'b' || choice == 'B') {
    		cout << "Enter book title: ";
    		std::getline(std::cin, titleTemp);
    		cout << "Enter book author: ";
    		std::getline(std::cin, authorTemp);
    		cout << "Enter call number: ";
    		cin >> callNumTemp;
    		return new Book(titleTemp,authorTemp,callNumTemp);
    	} else if (choice == 'r' || choice == 'R') {
    		cout << "Enter recording title: ";
    		std::getline(std::cin, titleTemp);
    		cout << "Enter performer: ";
    		std::getline(std::cin, performerTemp);
    		cout << "Enter format: (L)P, (C)assette, (R)eel_to_reel, (D)isk: ";
    		std::getline(std::cin, formatTemp);
    		if (formatTemp.compare("l") == 0 || formatTemp.compare("L") == 0)
    			formatTemp = "LP";
    		if (formatTemp.compare("c") == 0 || formatTemp.compare("C") == 0)
    			formatTemp = "Cassette";
    		if (formatTemp.compare("r") == 0 || formatTemp.compare("R") == 0)
    			formatTemp = "Reel_to_reel";
    		if (formatTemp.compare("d") == 0 || formatTemp.compare("D") == 0)
    			formatTemp = "Disk";
    		cout << "Enter recording number: ";
    		cin >> callNumTemp;
    		return new Recording(titleTemp,performerTemp,formatTemp,callNumTemp);
    	} else
    		cout << "else called (testing)"<< endl;
    return NULL;
    	
    }
    Code:
    // library.h
    
    #ifndef _LIBRARY_H
    #define _LIBRARY_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    #include "holding.h"
    #include "book.h"
    #include "record.h"
    extern	std::string titleTemp;
    extern  int callNumTemp;
    extern	char choice;
    extern  std::string authorTemp;
    Holding* getInfo(int x);
    	
    #endif
    Code:
    // Book.cpp
    
    #include "book.h"
    using namespace std;
    
    Book::Book(std::string titleTemp, std::string authorTemp, int 
    callNumtemp) :  Holding(title, callNumber) {  //seperated for screen width
    	author = authorTemp;
         
    }
    
    Book::~Book(){
    	//delete [] author;        
    }
    void Book::print(/*ostream &out*/){
            
            //cout << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
            //csis << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;   
    }
    Code:
    // Book.h
    
    #ifndef _BOOK_H
    #define _BOOK_H
    
    //#include <stdlib.h>
    //#include <iostream>
    //#include <fstream>
    
    #include "holding.h"
    
    class Book : public Holding {
          
    protected:
    
    public:
    	std::string author;
    	Book(std::string titleTemp, std::string authorTemp, int callNumTemp);
    	//Book(const Book &);
    	~Book();
    	void print(/*ostream &*/); //ostream is req for project
    // but for the time being, ignoring to fix other problem.
    };
    
    #endif
    Code:
    // holding.cpp
    
    #include "holding.h"
    using namespace std;
    extern ofstream csis;
    
    Holding::Holding(std::string tempTitle, int callNumTemp){ 
       	title = tempTitle;
    	callNumber = callNumTemp;
    }
    
    Holding::~Holding(){  
        //delete [] title;
    }
    Code:
    // holding.h
    
    #ifndef _HOLDING_H
    #define _HOLDING_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Holding{
    protected:
    	//std::string title;
    	//int callNumber; 
    public:
    	std::string title;
    	int callNumber; 
         Holding(std::string titleTemp, int callNumTemp);
    	 Holding();
         //Holding(const Holding &);
         virtual ~Holding();
         virtual void print(/*ostream &*/) = 0;  
    	 //~Holding();
    };
    #endif
    Code:
    // record.cpp
    
    #include "record.h"
    using namespace std;
    Recording::Recording(std::string titleTemp, std::string performerTemp, std::string
     formatTemp,int callNumtemp) :  Holding(title, callNumber) { //seperated for screen width
    	performer = performerTemp;
    	format = formatTemp;
    }
    Recording::~Recording(){
    	//delete [] author;        
    }
    void Recording::print(/*ostream &out*/){
    	//cout << "RECORDING: " << '\"'  << title << '\"' << " "  << performer   << " " << format << callNumber << endl;
    	//csis << "RECORDING: " << '\"'  << title << '\"' << " "  << performer   << " " << format << callNumber << endl;
    }
    Code:
    // record.h
    
    #ifndef _RECORDING_H
    #define _RECORDING_H
    
    //#include <stdlib.h>
    //#include <iostream>
    //#include <fstream>
    
    #include "holding.h"  //commented out above since holding.h has them.
    
    class Recording : public Holding {
          
    protected:
    
    public:
    	std::string performer;
    	std::string format;
    	Recording(std::string titleTemp, std::string performerTemp,std::string formatTemp, int callNumTemp);
    	//Book(const Book &);
    	~Recording();
    	void print(/*ostream &*/); //ostream is req for project
    // but for the time being, ignoring to fix other problem.
    };
    
    #endif

  12. #27
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One major problem is what you pass to the constructor of Holding from the constructor of Book and Recording.

    You should also think about getting rid of the globals in library.h (I don't think you even use them anywhere), and considering why every header should include <stdlib.h>, <iostream> and <fstream>, particularly if they don't need anything from them.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #28
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    Quote Originally Posted by anon View Post
    One major problem is what you pass to the constructor of Holding from the constructor of Book and Recording.
    I think we have a wiener...

    Edit: Yes we do, but is std:string and ofstream not compatible? 'working on getting my csis output line working...

    It may not be writing to my file but it's running! XD You guys rock.

    Edit2: I un-commented out '#include <fstream>' and it works great!
    Last edited by Skyy; 12-11-2008 at 07:39 PM.

  14. #29
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think this is supposed to look like:

    Code:
    void Recording::print(ostream &out){
    	out << "RECORDING: " << '\"'  << title << '\"' << " "  << performer   << " " << format << callNumber << endl;
    }
    And then you pass to it any output stream you like:

    Code:
    myrecording->print(std::cout);
    myrecording->print(csis);
    If you did that you'd see that you don't need csis to be a global.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #30
    Registered User
    Join Date
    Dec 2008
    Posts
    13

    Smile

    Good tip Anon.

    Thanks to everyone who helped, it has been graded and the class is over. You guys rock.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and polymorphism
    By lehe in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2009, 02:41 PM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. polymorphism
    By slaveofthenet in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2003, 11:50 AM

Tags for this Thread