1

We have derived our own Utility String class from RogueWave's RWCString class. Derived class handled numeric to string conversion through << and >> operators.

Below is declaration of this class.

#ifndef UtlString_h
#define UtlString_h 1
#include "platform.h"

#ifdef WIN32
#include "Utilities.h"
#endif

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "rw/cstring.h"
#include "UtlIdentifier.h"

const troint64 BIGBUF_LEN = 56000L ;

class UtlString;
unsigned UtlExport gHashString (const UtlString &);


class  UtlExport UtlString : public RWCString  
{
  public:
      UtlString ();
      UtlString (char c);
      UtlString (char c, size_t N);
      explicit UtlString (const char* cs);
      UtlString (const char *cs, size_t N);
      UtlString (const RWCString& str);
      UtlString (RWCSubString& ss);
      UtlString (float f);
      UtlString (UtlID id);
      UtlString (int i);
      UtlString (RWSize_T ic);
      UtlString (double d);
      UtlString (const UtlString& that)
      {
        this->operator =(that.data());
      }
      virtual ~UtlString();
      operator float () const;
      operator UtlID () const;
      operator int () const;
      float AsFloat () const;
      int AsInt () const;
      double AsDouble () const;
      troint64 AsLong () const;
      trouint64 AsULong () const;
      UtlString& operator << (const int value);
      UtlString& operator << (const troint64 value);
      UtlString& operator << (const float value);
      UtlString& operator << (const double value);
      UtlString& operator << (const char value);
      UtlString& operator << (const RWCString& value);
      UtlString& operator << (const short value);
      UtlString& operator << (const bool value);
      UtlString& operator << (const unsigned char value);
      UtlString& operator << (const unsigned short value);
      UtlString& operator << (const trouint64 value);
      UtlString& operator << (const char *value);
      UtlString& operator =  (const char *value)
      {
        this->remove(0,this->length());
        return *this;
      }
};

I am getting error in below code.

UtlString errMsg;

  errMsg = UtlString("DB Error: ")
    + UtlString(aStatus.message())
    + " ("
    + UtlString(aStatus.errorCode())
    + ") - "
    + UtlString((double)aStatus.vendorError1())
    + ": "
    + UtlString((double)aStatus.vendorError2())
    + ", "
    + UtlString(aStatus.vendorMessage1())
    + ": "
    + UtlString(aStatus.vendorMessage2());

Error: Overloading ambiguity between "UtlString::operator=(const char*)" and "UtlString::operator=(const UtlString&)".

Can you please help me here in resolving this error. I don't understand where UtlString::operator=(const UtlString&) is coming. I have never declared such operator.

Using CC: Sun C++ 5.8 compiler.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
anonymous
  • 1,920
  • 2
  • 20
  • 30
  • Take a look here http://stackoverflow.com/a/2614085/517319 and you will find an explanation. – Raphael Bossek Apr 24 '12 at 12:50
  • Isn't that the default assignment signature generated by the compiler ?? – DumbCoder Apr 24 '12 at 12:53
  • As per the `UtlString::operator=(const UtlString&)` the compiler declared and defined it for you. The *assignment operator* is *implicitly declared*, and will be *implicitly defined* if it is *used* by the program. As of why the error, I would not know, it does look like an error on the side of the compiler, unless you have a typedef like `typedef const char* UtlString` somewhere in the code that you are not showing. – David Rodríguez - dribeas Apr 24 '12 at 13:23
  • Where is `operator+` defined that takes a `UtlString`? – David Rodríguez - dribeas Apr 24 '12 at 13:25
  • BTW, after a quick look at `RWCString`, it does not seem to be designed to be inherited from. You might want to consider your design and not extend that type. – David Rodríguez - dribeas Apr 24 '12 at 13:32
  • Deriving from RWCString class seems to be easiest way to get conversion from numeric to string like double to string and all that. – anonymous Apr 24 '12 at 13:36
  • possible duplicate of [C++ addition overload ambiguity](http://stackoverflow.com/questions/2613645/c-addition-overload-ambiguity) – t0mm13b Dec 27 '12 at 01:44

1 Answers1

-2

errMsg = UtlString... is an initialization, so the compiler feels free to take the copy constructor into consideration. That's where you have your effective assignment operator UtlString::operator=(const UtlString&) from.

johsin18
  • 838
  • 7
  • 10
  • So how to resolve this error. I dont want to make changes in later code. I am open to make changes in UtlString class. – anonymous Apr 24 '12 at 13:16
  • 1
    `errMsg = UtlString(...);` is an assignment, it is only initialization when the variable is *defined* in that statement: `UtlString errMsg = UtlString(...)` – David Rodríguez - dribeas Apr 24 '12 at 13:19