Sponsored Content
Full Discussion: Ambiguity in operator []
Top Forums Programming Ambiguity in operator [] Post 302530190 by 14341 on Monday 13th of June 2011 09:39:30 AM
Old 06-13-2011
Ambiguity in operator []

Hi All,

When i try to compile the following code for 64-bit it works, whereas for 32-bit version, Compiler barfs:

Code:
#include <iostream>
#include <string>

class String {
  public:
    String() { }
    String(const char* src) : myStr(src) { }
    String(const std::string& src) : myStr(src) { }
    String(const String& src) : myStr(src.myStr) { }

    operator const char*() const         { return myStr.c_str(); }
    char& operator[](size_t index)       { return myStr[index]; }
    char operator[] (size_t index) const { return myStr[index]; }

    size_t length() const { return myStr.length(); }
  private:
    std::string myStr;
};

int main() {
    String s("abcd");

    char c = s[1];

    std::cout << c << std::endl;

    return 0;
}

Code:
$ g++ -m32 -Wall -g ambiguous.cc
ambiguous.cc: In function ‘int main()’:
ambiguous.cc:23: 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:
ambiguous.cc:12: note: candidate 1: char& String::operator[](size_t)
ambiguous.cc:23: note: candidate 2: operator[](const char*, int) <built-in>

$ g++ -Wall -g ambiguous.cc
$./a.out
b

1) I cannot modify all the instances like, c = s[1]. There are hundreds of similar things in code.
2) operator const char* is required.

Could someone suggest any work-around, considering the above 2 points.

Thanks,
14341

---------- Post updated at 08:39 AM ---------- Previous update was at 08:38 AM ----------

Hi All,

Overloading the operator[], for all integer types (unsigned/signed) (int; long; long long) solved the problem.

Cheers,
14341
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

And operator

I am trying to check two variables and if both are blank I want to set a flag: the_f3_pid=`rsh $target ps -ef | grep "f3.eab" | awk '{print $2}'` the_f7_pid=`rsh $target ps -ef | grep "f7.eab" | awk '{print $2}'` if ; then y=1 fi I get an error: ./script_name: test: 0403-021 ]... (4 Replies)
Discussion started by: rcarnesiii
4 Replies

2. HP-UX

Or operator with if

hi, i was trying to club to test condition with if. if -o ; then it is giving me error message, i wanted to ask how can we check two condtions with one if. (1 Reply)
Discussion started by: babom
1 Replies

3. Programming

ambiguity in program output

1 #include <fcntl.h> 2 3 main(int argc, char *argv) 4 { 5 char buf; 6 int fd, count = 0; 7 8 if (argc > 1) 9 fd = open(argv, O_RDONLY); 10 else 11 fd = 0; /* Use standard input */ 12 13 while (read(fd, buf, 1) > 0) { 14 if (count < 5) write(1, buf, 1); 15 ... (3 Replies)
Discussion started by: bishweshwar
3 Replies

4. AIX

Fs space ambiguity

Hi All, I found a strange thing in one of our DB server. one of the file system /orcale/ABC/rman size df output showing 100% full eventhough its occupying 2.18MB $ df -mP /oraclev/ABC/rman Filesystem MB blocks Used Available Capacity Mounted on /dev/lv20906 1024.00 1024.00 0.00 100%... (2 Replies)
Discussion started by: ram1729
2 Replies

5. Programming

new operator

Hi, Please clear the 2 questions, 2 Questions, 1) Why the new as a operator? Is there any special reason why it can't be a function like malloc? 2) How are we considering sizeof(),new are as a opearartors? I know + - * / -> , . etc.. are operators, which criteria satisfied by sizeof()... (4 Replies)
Discussion started by: Nagapandi
4 Replies

6. Shell Programming and Scripting

Ambiguity in unicode, Perl CGI

