Solved

Overloading same operators in seperate classes causes  "ambiguos operator" error?

Posted on 2003-11-17
21
669 Views
Last Modified: 2013-12-14
Can anyone tell me why adding the Tag class definition causes the overload lines of Category
to report an "ambiguos operator" error?

Thanks,

str8dn.

The two actual errors are :
error C2593: 'operator <' is ambiguous
error C2593: 'operator ==' is ambiguous


Here's the code:

#include <cstring>
#include <vector>

#include <iostream>
#include <fstream.h>      // ifstream, ofstream objects


typedef std::string string;
typedef std::vector<string> VString;


class Tag
{
private:
      string nameString;
      
public:
      Tag(string passedName) : nameString(passedName)
      { }

      ~Tag()
      { }


      friend bool operator<(const Tag&, const Tag&);
      friend bool operator==(const Tag&, const Tag&);

};

bool operator <(const Tag& t1, const Tag& t2)
{
      return(t1.nameString < t2.nameString) ? true : false;
}

bool operator ==(const Tag& t1, const Tag& t2)
{
      return(t1.nameString == t2.nameString) ? true : false;
}



//////////// End of "Tag" class definition ////////////////

class Category
{
private:
      string nameString;
      
public:
      Category(string passedName) : nameString(passedName)
      { }

      ~Category()
      { }

      string& name(string& passedName)
      {
            nameString = passedName;
            return nameString;
      }

      string& name()
      {
            return nameString;
      }
      
      const string displayString() {
            string tempDisplayString = nameString;
            
            return tempDisplayString;
      }
      
      void display() {
            cout << "Category display:" <<endl;
            cout << displayString().c_str() <<endl;
      }
      friend bool operator<(const Category&, const Category&);
      friend bool operator==(const Category&, const Category&);

};

bool operator <(const Category& c1, const Category& c2)
{
      return(c1.nameString < c2.nameString) ? true : false;
}

bool operator ==(const Category& c1, const Category& c2)
{
      return(c1.nameString == c2.nameString) ? true : false;
}

//////////// End of "Category" class definition ////////////////

void main (const int argc, const char *argv[])
{
      exit(0);
}
0
Comment
Question by:str8dn
  • 8
  • 6
  • 6
  • +1
21 Comments
 
LVL 13

Assisted Solution

by:SteH
SteH earned 125 total points
ID: 9764495
Perhaps not related but
#include <iostream>
#include <fstream.h>     // ifstream, ofstream objects
old and new style STL? or did you meant
#include <iostream>
#include <fstream>
0
 
LVL 13

Expert Comment

by:SteH
ID: 9764513
Are those two files linked somehow? Are in one of those files both headers included?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9764522
And *where* does the error occur? :o)
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 1

Expert Comment

by:Gratch06
ID: 9764565
This compiles just fine for me....
0
 
LVL 86

Expert Comment

by:jkr
ID: 9764602
>>This compiles just fine for me....

Not really surprising - the above code does not use the operators.
0
 
LVL 1

Author Comment

by:str8dn
ID: 9764608
Sorry, the "ambiguous operator" errors occur at
bool operator <(const Category& c1, const Category& c2)
-and-
bool operator ==(const Category& c1, const Category& c2)

If I remove the Tag definition code (leaving Category definition intact) it seems to be ok.
0
 
LVL 1

Author Comment

by:str8dn
ID: 9764651
For some reason '<fstream>' screams (without the .h)
Im using VC++ 6.0, but it is entirely possible I did not to some configuration correctly.

Thanks,

str8dn
0
 
LVL 13

Expert Comment

by:SteH
ID: 9764699
No problem for me to use <fstream> on VC++ 6.0 SP5.
0
 
LVL 1

Author Comment

by:str8dn
ID: 9764739
<fstream> (when not using the .h) screams by causing 'cout' to no longer be recognized.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9764752
Is there some more code in this project? It seems like there is a either a constructor
Tag (Category&) or the other way round.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9764765
Since you do not have a line
using namespace std;
you need to use
std::cout if I remember correctly.
0
 
