-
Pointers related.
I have a "f" function whose result type is A.
I have a "g" function that takes one parameter of type pointer-to-A.
I have to call the "g" function using the return value of "f", but I can't:
I get "error C2664: cannot convert parameter 1 from 'struct A' to 'struct A*'. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called".
An immediate idea would be to write:
But that fails as expected: "error C2102: '&' requires l-value"
-
After a function returns, the objects are destroyed. So, when you try to reference the location of A with &A, it doesn't exist anymore, so you get an error.
Hmmm...but this works:
Code:
#include <iostream>
using namespace std;
class A
{
public:
A(int n)
{
num = n;
}
private:
int num;
};
A func(void)
{
A an_object(20);
return an_object;
}
void gunc(A* pA)
{
cout<<"Hello world."<<endl;
}
int main()
{
A myObject(10);
gunc(&(func()));
return 0;
}
-
So I guess I must necessarily use a temporary variable, huh?
-
It looks like just your notation is wrong:
You have a function f() and you want to get the address of the return value, so you need to do this:
&(f())
Then you want to call g() with that as the parameter:
My previous explanation is wrong because when you return a local object, a copy is made and the copy is returned, and then the original is destroyed. You can't reference the original object, e.g. &an_object in my example, but you can reference the address of the copy, which is what you are doing when you say:
&(f())
-
Sorry, the error is still with me.
Code:
point3d _stdcall fpoint3d(double x,double y,double z)
{struct point3d t={x,y,z};
return t;}
point3d _stdcall vproduct(point3d *v1,point3d *v2)
{struct point3d t={v1->y*v2->z-v1->z*v2->y,v1->z*v2->x-v1->x*v2->z,v1->x*v2->y-v1->y*v2->x};
return t;}
... ... ... ... ... ...
point3d y=vproduct(&(fpoint3d(l->a,l->b,l->c)),&x);//error C2102: '&' requires l-value