473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ambiguous conversion

Hello,
I'm writing a String class for C++ and I'm getting the following error
message when using operator[]:

test.cpp: In function ‘int main()’:
test.cpp:7: error: ISO C++ says that these are ambiguous, even though
the worst conversion for the first is better than the worst conversion
for the second:
string.h:19: note: candidate 1: char Types::String::operator[](unsigned
int) const
test.cpp:7: note: candidate 2: operator[](char*, int) <built-in>

This is the declaration of the String class (implementation not included):

#ifndef _NIKOLA_STRING
#define _NIKOLA_STRING

namespace Types {
class String {
private:
char *_String;

public:
String();
String(const char *Value);
String(const String &Source);
~String();

bool operator==(const String &Value);
String &operator=(const char *Value);
String &operator+=(const String &Value);
String &operator+=(char Value);
char operator[](unsigned int Index) const;
const String operator+(const String &Value) const;
const String operator+(char Value) const;
operator char*();

int Length() const;
String Trim() const;
};
}

#endif

and this is how I'm using it:

#include "string.h"

using namespace Types;

int main() {
String SomeString = "12345";
char a = SomeString[1];
return 0;
}

Clearly, I want to use my own overloaded operator[], however for some
reason the compiler (and standard) considers this ambiguous. What am I
doing wrong, i.e. how do I make the program use my overloaded
operator[], considering I DON'T want to dispose of my typecast operator
overload

operator char*();

Thanks,
Nikola
Aug 20 '08 #1
8 5043
Nikola wrote:
Hello,
I'm writing a String class for C++ and I'm getting the following error
message when using operator[]:

test.cpp: In function ‘int main()’:
test.cpp:7: error: ISO C++ says that these are ambiguous, even though
the worst conversion for the first is better than the worst conversion
for the second:
string.h:19: note: candidate 1: char Types::String::operator[](unsigned
int) const
test.cpp:7: note: candidate 2: operator[](char*, int) <built-in>

This is the declaration of the String class (implementation not included):

#ifndef _NIKOLA_STRING
#define _NIKOLA_STRING

namespace Types {
class String {
private:
char *_String;

public:
String();
String(const char *Value);
String(const String &Source);
~String();

bool operator==(const String &Value);
String &operator=(const char *Value);
String &operator+=(const String &Value);
String &operator+=(char Value);
char operator[](unsigned int Index) const;
const String operator+(const String &Value) const;
const String operator+(char Value) const;
operator char*();

int Length() const;
String Trim() const;
};
}

#endif

and this is how I'm using it:

#include "string.h"

using namespace Types;

int main() {
String SomeString = "12345";
char a = SomeString[1];
return 0;
}

Clearly, I want to use my own overloaded operator[], however for some
reason the compiler (and standard) considers this ambiguous. What am I
doing wrong, i.e. how do I make the program use my overloaded
operator[], considering I DON'T want to dispose of my typecast operator
overload

operator char*();
Replace

char a = SomeString[1];

with

char a = SomeString::operator[](1);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '08 #2
Victor Bazarov wrote:
>
Replace

char a = SomeString[1];

with

char a = SomeString::operator[](1);

V
test.cpp: In function ‘int main()’:
test.cpp:7: error: ‘SomeString’ is not a class or namespace

char a = String::operator[](1);

also doesn't work. Doesn't the operator[] need to be declared static for
this?
Aug 20 '08 #3
Nikola wrote:
Victor Bazarov wrote:
>>
Replace

char a = SomeString[1];

with

char a = SomeString::operator[](1);

V

test.cpp: In function ‘int main()’:
test.cpp:7: error: ‘SomeString’ is not a class or namespace

char a = String::operator[](1);

also doesn't work. Doesn't the operator[] need to be declared static for
this?
.... that is, along with the fact that it needs 2 arguments in that case
also, in which case I'm better of just making a member function that
does the same thing and takes 1 argument.
Aug 20 '08 #4
Victor Bazarov wrote:
>
Replace

char a = SomeString[1];

with

char a = SomeString::operator[](1);

V
Or you mean

char a = SomeString.operator[](1);

to which I say that a member function beats this syntax by far.

Sorry for multiple responses, it's been a long day.

Nikola
Aug 20 '08 #5
Nikola wrote:
Victor Bazarov wrote:
>>
Replace

char a = SomeString[1];

with

char a = SomeString::operator[](1);

V

Or you mean

char a = SomeString.operator[](1);

to which I say that a member function beats this syntax by far.
Of course, it was an oversight on my part, good that you caught it.
Yes, you're correct, a named member function would be easier on the
eyes, and that's why 'std::string' does *not* define the conversion
function (no operator char const*), but instead has 'data' and 'c_str'
member functions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '08 #6
Nikola wrote:
Hello,
I'm writing a String class for C++ and I'm getting the following error
message when using operator[]:

test.cpp: In function ‘int main()’:
test.cpp:7: error: ISO C++ says that these are ambiguous, even though
the worst conversion for the first is better than the worst conversion
for the second:
string.h:19: note: candidate 1: char Types::String::operator[](unsigned
int) const
test.cpp:7: note: candidate 2: operator[](char*, int) <built-in>

This is the declaration of the String class (implementation not included):

#ifndef _NIKOLA_STRING
#define _NIKOLA_STRING