LVL 1

Author Comment

by:str8dn
ID: 9764791
There was lots more code, but I still get the errors when I run what I posted, so I figured
the widdled down code would be easier to decipher...

Thanks,

str8dn
0
 
LVL 86

Expert Comment

by:jkr
ID: 9764804
SoIs the above the *exact* code, i.e. the classes are not derived or anything else?
0
 
LVL 13

Expert Comment

by:SteH
ID: 9764866
Try to replace <fstring.h> with <fstring>
0
 
LVL 1

Author Comment

by:str8dn
ID: 9764942
Yes, the above is the exact code.  I run it 'as is' and get both errors.
The Category::displayString and Category::display methods can be removed without changing the error too.

Thanks,

str8dn
0
 
LVL 13

Expert Comment

by:SteH
ID: 9764947
I changed the head to

#include <string> // not <cstring>
#include <vector>

#include <iostream>
#include <fstream>     // not <fstream.h>


using namespace std;

class Tag
{

[]

Now I get errors in
the definitions that private member nameString cannot be accessed
0
 
LVL 13

Expert Comment

by:SteH
ID: 9765013
<cstring> includes <string.h>
This mixes old and new style STL classes. The ambiguity comes somehow from the string definition.
Using the following code at the beggining
#include <string>
#include <vector>

#include <iostream>
#include <fstream>     // ifstream, ofstream objects

typedef std::string string;

class Tag
{
[]
and replacing cout with std::cout and endl with std::endl
lets you compile everything. I am not sure why you are not allowed to use
using namespace std;
0
 
LVL 86

Expert Comment

by:jkr
ID: 9765037
This compiles just fine:

#include <string>
#include <vector>

#include <iostream>
#include <fstream>     // ifstream, ofstream objects

using namespace std;


typedef std::vector<string> VString;


class Tag
{
private:
    string nameString;
   
public:
    Tag(string passedName) : nameString(passedName)
    { }

    ~Tag()
     { }


    friend bool operator<(const Tag&, const Tag&);
    friend bool operator==(const Tag&, const Tag&);

};

bool operator <(const Tag& t1, const Tag& t2)
{
    return(t1.nameString < t2.nameString) ? true : false;
}

bool operator ==(const Tag& t1, const Tag& t2)
{
    return(t1.nameString == t2.nameString) ? true : false;
}



//////////// End of "Tag" class definition ////////////////

class Category
{
private:
    string nameString;
   
public:
    Category(string passedName) : nameString(passedName)
    { }

    ~Category()
     { }

    string& name(string& passedName)
     {
         nameString = passedName;
         return nameString;
    }

    string& name()
     {
         return nameString;
    }
   
     const string displayString() {
         string tempDisplayString = nameString;
         
         return tempDisplayString;
    }
   
     void display() {
         cout << "Category display:" <<endl;
         cout << displayString().c_str() <<endl;
    }
    friend bool operator<(const Category&, const Category&);
    friend bool operator==(const Category&, const Category&);

};

bool operator <(const Category& c1, const Category& c2)
{
    return(c1.nameString < c2.nameString) ? true : false;
}

bool operator ==(const Category& c1, const Category& c2)
{
    return(c1.nameString == c2.nameString) ? true : false;
}

//////////// End of "Category" class definition ////////////////

void main (const int argc, const char *argv[])
{
    exit(0);
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9765053
It seems that the mixed usage of header files was causing the problem.
0
 
LVL 1

Author Comment

by:str8dn
ID: 9765063
Thanks, I guess <cstring> was the problem?  
Are there drawbacks to using namespace std?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9765090
>>Are there drawbacks to using namespace std?

No. At least, not that I ever encountered any.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Losing latter half of command line in Visual Studio C++ online program 10 80
eclipse luna javeEE perspecive missing 5 110
thread-safe code in c++ 2 101
Best IDE for PHP starters 11 123
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question