explicit constructor problem

Hi,


I have the following 'struct':

struct IndexI : public Index
{
  explicit IndexI( const string& v )  { type = itString; strValue = v; }
  explicit IndexI(       string  v )  { type = itString; strValue = v; }
  explicit IndexI( char const *  v )  { type = itString; strValue = v; }

  explicit IndexI( double v )         { type = itDouble; dblValue = v; }

  ...
  a lot more explicit's...
  ...
};

Now I have about this code:

const string abc = "test";

somefunction( IndexI(abc) );

where somefunction is defined as
void somefunction( Index idx );


The error I am getting is:
error C2440: 'type cast' : cannot convert from 'const std::string' to 'IndexI'
  No constructor could take the source type, or constructor overload resolution was ambiguous


What should I do to make it work?


Thanks!
half_life_foolAsked:
Who is Participating?
 
jkrCommented:
It should be possible...

#include <string>

class IndexI {

enum it {

  itString
};

public:

  explicit IndexI( const std::string& v )  { type = itString; strValue = v; }

  std::string strValue;
  it type;
};

void somefunction( IndexI& idx ) {}

int main () {

  const std::string abc = "test";
  IndexI ii(abc);
  somefunction( ii );

  return 0;
}

0
 
rahul_r_jadhavCommented:
you can remove the "explict" keyword from the  constructor
0
 
rahul_r_jadhavCommented:
class X {
public:
   explicit X(int);      //legal
   explicit X(double) {   //legal
      // ...
   }
};

An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above:

void f(X) {}
void g(int I) {
   f(i);      // will cause error
}
void h() {
   X x1(1);      // legal
}
The function call f(i) fails because there is no available implicit conversion from int to X.

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
rahul_r_jadhavCommented:
i.e.

struct IndexI : public Index
{
  IndexI( const string& v )  { type = itString; strValue = v; }
  IndexI(       string  v )  { type = itString; strValue = v; }
  IndexI( char const *  v )  { type = itString; strValue = v; }

  IndexI( double v )         { type = itDouble; dblValue = v; }

  ...

  ...
};
0
 
half_life_foolAuthor Commented:
sorry rahul_r_jadhav, but that's not really an option...

I don't want any implicit conversion to take place.

Greetz
0
 
half_life_foolAuthor Commented:
What I am asking here in fact is what constructor I need for this error to go away.

Greetz
0
 
jkrCommented:
I'd try to provide all possible variants that could be used to construct that object, e.g.

  explicit IndexI( const std::string& v )  { type = itString; strValue = v; }
  explicit IndexI(       std::string&  v )  { type = itString; strValue = v; }
  explicit IndexI( const std::string v )  { type = itString; strValue = v; }
  explicit IndexI(       std::string  v )  { type = itString; strValue = v; }
0
 
half_life_foolAuthor Commented:
 explicit IndexI( const std::string& v )  { type = itString; strValue = v; }
  explicit IndexI( const std::string  v )  { type = itString; strValue = v; }
  explicit IndexI(       std::string& v )  { type = itString; strValue = v; }
  explicit IndexI(       std::string  v )  { type = itString; strValue = v; }

That's what I have now...
So the compiler complains now about:
error C2535: 'QF::IndexI::IndexI(const std::string)' : member function already defined or declared [std::string]
        see declaration of 'QF::IndexI::IndexI' [const std::string]

So I think like... Ok, uncomment one of both... But both solutions don't work.

Is it really not possible to have an explicit constructor for std::string ?

0
 
half_life_foolAuthor Commented:
This works...

Ok I understand also now why:

  explicit IndexI( std::string v )  { type = itString; strValue = v; }

If you add this, it won't work...

So I just removed everything but const string& ... And it works like a charm ;)

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.