C ++ operator not found

Can someone please tell me why this is not working? I got the impression that C ++ automatically passes a link to the result of the return-by-value function to the constructor, but it complains that no matching statement was found.

class bucket_string { public: bucket_string(); bucket_string(bucket_string & rhs); bucket_string & operator=(bucket_string & rhs); virtual ~bucket_string(); bucket_string substr(iterator start, iterator end){ bucket_string b(str); return b; } }; bucket_string bs("the quick brown fox jumps over the lazy dog"); bucket_string bs1 = bs.substr(bs.begin(), bs.end()); 

returns the following error:

 error: no matching function for call to 'bucket_string::bucket_string(bucket_string)' note: candidates are: bucket_string::bucket_string(bucket_string&) bucket_string::bucket_string() 
+4
source share
3 answers

In C ++, temporary values ​​cannot be bound to non-constant references.

Your function bucket_string substr(iterator start, iterator end) returns a temporary one, and your constructor / assignment operator accepts a non-constant reference as a parameter, hence your problem.

Thus, you need to add the missing const specifier to your constructor and assignment operator. For instance:

 bucket_string(const bucket_string& rhs); bucket_string& operator=(const bucket_string& rhs); 

Here is an interesting discussion on the topic for a better understanding.

On the side of the note, and if C ++ 11 is an option, you can also make your class movable. This will transfer the internal resources of your temporary object to another instance. We do not have enough context to say whether this is good in your situation.

Then you will have to implement these methods:

 bucket_string(bucket_string&& other); bucket_string& operator=(bucket_string&& other); 
+7
source

Put some const .

 bucket_string(const bucket_string & rhs); ^^^^^ bucket_string & operator=(const bucket_string & rhs); ^^^^^ 

You pass temporary const values ​​to the constructor. The compiler is looking for a constructor that uses a const reference:

 bucket_string bs("the quick brown fox jumps over the lazy dog"); 
+6
source

Code mixes value and referential semantics in a way that doesn't work. substr returns a value by value, but the constructor takes a parameter using a non-constant reference. Being unstable, indicates that the parameter will be considered as a modifiable object, not a pure value. The language prohibits the use of links in this way. Adjust the assignment operator so that it cannot change its argument:

  bucket_string & operator=(bucket_string const & rhs); 

This works because C ++ allows you to pass a temporary object (e.g., returned by a function) through a const reference.

If you really want to change the source of the assignment operation, then C ++ offers a solution in rvalue links. Instead of const & use && . But the destination will no longer be idempotent, so it will be called the move destination and you will need an alternative syntax when using non-temporary objects.

+1
source

All Articles