str8dn
asked on
Overloading same operators in seperate classes causes "ambiguos operator" error?
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);
}
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);
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are those two files linked somehow? Are in one of those files both headers included?
And *where* does the error occur? :o)
This compiles just fine for me....
>>This compiles just fine for me....
Not really surprising - the above code does not use the operators.
Not really surprising - the above code does not use the operators.
ASKER
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.
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.
ASKER
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
Im using VC++ 6.0, but it is entirely possible I did not to some configuration correctly.
Thanks,
str8dn
No problem for me to use <fstream> on VC++ 6.0 SP5.
ASKER
<fstream> (when not using the .h) screams by causing 'cout' to no longer be recognized.
Is there some more code in this project? It seems like there is a either a constructor
Tag (Category&) or the other way round.
Tag (Category&) or the other way round.
Since you do not have a line
using namespace std;
you need to use
std::cout if I remember correctly.
using namespace std;
you need to use
std::cout if I remember correctly.
ASKER
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
the widdled down code would be easier to decipher...
Thanks,
str8dn
SoIs the above the *exact* code, i.e. the classes are not derived or anything else?
Try to replace <fstring.h> with <fstring>
ASKER
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
The Category::displayString and Category::display methods can be removed without changing the error too.
Thanks,
str8dn
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
#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
<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;
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;
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);
}
#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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I guess <cstring> was the problem?
Are there drawbacks to using namespace std?
Are there drawbacks to using namespace std?
>>Are there drawbacks to using namespace std?
No. At least, not that I ever encountered any.
No. At least, not that I ever encountered any.