pointers

This is a discussion on pointers within the C++ Programming forums, part of the General Programming Boards category; Here's a really dumb question: How the heck do you declare an array of ptrs?? for a class Holding?? is ...

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    pointers

    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...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    That is indeed correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    that's a shocker...!!! my compiler is telling me that ptr is an undeclared variable though??? any thoughts on that?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    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

  6. #6
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Ok I have this...

    Code:
    //THIS IS IN MAIN.....
    	Holding* ptr[5];
    	int i = 0;

    Then I have this :

    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];
    basically I need to take all that input I was trying to figure out cin for....

    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....

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    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"

  8. #8
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    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;
    
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    You're not calling createNewHold in your main function

  11. #11
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Quote Originally Posted by warfang View Post
    You're not calling createNewHold in your main function
    When I do this in my main:

    Code:
     
    Holding* ptr[5];
    	
    	for(int i = 0; i < 5; i++)
    	{
    		ptr[i] = createNewHold();
    	}
    	return 0;
    It says that createNewHold() identifier....not found....

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    You have to declare your createNewHold function before you main function. Either in your header file or right there where all your code is.

  13. #13
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Quote Originally Posted by warfang View Post
    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:

    Code:
     ptr[i] = createNewHold();
    I'm basically creating a assigning a derived class to the base class....

    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:

    Code:
    Book::Book(char* a, char* str, int x) : Holding(str, x)
    {
    	author = a;
    }
    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....

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    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.

  15. #15
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Quote Originally Posted by warfang View Post
    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;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21