Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

explicit constructor problem

Posted on 2006-04-20
9
Medium Priority
?
413 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:half_life_fool
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:rahul_r_jadhav
ID: 16498452
you can remove the "explict" keyword from the  constructor
0
 
LVL 2

Expert Comment

by:rahul_r_jadhav
ID: 16498469
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
 
LVL 2

Expert Comment

by:rahul_r_jadhav
ID: 16498512
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:half_life_fool
ID: 16498854
sorry rahul_r_jadhav, but that's not really an option...

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

Greetz
0
 

Author Comment

by:half_life_fool
ID: 16498907
What I am asking here in fact is what constructor I need for this error to go away.

Greetz
0
 
LVL 86

Expert Comment

by:jkr
ID: 16499790
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
 

Author Comment

by:half_life_fool
ID: 16500645
 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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16500954
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
 

Author Comment

by:half_life_fool
ID: 16502251
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question