Ok I am trying to port my vending machine program from Java to C++, this isn't going a smoothly as I had planned however. And I get the following errors whenever I try to use cout.
C2872: 'cout' : ambiguous symbol
C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator
<char> >' (or there is no acceptable conversion)
I have to files a header file called classes.h and main file called vmachine.cpp. I am using MSVC++ 6. Here is the code respectively.
C2872: 'cout' : ambiguous symbol
C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator
<char> >' (or there is no acceptable conversion)
I have to files a header file called classes.h and main file called vmachine.cpp. I am using MSVC++ 6. Here is the code respectively.
Code:
/*This file contains all the classes necessary to
*operate the main vending machine program
*/
#include <iostream>
#include <string>
using namespace std;
class Bank {
public:
Bank();
~Bank() {}
int addCred(int addCred) {
currCred += addCred;
totalCred += addCred;
return currCred;
}
int giveChange(int deductCred){
currCred -= deductCred;
totalCred -= deductCred;
return currCred;
}
void updateCred(int cred) { currCred = cred; }
int getCred() { return currCred; }
private:
int currCred;
int totalCred;
};
Bank::Bank() {
currCred = 0;
totalCred = 2000;
}
class Display {
public:
Display();
~Display() {}
void showDisplay(string displayText) {
text = displayText;
cout<<"DISPLAY:: " <<text;
}
void clear() {
text = "READY";
showDisplay(text);
}
private:
string text;
};
Display::Display() {}
class Keypad {
public:
Keypad();
~Keypad() {}
string getCode() {
// cin.getline((char*)code.data(), 10, '\n');
strupr((char*)code.data());
return code;
}
private:
string code;
};
Keypad::Keypad() {}
class Product{
public:
// default constructor (an added benefit is that it can
// be initalized with a string literal)
Product(const char* itsName = "", int itsPrice = 0):
name(itsName), price(0)
{}
// normal constructor
Product(const string& itsName, int itsPrice):
name(itsName), price(itsPrice) {}
// if your destructor doesn't need to do anything,
// you don't need to implement one
// since these functions don't modify the object (and shouldn't), mark them as const
const string& getName() const {return name;}
int getPrice() const {return price;}
private:
string name;
int price;
};
class Tray{
public:
Tray(const char* itsCode = ""):
code(itsCode), prod()
{}
Tray(const string& itsCode, const Product& trayItem):
limit(20), quantity(20), code(itsCode), prod(trayItem)
{}
// I'm guessing that you don't need a deconstructor here, either
const string& getCode() const {return code;}
const Product& getProduct() const {return prod;}
int getQuant() const {return quantity;}
void dispense() {--quantity;}
private:
int limit;
int quantity;
string code;
Product prod;
};
Code:
#include <iostream.h>
#include <string>
#include "classes.h"
using namespace std;
Tray *tray_prod_init() {
Tray *t_array = new Tray[20];
//Set up List of Products
Product mars("Mars",40);
Product Snickers("Snickers",40);
Product Wispa("Wispa",35);
//Set up List of trays
Tray A1("A1",mars);
Tray A2("A2",Snickers);
Tray A3("A3",Wispa);
//Assign position in array for each tray
t_array[0] = A1;
t_array[1] = A2;
t_array[2] = A3;
return t_array;
}
void interface1(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad) {
}
void interface2(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad) {
}
void interface3(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad) {
}
void main() {
//Create objects needed
Tray* tList = tray_prod_init(); // use tList
Bank* moneyBag = new Bank();
Display* display = new Display();
Keypad* keypad = new Keypad();
//Run the interface menu
interface1(tList,moneyBag,display,keypad);
//Delete objects
delete[] tList; // MAKE SURE YOU DO THIS WHEN YOU'RE DONE!!!
delete moneyBag;
delete display;
delete keypad;
}