namespace Types {
class String {
char operator[](unsigned int Index) const;
operator char*();
};
and this is how I'm using it:

#include "string.h"

using namespace Types;

int main() {
String SomeString = "12345";
char a = SomeString[1];
return 0;
}

Clearly, I want to use my own overloaded operator[], however for some
reason the compiler (and standard) considers this ambiguous. What am I
doing wrong, i.e. how do I make the program use my overloaded
operator[], considering I DON'T want to dispose of my typecast operator
overload

operator char*();

Thanks,
Nikola
The problem occurs because int argument to array is not quite the same
as unsigned int, conversion is required , hence ambiguous with char*
conversion.

You can resolve the problem in several ways.
Locally you can call only with unsigned int

SomeString[1u]

You can resolve by changing the signature of array operator

in SomeString ..

char operator[] (int) const;

better, You can resolve it by using a template to allow multiple arg types

template <typename Ioperator[] ( I i)const;

best, In the last version you can use a constraint to restrict to any
integral types. google for enable_if ,is_integral etc..

In this way compiler sees the array operator as a better match because
the argument doesnt require conversion to another type.

regards
Andy Little
Aug 20 '08 #7
kwikius wrote:
Nikola wrote:
>Hello,
I'm writing a String class for C++ and I'm getting the following error
message when using operator[]:

test.cpp: In function ‘int main()’:
test.cpp:7: error: ISO C++ says that these are ambiguous, even though
the worst conversion for the first is better than the worst conversion
for the second:
string.h:19: note: candidate 1: char
Types::String::operator[](unsigned int) const
test.cpp:7: note: candidate 2: operator[](char*, int) <built-in>

This is the declaration of the String class (implementation not
included):

#ifndef _NIKOLA_STRING
#define _NIKOLA_STRING

namespace Types {
class String {
> char operator[](unsigned int Index) const;
> operator char*();
> };
>and this is how I'm using it:

#include "string.h"

using namespace Types;

int main() {
String SomeString = "12345";
char a = SomeString[1];
return 0;
}

Clearly, I want to use my own overloaded operator[], however for some
reason the compiler (and standard) considers this ambiguous. What am I
doing wrong, i.e. how do I make the program use my overloaded
operator[], considering I DON'T want to dispose of my typecast
operator overload

operator char*();

Thanks,
Nikola

The problem occurs because int argument to array is not quite the same
as unsigned int, conversion is required , hence ambiguous with char*
conversion.

You can resolve the problem in several ways.
Locally you can call only with unsigned int

SomeString[1u]

You can resolve by changing the signature of array operator

in SomeString ..

char operator[] (int) const;

better, You can resolve it by using a template to allow multiple arg types
//############################################
template <typename Ioperator[] ( I i)const;
//#################################

should be
template <typename Ichar operator[] ( I i)const;

regards
Andy Little
Aug 20 '08 #8
On 20 Aug., 14:24, Nikola <enlorkREM...@THISgmail.comwrote:
Hello,
I'm writing a String class for C++ and I'm getting the following error
message when using operator[]:

test.cpp: In function ‘int main()’:
test.cpp:7: error: ISO C++ says that these are ambiguous, even though
the worst conversion for the first is better than the worst conversion
for the second:
string.h:19: note: candidate 1: char Types::String::operator[](unsigned
int) const
test.cpp:7: note: candidate 2: operator[](char*, int) <built-in>

This is the declaration of the String class (implementation not included):

#ifndef _NIKOLA_STRING
#define _NIKOLA_STRING

namespace Types {
* class String {
* private:
* *char *_String;

* public:
* *String();
* *String(const char *Value);
* *String(const String &Source);
* *~String();

* *bool operator==(const String &Value);
* *String &operator=(const char *Value);
* *String &operator+=(const String &Value);
* *String &operator+=(char Value);
* *char operator[](unsigned int Index) const;
* *const String operator+(const String &Value) const;
* *const String operator+(char Value) const;
* *operator char*();

* *int Length() const;
* *String Trim() const;
* };

}

#endif

and this is how I'm using it:

#include "string.h"

using namespace Types;

int main() {
* String SomeString = "12345";
* char a = SomeString[1];
* return 0;

}

Clearly, I want to use my own overloaded operator[], however for some
reason the compiler (and standard) considers this ambiguous. What am I
doing wrong, i.e. how do I make the program use my overloaded
operator[], considering I DON'T want to dispose of my typecast operator
overload

operator char*();

Thanks,
Nikola
The problem is your operator char*. In the offending line the compiler
can either convert your Somestring to const and then call operator[].
Alternatively it can convert to char* using your user-supplied
operator and then use simple pointer-arithmetic to get the second
element.
A solution would be to also provide a non-const operator[], but it is
better to get rid of your operator char* which could be quite
dangerous in real code. Yes - I am aware that you don't want to do
that, but it is still a better idea ;-)

/Peter
Aug 20 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philip Pemberton | last post by:
Hi, I've just been trying to get some (pretty badly written) code working on a different compiler. Unfortunately, I've hit a problem. When the code below is compiled under Borland C++ it...
4
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
16
by: REH | last post by:
Can some tell me why the chooses the constructor in class B over operator B in class A? Is this not ambiguous? Thanks. #include <iostream> using namespace std;
1
by: Alan Johnson | last post by:
Consider the following code, with the interesting lines numbered in comments: class A { public : bool f(int level = 1) // line 5 { return true ; }
9
by: xuatla | last post by:
compile error: test1.cpp:21: error: ISO C++ says that `T mtd::CDiffOperator::getdp(const mtd::mVector&, long int, mtd::mBCTYPE) const' and `void mtd::CDiffOperator::getdp(mtd::mVector&, const...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
1
by: Dr Evil | last post by:
There seems to be a bug in managed VC++ when a class is named 'Object'. The code following demonstrates the problem. Note that if the class named 'Object' is renamed ( and references to it changed...
4
by: Coleen | last post by:
Hi All :-) I'm new to this site. I've been trying to convert several .Net 2003 web applications and getting tons of conversion errors. I found this site to help walk me through the...
9
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.