Using objects returned from functions

G

Guest

I'm having trouble using Braid (my user-defined type) objects returned
from functions. I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b). When a Braid
calls this, b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.

I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck (this may partly be because I
overloaded * to multiply Braids together?)

Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Thanks!

James McLaughlin.
 
V

Victor Bazarov

I'm having trouble using Braid (my user-defined type) objects returned
from functions.

What kind of trouble?
> I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b).

What does that mean "function needs to use"?
> When a Braid
calls this,

'This' -- what?
> b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.

Uh... With what problem?
I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

What situation?
Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck (this may partly be because I
overloaded * to multiply Braids together?)

Of course. The object which 'descend_n' returns, is _a_temporary_.
It cannot be bound to a reference to non-const. Make your 'left_multiply'
accept a reference to const:

void left_multiply(Braid const&);
Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

See above.
as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Read a bit more about const-correctness. That should give you a hint.

V
 
A

Alipha

I'm having trouble using Braid (my user-defined type) objects returned
from functions. I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b). When a Braid
calls this, b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.

Perhaps you should refresh yourself on what your code is actually
doing? Sounds like double dispatch (google if you don't know what it
is, but you need a strong understanding of object-oriented design to
understand what double dispatch is), but double dispatch makes use of
polymorphism, which your code has otherwise shown no indication of
having.

the whole point of passing something by reference (to non-const) is so
that the function modifies the paramater you pass it AND you do
something with the parameter afterwards:

void foo(double &x) { x = 3.0; }

int main() {
dobule y = 2;
foo(y); /* modify y */
std::cout << y << std::endl; /* do something with y afterwards */
}

There is no point in passing a temporary by reference (to non-const),
since you can't do anything with the resulting modifications:

void foo(double &x) { x = 3.0; }

int main() {
foo(std::pow(5.0, 2.0)); /* foo modifies the return value of
std::pow, but what's the point? you can't access the variable that was
just modified */
}

Note that the above program will NOT COMPILE. There is generally no
point in passing a temporary to a function by reference, and so the
compiler won't let you.

That pretty much sums up your situation and why you're getting the
errors.
I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck

Don't randomly try stuff. Computer science is a science, not guesswork.
If you don't know beforehand whether or not something will solve your
problem, then you need to read your book and get a better understanding
of the language.
(this may partly be because I
overloaded * to multiply Braids together?)

No. Unary and binary * are completely different operations. Read your
book.
Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Thanks!

James McLaughlin.

Why does left_multiply take a reference to non-const in the first
place? Should it take an object instead? should it take a reference to
const? Do you know?
 
J

Josh Mcfarlane

I'm having trouble using Braid (my user-defined type) objects returned
from functions. I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b). When a Braid
calls this, b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.

I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck (this may partly be because I
overloaded * to multiply Braids together?)

Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Thanks!

James McLaughlin.

Posting the definition for left_multiply would help more.

Anyways...maybe moving left multiply to using a Braid vs Braid&.
(You're using a reference to a temporary object. This could end up
having undesired side effects).

Perhaps a post of the left_multiply and descend code would also help.
 
G

Geo

Alipha said:
Don't randomly try stuff. Computer science is a science, not guesswork.
If you don't know beforehand whether or not something will solve your
problem, then you need to read your book and get a better understanding
of the language.

Is it 'guesswork' or is it 'experimentation', you shouldn't be so
judgemental, sometimes experimenting is the best way to learn, if we
only stuck to stuff we read in books, we'd never move forward, and if
we knew beforehand what the solution was, then life really would be
boring !!!!
 
J

Josh Mcfarlane

Geo said:
Is it 'guesswork' or is it 'experimentation', you shouldn't be so
judgemental, sometimes experimenting is the best way to learn, if we
only stuck to stuff we read in books, we'd never move forward, and if
we knew beforehand what the solution was, then life really would be
boring !!!!

I'll admit, it personally took me some fiddling around to get the hang
of pointers, & operator, and *. It was confusing at first, and no
matter how much I read, you just really can't get the hang of it til
you start trying to use it in code. Granted, it all got caught by the
compiler, so no hassle trying to find deep problems, but if you're just
playing around, experimentation with thorough tests is a great way to
learn.
 
G

Guest

Thanks (especially to Geo and Josh for friendly postings!) I'm now
passing const Braid& into my functions and they work perfectly.
 

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top