![]() |
| | #16 | |
| Registered User Join Date: Jan 2008
Posts: 65
| Quote:
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? | |
| Drac is offline | |
| | #17 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Code: *holdPrt[i] = getInfo(); //need to figure out how to declare holdPrt 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
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #18 |
| Registered User Join Date: Dec 2008
Posts: 13
| 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;
}
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
Code: Holding::Holding(std::string tempTitle, int callNumTemp){
title = tempTitle;
*callNumber = callNumTemp;
}
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
Code: Holding::Holding(std::string tempTitle, int callNumTemp){
title = tempTitle;
*callNumber = callNumTemp;
}
|
| Skyy is offline | |
| | #19 | |
| The larch Join Date: May 2006
Posts: 3,082
| Code: Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) : Holding(title, *callNumber) {
std::string author;
author = authorTemp;
}
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
};
Code: Holding::Holding(std::string tempTitle, int callNumTemp){
title = tempTitle;
*callNumber = callNumTemp;
}
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. Quote:
| |
| anon is offline | |
| | #20 | |
| Registered User Join Date: Dec 2008
Posts: 13
| 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:
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;
}
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
| |
| Skyy is offline | |
| | #21 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #22 | |
| Registered User Join Date: Dec 2008
Posts: 13
| Quote:
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() | |
| Skyy is offline | |
| | #23 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #24 |
| Registered User Join Date: Dec 2008
Posts: 13
| This is the function: Code: Book::Book(std::string titleTemp, std::string authorTemp, int callNumtemp) : Holding(title, callNumber) {
author = authorTemp;
}
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;
}
Code: holdPrt[i] = getInfo(); Code: return new Book(titleTemp,authorTemp,callNumTemp); Last edited by Skyy; 12-11-2008 at 04:00 PM. |
| Skyy is offline | |
| | #25 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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...
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
Last edited by Elysia; 12-11-2008 at 03:32 PM. | |
| Elysia is offline | |
| | #26 | |
| Registered User Join Date: Dec 2008
Posts: 13
| Quote:
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
| |
| Skyy is offline | |
| | #27 | |
| The larch Join Date: May 2006
Posts: 3,082
| 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. Quote:
| |
| anon is offline | |
| | #28 | |
| Registered User Join Date: Dec 2008
Posts: 13
| Quote:
![]() 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. | |
| Skyy is offline | |
| | #29 | |
| The larch Join Date: May 2006
Posts: 3,082
| I think this is supposed to look like: Code: void Recording::print(ostream &out){
out << "RECORDING: " << '\"' << title << '\"' << " " << performer << " " << format << callNumber << endl;
}
Code: myrecording->print(std::cout); myrecording->print(csis);
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #30 |
| Registered User Join Date: Dec 2008
Posts: 13
| Good tip Anon. Thanks to everyone who helped, it has been graded and the class is over. You guys rock. |
| Skyy is offline | |
![]() |
| Tags |
| pholymorphism, pointers, virtual |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| template and polymorphism | lehe | C++ Programming | 7 | 06-22-2009 02:41 PM |
| Base-class pointer, accessing object from derived class | Korhedron | C++ Programming | 15 | 09-28-2008 05:30 AM |
| Smart pointer class | Elysia | C++ Programming | 63 | 11-03-2007 07:05 AM |
| Polymorphism Theory Question - Polymorphic Class Definition. | ventolin | C++ Programming | 3 | 10-31-2005 12:05 PM |
| polymorphism | slaveofthenet | C++ Programming | 15 | 07-10-2003 11:50 AM |