Hello, I was written a cgi with a textarea to save some words from web. I grab and write words like this: $cgiparams{'CONTENTS'} =~ s/\r//g; #$cgiparams{'CONTENTS'} =~ s/á/&aacute;/g; open(TM, ">$editedfilename"); #binmode(TM,... (1 Reply)
Discussion started by: Zaxon
1 Replies

7. UNIX for Dummies Questions & Answers

su with << operator

All, THe below is my script , when i use this i am getting nothing . could any one help me to know what is the use of the << operator below su - $8 << supo echo "exportsph $2 $1 $3 $4" exportsph $2 $1 $3 $4 supo i also tried as individual command su - userid << supo , when i do... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

8. Shell Programming and Scripting

equal to operator

Hi, I have the below script executed arg="dir" if "$arg" = "dir" then echo "true" else echo "false" fi Please let me know what happens in the if command. My output is: dir: dir: No such file or directory false which is not the desired output. When i used test command... (1 Reply)
Discussion started by: anijan
1 Replies

9. UNIX for Dummies Questions & Answers

+= operator

im new to bash scripting and im just using online tutorials and trial and error. i wanted to write a script to read numbers from a file and find their sum: #!/bin/bash theSum=0 for line in $(cat numbers.txt) do let "theSum = theSum + $line" echo "$line" done echo "The sum is... (3 Replies)
Discussion started by: astrolux444
3 Replies

10. Programming

Difference in multiple inheritance and multilevel inheritance: same method name ambiguity problem

