Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 598993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:26:59+00:00 2026-05-13T16:26:59+00:00

Im working on an assignment for one of my classes. Simply I have a

  • 0

Im working on an assignment for one of my classes. Simply I have a GumballMachine class and a bunch of State classes that change the state of the GumballMachine.

Here is the offending code:

class GumballMachine;

class State {
public:
 virtual void insertQuarter() const = 0;
 virtual void ejectQuarter() const = 0;
 virtual void turnCrank() const = 0;
 virtual void dispense() const = 0; 

protected:
 GumballMachine *GBM;
};

class NoQuarterState : public State {
public: 
 NoQuarterState (GumballMachine *GBM) {
  this->GBM = GBM;  
 }

 void insertQuarter() const {
  cout << "You inserted a quarter\n";  
  **this->GBM->QuarterInserted();** // <--- C2027 error on MSDN  
 }
};

Now further below I have defined my GumballMachine class as:

class GumballMachine {
public:
 GumballMachine(int numOfGB) {
  this->noQuarterState = new NoQuarterState(this);
  this->soldOutState = new SoldOutState(this);
  this->hasQuarterState = new HasQuarterState(this);
  this->soldState = new SoldState(this);
  this->winnerState = new WinnerState(this);
  this->count = numOfGB;
  if (0 < numOfGB) {
   this->state = this->noQuarterState;
  }
  else {
   this->state = this->soldOutState;
  }  
 }

        ... more code ...

 void QuarterInserted() {
  this->state = this->hasQuarterState;
 }

 ... more code ...

protected:
 int count;
 NoQuarterState *noQuarterState;
 SoldOutState *soldOutState;
 HasQuarterState *hasQuarterState;
 SoldState *soldState;
 WinnerState *winnerState;
 State *state;
};

Visual Studios was throwing a C2259 and C2027 error but after looking at MSDN I feel like I am doing it right. Maybe I am just tired, but I can’t seem to find the error/see what I did wrong.

Much thanks to any help. 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:26:59+00:00Added an answer on May 13, 2026 at 4:26 pm

    You cannot access any members of GumballMachine before you define the class, so you’ll have to either split your file into several files, each containing one class, or define your NoQuarterState::insertQuarter method after the definition of the GumballMachine class:

    class NoQuarterState : public State {
      public: 
        NoQuarterState (GumballMachine *GBM) {
           this->GBM = GBM;  
        }
    
        void insertQuarter() const; // Declaration only
    };
    
    class GumballMachine {
      public:
        ...
    };
    
    void NoQuarterState::insertQuarter() const {
      cout << "You inserted a quarter\n";  
      this->GBM->QuarterInserted(); // Works now bec. comp. has seen the def.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an assignment and have to create two classes, one represents
I'm working on a homework assignment (a project), for which one criterion is that
I am working on an Operating Systems assignment for one of my summer classes.
I have one class that declares an enumeration type as: public enum HOME_LOAN_TERMS {FIFTEEN_YEAR,
ok so this is the assignment that I'm working on: Implement a class Student
I have an assignment and we have a couple of classes given, one of
I've been working on building several classes which inherit from one base class but
I am working on an assignment that uses a base class bankAccount and two
I'm working on a class assignment that started small, so I had it all
I am working on an assignment where one of the problems asks to derive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.