Pointers related.

This is a discussion on Pointers related. within the C++ Programming forums, part of the General Programming Boards category; I have a "f" function whose result type is A. I have a "g" function that takes one parameter of ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    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:
    Code:
    temp=g(f())
    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:
    Code:
    temp=g(&f())
    But that fails as expected: "error C2102: '&' requires l-value"

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }
    Last edited by 7stud; 08-01-2003 at 04:57 PM.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    So I guess I must necessarily use a temporary variable, huh?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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:

    Code:
    g( &(f()) )
    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())
    Last edited by 7stud; 08-01-2003 at 10:23 PM.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Question related to array and pointers
    By Q4u in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2002, 01:54 PM
  4. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 02:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21