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
strcpy(9F)						   Kernel Functions for Drivers 						strcpy(9F)

NAME
strcpy, strlcat, strlcpy, strncat, strncpy, strspn - String operations. SYNOPSIS
#include <sys/ddi.h> char *strcpy(char *dst, const char *src); size_t strlcat(char *dst, const char *src, size_t dstsize); size_t strlcpy(char *dst, const char *src, size_t dstsize); char *strncat(char *restrict s1, const char *restrict s2, size_t n); char *strncpy(char *dst, const char *src, size_t n); size_t strspn(const char *s1, const char *s2); INTERFACE LEVEL
Solaris DDI specific (Solaris DDI). PARAMETERS
dst, src Pointers to character strings. s1, s2 Pointers to character strings. n Count of characters to be copied. DESCRIPTION
The arguments dst, src, s1 and s2 point to strings. The strcpy(), strlcpy(), strncpy(), strlcat() and strncat() functions all alter their first argument. These functions do not check for overflow of the array pointed to by the first argument. strcpy() The strcpy() function copies characters in the string src to dst, terminating at the first null character in src, and returns dst to the caller. No bounds checking is done. strncpy() The strncpy() function copies src to dst, null-padding or truncating at n bytes, and returns dst. No bounds checking is done. strlcpy() The strlcpy() function copies a maximum of dstsize-1 characters (where dstsize represents the size of the string buffer dst) from src to dst, truncating src if necessary. The result is always null-terminated. The function returns strlen(src). Buffer overflow can be checked as follows: if (strlcpy(dst, src, dstsize) >= dstsize) return (-1); strncat() The strncat() function appends a maximum of n characters. The initial character of s2 overrides the null character at the end of s1. strlcat() The strlcat() function appends a maximum of (dstsize- strlen(dst)-1) characters of src to dst (where dstsize represents the size of the string buffer dst). If the string pointed to by dst contains a null-terminated string that fits into dstsize bytes when strlcat() is called, the string pointed to by dst is a null-terminated string that fits in dstsize bytes (including the terminating null character) when it completes, and the initial character of src overrides the null character at the end of dst. If the string pointed to by dst is longer than dstsize bytes when strlcat() is called, the string pointed to by dst is not changed. The function returns min{dst- size,strlen(dst)}+strlen(src). Buffer overflow can be checked as follows: if (strlcat(dst, src, dstsize) >= dstsize) return -1; strspn() The strspn() function returns the length of the initial segment of string s1 that consists entirely of characters from string s2. RETURN VALUES
strcpy(), strncat() and strncpy() return dst. For strlcat(), strlcpy() and strspn(), see the Description section. CONTEXT
These functions can be called from user or interrupt context. SEE ALSO
strlen(9F), strcmp(9F), bcopy(9F), ddi_copyin(9F) Writing Device Drivers SunOS 5.10 7 Sep 2004 strcpy(9F)
All times are GMT -4. The time now is 10:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy