0

I basically do not know how to ask this question, I am fairly new to c++...anyway my problem is, I am trying to create this vendingmachine class and this user class, the user needs to have access to the insertCoins method and makePurchase methods from vendingMachine.cpp I tried creating an instance of the class vending machine inside the method calls in user.cpp as it is here, but when I try to list the items in vending machine obviously the list is untouched because theinstance I create inside the method calls in user.cpp are just temporary...how would I get a global instance so that I use it within user.cpp while using it within vending machine inside main.cpp...

#include "user.h"
#include "VendingMachine.h"

user::user(){
}

user::~user(){

}

void user::makePurchase(int choice){
    VendingMachine vm;
    vm.purchaseProduct(choice);
}

void user::insertCoins(double coin){
    VendingMachine vm;
    vm.insertCoins(coin);
}

~~~~~~~~~~~~~

#include "VendingMachine.h"
#include "machineOperator.h"
#include "user.h"
using namespace std;

int main(){

VendingMachine vm = VendingMachine();
user u = user();
vm.listProducts();
cout << endl;
u.insertCoins(1.0);
u.insertCoins(1.0);
u.makePurchase(2);
vm.listProducts();
cout << endl;

return 0;

 }

~~~~~~~~~~~~~~~~~~~

    /*
 * user.h
 *
 *  Created on: Jan 12, 2014
 *      Author: Andrey
 */

#ifndef USER_H_
#define USER_H_

class user {

public:
    user();
    ~user();
    void makePurchase(int);
    void insertCoins(double);
};

#endif /* USER_H_ */

2 Answers 2

4

It is natural to assume that a user can purchase from and insert coin to many different vending machines.

void user::makePurchase (VendingMachine &vm, int choice)
{
    vm.purchaseProduct(choice);
}

void user::insertCoins (VendingMachine &vm, double coin)
{
    vm.insertCoins(coin);
}
Sign up to request clarification or add additional context in comments.

13 Comments

I tried this but it gives me this..."prototype for 'void user::makePurchase(VendingMachine&, int)' does not match any in class 'user' "
@AndreyArias You have to add it your user declaration (user.h), as you must have done with all your other functions.
@AndreyArias: I am assuming the first code snippet in your question is the header #include "machineOperator.h". You included the header files: #include "VendingMachine.h" and #include "user.h" in the main.cpp too. Remove those from there and it will work.
@AndreyArias: As I think this is not really an answer (as long as it is not the solution) and I don't want to mess it up as comment... here is it: pastebin.com/yjEZwTnq
Use the forward declaration class VendingMachine; before your class user {...};.
|
0

Use a pointer to the VendingMachine in your user and pass it in to the constructor.

user.h

class VendingMachine;

class User {
    private:
        VendingMachine* vm;
}

user.cc

#include "user.h"
#include "vendingmachine.h"

User::User(VendingMachine* vm): vm(vm) {}

void User::makePurchase(int choice){
    vm->purchaseProduct(choice);
}

void User::insertCoins(double coin){
    vm->insertCoins(coin);
}

main.cc

#include "VendingMachine.h"
#include "machineOperator.h"
#include "user.h"
using namespace std;

int main(){

    VendingMachine vm = VendingMachine();
    User u = User(&vm);
    vm.listProducts();
    cout << endl;
    u.insertCoins(1.0);
    u.insertCoins(1.0);
    u.makePurchase(2);
    vm.listProducts();
    cout << endl;

    return 0;

}

1 Comment

user534498's reply probably makes more sense with passing the vending machine to the methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.