473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

no matching function for call to explicit copy-constructor

Trying to compile the following code-fragment with g++ 2.96:

class Entity {
private:
void * data;
public:
explicit Entity(int);
explicit Entity(Entity &);
virtual ~Entity();
void makeSomething();
};
Entity f(int n) {
Entity result(n);
result.makeSomething();
return result;
}

I obtain the error:
no matching function for call to `Entity::Entity (Entity &)'
on the the row:
return result;

It's a my fault?

The only way to get away from the error is to change the copy
constructor into:

/* explicit */ Entity(Entity &);

But I want to use explicit...

Is there any other option to solve my problem?

- Dario

Jul 19 '05 #1
2 15309
Dario wrote:
Trying to compile the following code-fragment with g++ 2.96:

class Entity {
private:
void * data;
public:
explicit Entity(int);
explicit Entity(Entity &); explicit Entity(Entity const &); virtual ~Entity();
void makeSomething();
};
Entity f(int n) {
Entity result(n);
result.makeSomething();
return result;
}

I obtain the error:
no matching function for call to `Entity::Entity (Entity &)'
on the the row:
return result;

It's a my fault?
Yes. You made the copy constructor explicit.
The only way to get away from the error is to change the copy
constructor into:

/* explicit */ Entity(Entity &);

But I want to use explicit...

Is there any other option to solve my problem?


Not really. Making the copy constructor explicit does not seem to be a good
idea.

Of course - in any other case - you could write Entity(result), but not in a
return statement. That would count as copy initialization of the return
value and an accessible (implicit) copy constructor has to be present.

I think you have no choice but to remove the explicit.

--
Attila aka WW
Jul 19 '05 #2
On Mon, 22 Sep 2003 12:28:57 +0200, Dario <da***@despammed.com> wrote:
Trying to compile the following code-fragment with g++ 2.96:

class Entity {
private:
void * data;
public:
explicit Entity(int);
explicit Entity(Entity &);
Are you sure you don't want:
Entity(Entity const&);
? If not, why not?
virtual ~Entity();
void makeSomething();
};
Entity f(int n) {
Entity result(n);
result.makeSomething();
return result;
}

I obtain the error:
no matching function for call to `Entity::Entity (Entity &)'
on the the row:
return result;

It's a my fault?
Yes, since you made the copy constructor "explicit".

The only way to get away from the error is to change the copy
constructor into:

/* explicit */ Entity(Entity &);

But I want to use explicit...


Why? Making a copy constructor explicit is done precisely to prevent
that type from being returned by value or used as a pass-by-value
parameter to a function, while still allowing local copies to be made.
What you you using explicit for?

Tom
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
1
by: A. Farber | last post by:
Hi, I'm trying to port a bigger program from Visual C++ to Linux and after rewriting few Visual C++ functions (strnicmp, filelength, findnext, etc.) I'm finally down to just one (!) error...
5
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
1
by: yuanhp_china | last post by:
I define a class: class RtServer { public: RtServer(); void init(); void close();
2
by: hectic | last post by:
i have three file header file where i define the function prototype and the other implementaion file for the function body and other one is the test file which takes input for the implementation file...
2
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
5
by: tomas | last post by:
Hi. I wrote a code similar to this one, for a wrapper application that I needed: #include <iostream> using namespace std; class Client {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.