Here's a really dumb question: How the heck do you declare an array of ptrs??
for a class Holding??
is it:
Holding* ptr[5];
I wouldn't think so...
Here's a really dumb question: How the heck do you declare an array of ptrs??
for a class Holding??
is it:
Holding* ptr[5];
I wouldn't think so...
That is indeed correct.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
that's a shocker...!!! my compiler is telling me that ptr is an undeclared variable though??? any thoughts on that?
That's a shocker too You made some other mistake. You need to post some code that demonstrates the error, and cut out all the unnecessary parts when you do so.that's a shocker...!!! my compiler is telling me that ptr is an undeclared variable though??? any thoughts on that?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That is correct. Post some more of your code or the actual error so we can see what's going on.
EDIT: laserlight beat me to it
Ok I have this...
Code://THIS IS IN MAIN..... Holding* ptr[5]; int i = 0;
Then I have this :
basically I need to take all that input I was trying to figure out cin for....Code://THIS IS A FUNCTION I'M CALLING FROM MAIN.... else if(toLowerChoice = 'r') { cout << "Enter recording title: " << endl; std::cin.ignore('\n', 10); std::cin.getline(recordingTitle, 100); cout << "Enter performer: " << endl; std::cin.getline(recordingPerformer, 100); cout << "Enter format: " << endl; std::cin.getline(recordingFormat, 100); cout << "Enter call number: " << endl; cin >> recordingCallNum; Holding* ptr[i] = new Recording(recordingPerformer, recordingFormat, recordingTitle, recordingCallNum); } return Holding* ptr[i];
Holding is my base class...and I need to declare an array of 5 pointers of type Holding. Then I need to build a new Recording object with all those variables I got from user input...then return the Recording object as a "Holding" object...just in case you were trying to figure out what I was doing....my stuff's a mess....
Are you linking to the holding class correctly? Are you using all the files in a project? Make sure you have #include "class.h" and if it's not in a project you'll have to also add #include "class.cpp"
Okay i know you wanted all the extraneous stuff out but...this is what I did now and it seems to compile..don't know if it works yet...
Code:int main(void) { //need to declare an array of five pointers to Holding Holding* ptr[5]; int i = 0; return 0; } Holding* createNewHold() { Holding* pttt; char holdingChoice, toLowerChoice; char bookTitle[100] , bookAuthor[100], recordingTitle[100], recordingPerformer[100],recordingFormat[100]; int bookCallNum, recordingCallNum; cout << "Enter holdings to be stored in list: " << endl; cout << "\nEnter B for book or R for recording: " << endl; cin >> holdingChoice; toLowerChoice = tolower(holdingChoice); if(toLowerChoice == 'b') { cout << "Enter book title: " << endl; std::cin.ignore('\n', 10); std::cin.getline(bookTitle, 100); cout << "Enter author: " << endl; std::cin.getline(bookAuthor, 100); cout << "Enter call number: " << endl; cin >> bookCallNum; pttt = new Book(bookAuthor, bookTitle, bookCallNum); } else if(toLowerChoice = 'r') { cout << "Enter recording title: " << endl; std::cin.ignore('\n', 10); std::cin.getline(recordingTitle, 100); cout << "Enter performer: " << endl; std::cin.getline(recordingPerformer, 100); cout << "Enter format: " << endl; std::cin.getline(recordingFormat, 100); cout << "Enter call number: " << endl; cin >> recordingCallNum; pttt = new Recording(recordingPerformer, recordingFormat, recordingTitle, recordingCallNum); } return pttt; }
Basically, the problem is one of scope: you need to declare ptr such that it is in scope when you want to return it. There are more problems as well, including possibly illegal syntax (return ptr or return ptr[i], not return Holding* ptr[i]), an probably incorrect condition (toLowerChoice == 'r', not toLowerChoice = 'r'), etc.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You're not calling createNewHold in your main function
You have to declare your createNewHold function before you main function. Either in your header file or right there where all your code is.
LOL hey I figured that out. So here's my next problem: besides not knowing anything....:
when I do this:
I'm basically creating a assigning a derived class to the base class....Code:ptr[i] = createNewHold();
this is what's getting assigned:...Code:pttt = new Book(bookAuthor, bookTitle, bookCallNum);
the bookAuthor and bookTitle are char[100]....when they get assigned values...but my constructors are char*'s is there a way to work with this or do I need to change all the types??
Cause when I went to change my constructor:
where str is a char* as well....I went to change them all to Book(char[] a, char[] str, int x) my compile wigged out saying that i can't use an empty array, empty attribute block not allowed, syntax errors on my identifiers etc....Code:Book::Book(char* a, char* str, int x) : Holding(str, x) { author = a; }
it should work just like that except in your Book constructor your author needs to be a char pointer as well to match a.
Last edited by warfang; 05-09-2007 at 04:56 PM.
my variable author is a char*...
it's saying that the location can't be referenced.... But just to make sure he's how it goes again:
1) I have a base constructor Holding.
2) I have two derived classes Recording and Book
3) I declare an array of five pointers and get user input which I use to create either a new Recording object or a new Book object. I have to assign that object to a Holding object ptr....(this is where I'm having a problem I think)...
4) My constructors take char* and ints but the variables I'm passing are arrays....(I don't know if that's legal).
5) Here's my main and one of my constructors:
Code:#include <string> #include "Recording.h" #include "Book.h" #include "Holding.h" #include "main.h" using namespace std; Holding* createNewHold(); int main(void) { //need to declare an array of five pointers to Holding Holding* ptr[5]; for(int i = 0; i < 1; i++) { ptr[i] = createNewHold(); } ptr[1]->print(); return 0; } Holding* createNewHold() { Holding* pttt; char holdingChoice, toLowerChoice; char bookTitle[100] , bookAuthor[100], recordingTitle[100], recordingPerformer[100],recordingFormat[100]; int bookCallNum, recordingCallNum; cout << "Enter holdings to be stored in list: " << endl; cout << "\nEnter B for book or R for recording: " << endl; cin >> holdingChoice; toLowerChoice = tolower(holdingChoice); if(toLowerChoice == 'b') { cout << "Enter book title: " << endl; std::cin.ignore('\n', 10); std::cin.getline(bookTitle, 100); cout << "Enter author: " << endl; std::cin.getline(bookAuthor, 100); cout << "Enter call number: " << endl; cin >> bookCallNum; pttt = new Book(bookAuthor, bookTitle, bookCallNum); } else if(toLowerChoice = 'r') { cout << "Enter recording title: " << endl; std::cin.ignore('\n', 10); std::cin.getline(recordingTitle, 100); cout << "Enter performer: " << endl; std::cin.getline(recordingPerformer, 100); cout << "Enter format: " << endl; std::cin.getline(recordingFormat, 100); cout << "Enter call number: " << endl; cin >> recordingCallNum; pttt = new Recording(recordingPerformer, recordingFormat, recordingTitle, recordingCallNum); } return pttt; } HERE'S MY CONSTRUCTOR ECT FOR MY BOOK CLASS.... #include "stdafx.h" #include <iostream> #include "Holding.h" #include "Book.h" using namespace std; Book::Book(Book& b): Holding(b) { author = b.getAuthor(); } Book::Book(char* a, char* str, int x) : Holding(str, x) { author = a; } Book::~Book() { delete[]title; delete[]author; } char* Book::getAuthor() { return author; } void Book::print() { cout <<"Title: " << title << endl; cout <<"Call Number: " << callNumber << endl; cout <<"Author: " << author << endl; } //HERE'S MY BASE HOLDING CLASS #include "stdafx.h" #include <iostream> #include "Holding.h" Holding::Holding(Holding& h) { title = h.getTitle(); callNumber = h.getCallNumber(); } Holding::Holding(char* s) { callNumber = 0; title = s; } Holding::Holding(int num) { title = ""; callNumber = num; } Holding::Holding(char* str, int num) { title = str; callNumber = num; } Holding::~Holding() { delete[]title; } char* Holding::getTitle() { return title; } int Holding::getCallNumber() { return callNumber; }