'operator >>' is ambiguous

Discussion in 'C++' started by John M, Jun 20, 2004.

  1. John M

    John M Guest

    Can someone tell how to fix my visual c++ so it can discard this error
    message.
    I get this error message
    error C2593: 'operator >>' is ambiguous
    But I do not when I am on my mandrake linux platform. Why is this, a bug in
    visual c++ ?

    John J
     
    John M, Jun 20, 2004
    #1
    1. Advertisements

  2. John M

    David Harmon Guest

    On Sat, 19 Jun 2004 19:41:33 -0400 in comp.lang.c++, "John M"
    Possibly a bug in VC++. Possibly a bug in g++.
    How the heck could anybody guess when your code is secret?
     
    David Harmon, Jun 20, 2004
    #2
    1. Advertisements

  3. Post the code!!

    My guess is that you are mixing correct headers (like <string>) with
    incorrect headers (like <iostream.h>). In other words your code is at fault
    not g++, or VC++. No C++ headers (except those that C++ gets from C) have a
    ..h

    #include <iostream.h> // wrong
    #include <iostream> // right

    #include <fstream.h> // wrong
    #include <fstream> // right

    #include <string.h> // OK, C string handling
    #include <string> // OK, C++ string handling

    Gosh my psychic powers are good today.

    john
     
    John Harrison, Jun 20, 2004
    #3
  4. "John M" typed:

    I think it's a bug in VC++/6.0, though I may be wrong. I overloaded
    the insertion operator for my own class, Questions, once. I placed a
    number of such objects in a "list" container, declared an iterator,
    moved it to point to the object that I wanted it to, dereferenced it,
    and called

    cout << *iter;

    g++ didn't complain. VC++/6.0 flagged it as ambiguous.
     
    Ayaz Ahmed Khan, Jun 20, 2004
    #4
  5. John M

    John M Guest

    #include <iostream>
    #include<conio.h>
    using namespace std;

    class Equipment {
    protected:
    float price;

    public:
    Equipment(float p)
    { price = p;}

    virtual void display() const { }
    virtual void read() { }
    } ;

    class Printer : public Equipment {
    private:
    char* type;
    int speed;

    public:
    Printer(const char s[] = "", const float p = 0, const int ppm = 0) :
    Equipment(p)
    { type = new char[strlen(s)+1];
    if (type == NULL) { cout << "Out of memory\n"; exit(0); }
    strcpy(type,s);
    speed = ppm;}

    friend ostream& operator << (ostream& out, const Equipment* e);
    friend istream& operator >> (istream& in, Equipment& e);

    /*Printer& operator = (const Printer& p)
    { if (this == &p) return *this;
    delete [] type;
    strcpy(type, p.type);
    return *this; } */

    void read()
    { cout << " Enter printer type : "; cin >> type;
    cout << " Enter printer speed: "; cin >> speed;
    cout << " Enter printer price: "; cin >> price;
    cout << endl;}

    void display() const
    { cout << " Printer type: " << type << endl;
    cout << " Printer speed: " << speed << endl;
    cout << " Printer price: " << price << endl << endl; }

    ~Printer()
    { delete [] type; }
    } ;



    ostream& operator << (ostream& out, const Equipment* e) //for cout
    { e->display(); return out;}

    istream& operator >> (istream& in, Equipment& e) // for cin
    { e.read(); return cin;}

    int main()
    { cout << endl << endl;
    Equipment* dataBase[10]; int cnt = 0;

    Printer p;

    cout << " --- Reading Objects --- " << endl;
    cin >> p;
    dataBase[cnt++] = &p;


    cout << " --- Displaying Objects --- " << endl;
    for (int i=0; i < cnt; i++)
    cout << dataBase;
    getch();
    return 0;

    }
     
    John M, Jun 21, 2004
    #5


  6. It compiles without problems on my VC++ 6.

    I can see a few issues with the code but nothing that would cause it to fail
    to compile.

    Maybe you don't have the latest service pack for the compiler, or maybe your
    installation or project settings are incorrect. Whatever it is it not a C++
    issue, try a VC++ 6 group, e.g.
    john
     
    John Harrison, Jun 21, 2004
    #6
  7. "conio.h"??? There is no such standard header.

    -- --
    To iterate is human, to recurse divine.
    -L. Peter Deutsch
    -- --
     
    Prateek R Karandikar, Jun 21, 2004
    #7
  8. There's no guarantee that <iostream> includes <istream>, which is where
    the overloads for operator>> are actually declared. Add <istream> (and
    <ostream>) and see what happens.
     
    Richard Herring, Jun 22, 2004
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.