Question about type mismatch with references.

S

soft_guy

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
 
A

Andre Kostur

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;
};


?
 
S

soft_guy

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.
 
V

Victor Bazarov

soft_guy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top