| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"soft_guy" |
| Date: |
10 Jul 2007 01:48:04 PM |
| Object: |
Question about type mismatch with references. |
I have an issue I would like to understand. I have a class which takes
arguments in its constructor which are references to objects. I'm
going to use QString as an example because that's the class that I'm
really having this issue with, but I suspect it could just as easily
many other types.
Example:
class Person
{
public:
Person(const QString & fn, const QString & ln) : firstName(fn),
lastName(ln) {}
private:
QString firstName;
QString lastName;
};
Let's say there are functions like this which can generate data:
QString generateFirstName();
QString generateLastName();
Then I want to create an instance of my class:
Person somedude(generateFirstName(), generateLastName());
This results in a compiler error that says "no matching function call
for Person::Person(QString, QString). Candidates are
Person::Person(QString &, QString &)". (I'm seeing this error in GCC
4.0.1.)
I can get the error to go away by doing this:
QString fn = generateFirstName();
QString ln = generateLastName();
Person somedude(fn, ln);
I'd like to understand why I'm seeing this error and what it means. I
know at a basic level that it is because I'm trying to pass the
results of a function call rather than an actual variable, but why are
these different?
Thanks.
Brant Sears
.
|
|
| User: "Andre Kostur" |
|
| Title: Re: Question about type mismatch with references. |
10 Jul 2007 01:56:56 PM |
|
|
soft_guy <brant@mac.com> wrote in news:1184093284.607947.194710@
57g2000hsv.googlegroups.com:
I have an issue I would like to understand. I have a class which takes
arguments in its constructor which are references to objects. I'm
going to use QString as an example because that's the class that I'm
really having this issue with, but I suspect it could just as easily
many other types.
Example:
class Person
{
public:
Person(const QString & fn, const QString & ln) : firstName(fn),
lastName(ln) {}
private:
QString firstName;
QString lastName;
};
Let's say there are functions like this which can generate data:
QString generateFirstName();
QString generateLastName();
Then I want to create an instance of my class:
Person somedude(generateFirstName(), generateLastName());
This results in a compiler error that says "no matching function call
for Person::Person(QString, QString). Candidates are
Person::Person(QString &, QString &)". (I'm seeing this error in GCC
4.0.1.)
I can get the error to go away by doing this:
QString fn = generateFirstName();
QString ln = generateLastName();
Person somedude(fn, ln);
I'd like to understand why I'm seeing this error and what it means. I
know at a basic level that it is because I'm trying to pass the
results of a function call rather than an actual variable, but why are
these different?
Are you sure that you've posted the exact code you're having problems
with, or is your Person class defined as follows:
class Person
{
public:
Person(QString & fn, QString & ln) : firstName(fn), lastName(ln) {}
private:
QString firstName;
QString lastName;
};
?
.
|
|
|
| User: "soft_guy" |
|
| Title: Re: Question about type mismatch with references. |
10 Jul 2007 02:18:11 PM |
|
|
Are you sure that you've posted the exact code you're having problems with:
No, its not the exact code. So, yes, let's say that the arguments
aren't const.
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Question about type mismatch with references. |
10 Jul 2007 02:20:07 PM |
|
|
soft_guy wrote:
Are you sure that you've posted the exact code you're having
problems with:
No, its not the exact code. So, yes, let's say that the arguments
aren't const.
References to non-const objects cannot be bound to temporaries.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
|
|

|
Related Articles |
|
|