Calling Polymorphic Function

This is a discussion on Calling Polymorphic Function within the C++ Programming forums, part of the General Programming Boards category; I am getting an error compiling the following code. Beverage is a private member of type pointer to BeerScore. I ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Calling Polymorphic Function

    I am getting an error compiling the following code.
    Beverage is a private member of type pointer to BeerScore.
    I create a new AleScore and assign it to Beverage (AleScore inherits from BeerScore).
    I am getting an error calling Beverage.Read(); immediately after.
    /home/curlious/assignment7/src/beveragecreator.cpp:54: error: request for member `Read' in `((BeverageCreator*)this)->BeverageCreator::Beverage', which is of non-class type `BeerScore*'

    Code:
    #include "beveragecreator.h"
    #include <stdlib.h>
    #
    #include "beerscore.h"
    #include "alescore.h"
    #include "lagerscore.h"
    
    BeverageCreator::BeverageCreator()
    {
       
    }
               
    BeerScore * BeverageCreator::CreateBeverage(vector<string>& BeverageMessage)
    {  
       vector<string> Beverages;
       Beverages.push_back("Ale"); Beverages.push_back("Lager"); Beverages.push_back("Malt Liquor"); Beverages.push_back("Wine Cooler");
       
       int TypeIndex = 0;
       
       vector<string>::iterator TypeLookup;
       for (TypeLookup = Beverages.begin(); TypeLookup != Beverages.end(); TypeLookup++)
       {
          
          if (BeverageMessage[0] == *TypeLookup)
             break;
          TypeIndex++;
       }
       
       BeverageMessage.erase(BeverageMessage.begin());
       
       switch (TypeIndex)
       {   
           case 0:
              Beverage = new AleScore();
    	  Beverage.Read(BeverageMessage); //problem here
              return Beverage;
           case 1:
             Beverage = new 
                LagerScore(BeverageMessage[2],atoi(BeverageMessage[3].chars()),atoi(BeverageMessage[4].chars()),atoi(BeverageMessage[5].chars()));
             return Beverage;
           case 2:   
           case 3: 
           default:
               break;
       }
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Given that Beverage is a pointer, use the -> operator.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    doh Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. implementing a polymorphic function in C++
    By coletek in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2009, 03:13 PM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 06:32 PM

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