Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'cout' ambiguous symbol???

Status
Not open for further replies.

paulstat

Programmer
Jan 7, 2002
2
GB
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.
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<<&quot;DISPLAY:: &quot; <<text;
    }
	void clear() {
	   text = &quot;READY&quot;;       
       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 = &quot;&quot;, 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 = &quot;&quot;):
		  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 &quot;classes.h&quot;
using namespace std;

Tray *tray_prod_init() {

	Tray *t_array = new Tray[20];

    //Set up List of Products

    Product mars(&quot;Mars&quot;,40);
    Product Snickers(&quot;Snickers&quot;,40);
    Product Wispa(&quot;Wispa&quot;,35);   

    //Set up List of trays
    Tray A1(&quot;A1&quot;,mars);
    Tray A2(&quot;A2&quot;,Snickers);
    Tray A3(&quot;A3&quot;,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;

}
 
With visual c++ you have a few options... if you create the project without any support for MFC (Console app) then the above code SHOULD work fine.

Second option is to comment out the include iostream.h line

The next option is to use:

using namespace std;


Any of those three should clear it up.

Matt
 
Using the namespace(std::) worked best for me. Commenting out iostream still causes problems if you have it in other classes that are included and can just get plain messy. Especially if you have code that you want to be able to port to non MFC applications or vice versa. MYenigmaSELF:-9
myenigmaself@yahoo.com
 
I've seen this problem before. I know for a fact that this error occurs when you use BOTH <iostream.h> and <iostream> !!! Do not mix the two. Use one or the other, but NEVER, EVER both :)

~Spaniard
 
I would go the route MyEnigmaSelf suggested... dont bother with including iostream.h or iostream, instead just use

std::cout<<&quot;message\n&quot;;

Works fine for me too :p

Matt
 
I also found that if you comment out the &quot;using namespace std;&quot; line right below the includes this fixed the problem as well, without using the scope resolution operator, but I would still go with the std::cerr<< or std::cout<<, for reasons stated above. This is just a side note. MYenigmaSELF:-9
myenigmaself@yahoo.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
Your code is a bit unclear, but one reason for the &quot;ambiguous&quot; error is that the compiler thinks the cout command is being multiply defined. This can happen if header files are included more than once.
Unlike Java, the header file is where the class definition is declared ( a bit like an interface). It is included in every program that nees to use the classes or datatypes defined therein.
To avoid your classes being defined every time the compiler pulls in your header file, you usually put the following two lines at the very top of your header file:

#ifndef MY_HEADER_FILE_H_DEFINED
#define MY_HEADER_FILE_H_DEFINED

and place the following line at the bottom of your header file

#endif // MY_HEADER_FILE_H_DEFINED

where MY_HEADER_FILE is the name of the header file (without the .h)

All your code, including #include statements, should sit between these two bits of code.

In this way, the declaration is available to the compiler when compiling your cpp code, but the datatypes will only be defined once, including any include files that are inside your header file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top