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...
Printable View
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.
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 :p 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.Quote:
that's a shocker...!!! my compiler is telling me that ptr is an undeclared variable though??? any thoughts on that?
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.
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.
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;
}
>> author = a;
You cannot assign C style strings. You have to allocate memory for author and then use strcpy to copy the string.
You can't assign a pointer to another pointer. You can assign a pointer to a reference or a value but then you wouldn't be able to pass in a pointer. At least I think. Also, in your destructor you're trying to delete author. You can only use keyword delete when you allocate memory with keyword new.
I don't think you should assign author to the reference of "a" though. When your program goes through the destructors you'll most likely get a segmentation fault from trying to access inaccessible memory.Code:char * author;
author = new char[x]; //allocating x amount of chars
delete [] author;
You can keep what you're doing now and just do this:
Code:int length = strlen(a);
author = new char[length + 1];
strcpy(author, a);
author[length] = '\0'; //null character, you actually don't need this, more of an FYI
Code:if (toLowerChoice == 'b')
{
pttt = new ...
}
else if (toLowerChoice == 'r')
{
pttt = new ...
}
// What if neither of the conditions are met...? What will you be returning?
return pttt; // could be anything
// you could try initializing 'pttt' like so,
Holding * pttt = 0;
// or you could try...
switch (toLowerChoice)
{
case 'r':
{
// ...
break;
}
case 'b':
{
// ...
break;
}
default: // if the user refuses to cooperate
{
pttt = NULL; // pttt = 0;
}
};
return pttt;
// you could also, place within a do{}while until conditions are met
do
{
cout << "\nEnter B for book or R for recording: " << endl;
cin >> holdingChoice;
switch(toLowerChoice)
{
// ...
}
}
while (pttt == NULL);
return pttt;
> You can't assign a pointer to another pointer.
Not true.
> You cannot assign C style strings
Oh, but you can.
Nevertheless, it's bad practice, especially in C++. Use string objects.