Hi, In multi-level inheritance: class A { public: void fun() { cout << "A" << endl; } }; class B : public A { public: void fun() { cout << "A" << endl; } }; class C : public B { }; int main() { C c; c.fun(); // Ans: A } (1 Reply)
Discussion started by: royalibrahim
1 Replies
__gnu_cxx::_Pointer_adapter< _Storage_policy >(3cxx)					      __gnu_cxx::_Pointer_adapter< _Storage_policy >(3cxx)

NAME
__gnu_cxx::_Pointer_adapter< _Storage_policy > - SYNOPSIS
Inherits _Storage_policy. Public Types typedef std::ptrdiff_t difference_type typedef _Storage_policy::element_type element_type" typedef std::random_access_iterator_tag iterator_category" typedef _Pointer_adapter pointer typedef _Reference_type < element_type >::reference reference" typedef _Unqualified_type < element_type >::type value_type" Public Member Functions _Pointer_adapter (element_type *__arg=0) _Pointer_adapter (const _Pointer_adapter &__arg) template<typename _Up > _Pointer_adapter (_Up *__arg) template<typename _Up > _Pointer_adapter (const _Pointer_adapter< _Up > &__arg) operator __unspecified_bool_type () const bool operator! () const reference operator* () const _Pointer_adapter & operator++ () _Pointer_adapter operator++ (int) _Pointer_adapter & operator+= (short __offset) _Pointer_adapter & operator+= (unsigned short __offset) _Pointer_adapter & operator+= (int __offset) _Pointer_adapter & operator+= (unsigned int __offset) _Pointer_adapter & operator+= (long __offset) _Pointer_adapter & operator+= (unsigned long __offset) template<typename _Up > std::ptrdiff_t operator- (const _Pointer_adapter< _Up > &__rhs) const _Pointer_adapter & operator-- () _Pointer_adapter operator-- (int) _Pointer_adapter & operator-= (short __offset) _Pointer_adapter & operator-= (unsigned short __offset) _Pointer_adapter & operator-= (int __offset) _Pointer_adapter & operator-= (unsigned int __offset) _Pointer_adapter & operator-= (long __offset) _Pointer_adapter & operator-= (unsigned long __offset) element_type * operator-> () const _Pointer_adapter & operator= (const _Pointer_adapter &__arg) template<typename _Up > _Pointer_adapter & operator= (const _Pointer_adapter< _Up > &__arg) template<typename _Up > _Pointer_adapter & operator= (_Up *__arg) reference operator[] (std::ptrdiff_t __index) const Friends _Pointer_adapter operator+ (const _Pointer_adapter &__lhs, short __offset) _Pointer_adapter operator+ (short __offset, const _Pointer_adapter &__rhs) _Pointer_adapter operator+ (const _Pointer_adapter &__lhs, unsigned short __offset) _Pointer_adapter operator+ (unsigned short __offset, const _Pointer_adapter &__rhs) _Pointer_adapter operator+ (const _Pointer_adapter &__lhs, int __offset) _Pointer_adapter operator+ (int __offset, const _Pointer_adapter &__rhs) _Pointer_adapter operator+ (const _Pointer_adapter &__lhs, unsigned int __offset) _Pointer_adapter operator+ (unsigned int __offset, const _Pointer_adapter &__rhs) _Pointer_adapter operator+ (const _Pointer_adapter &__lhs, long __offset) _Pointer_adapter operator+ (long __offset, const _Pointer_adapter &__rhs) _Pointer_adapter operator+ (unsigned long __offset, const _Pointer_adapter &__rhs) _Pointer_adapter operator+ (const _Pointer_adapter &__lhs, unsigned long __offset) std::ptrdiff_t operator- (const _Pointer_adapter &__lhs, element_type *__rhs) std::ptrdiff_t operator- (element_type *__lhs, const _Pointer_adapter &__rhs) template<typename _Up > std::ptrdiff_t operator- (const _Pointer_adapter &__lhs, _Up *__rhs) template<typename _Up > std::ptrdiff_t operator- (_Up *__lhs, const _Pointer_adapter &__rhs) _Pointer_adapter operator- (const _Pointer_adapter &__lhs, short __offset) _Pointer_adapter operator- (const _Pointer_adapter &__lhs, unsigned short __offset) _Pointer_adapter operator- (const _Pointer_adapter &__lhs, int __offset) _Pointer_adapter operator- (const _Pointer_adapter &__lhs, unsigned int __offset) _Pointer_adapter operator- (const _Pointer_adapter &__lhs, long __offset) _Pointer_adapter operator- (const _Pointer_adapter &__lhs, unsigned long __offset) Detailed Description template<typename _Storage_policy>class __gnu_cxx::_Pointer_adapter< _Storage_policy > The following provides an 'alternative pointer' that works with the containers when specified as the pointer typedef of the allocator. The pointer type used with the containers doesn't have to be this class, but it must support the implicit conversions, pointer arithmetic, comparison operators, etc. that are supported by this class, and avoid raising compile-time ambiguities. Because creating a working pointer can be challenging, this pointer template was designed to wrapper an easier storage policy type, so that it becomes reusable for creating other pointer types. A key point of this class is also that it allows container writers to 'assume' Alocator::pointer is a typedef for a normal pointer. This class supports most of the conventions of a true pointer, and can, for instance handle implicit conversion to const and base class pointer types. The only impositions on container writers to support extended pointers are: 1) use the Allocator::pointer typedef appropriately for pointer types. 2) if you need pointer casting, use the __pointer_cast<> functions from ext/cast.h. This allows pointer cast operations to be overloaded is necessary by custom pointers. Note: The const qualifier works with this pointer adapter as follows: _Tp* == _Pointer_adapter<_Std_pointer_impl<_Tp> >; const _Tp* == _Pointer_adapter<_Std_pointer_impl<const _Tp> >; _Tp* const == const _Pointer_adapter<_Std_pointer_impl<_Tp> >; const _Tp* const == const _Pointer_adapter<_Std_pointer_impl<const _Tp> >; Definition at line 285 of file pointer.h. Author Generated automatically by Doxygen for libstdc++ from the source code. libstdc++ Tue Nov 27 2012 __gnu_cxx::_Pointer_adapter< _Storage_policy >(3cxx)
All times are GMT -4. The time now is 05:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy