A reference to separate pairs of values

This is a discussion on A reference to separate pairs of values within the C++ Programming forums, part of the General Programming Boards category; It does compile however, but I've not tried to use it. What would the problem be? It seems like most ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    It does compile however, but I've not tried to use it. What would the problem be? It seems like most of the natural complex op.s would work.
    Are you sure it compiles? I noticed that my statement was inaccurate since std::complex is not a container, but still, references are not copyable (though what they refer to may be copyable). So, what happens if you try to assign a std::complex<double&> to another std::complex<double&>? It would mean that you have to assign the references themselves so they refer to a different object, and as pointed out this is illegal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    Quote Originally Posted by brewbuck View Post
    The problem is that when a reference is created it MUST be seated to another variable. In the following code, where exactly does that reference get seated?

    Code:
    std::complex<double &> val;
    std::complex will attempt to default-construct the real and imaginary parts -- but since these parts are of type "double &" there is no such default constructor.
    Well I tried to contruct it like

    Code:
    double x,y ;
    std::complex<double &> val(x,y) ;
    But I'm having some issues with the roll your own complex reference class as well. If I want to implement a forward iterator it seems I can either support reading the values or assigning the values but not both.

    The problem is how can I write an assignment operator that assigns a class of type complex_ref to the class std::complex<double> ?

    Code:
    typedef std::complex<double> complexd
    
    complexd & complexd::operator=(complex_ref &u)
    {
    
      this->real() = u.real() ;
      this->imag = u.imag() ;
      return *this ;
    }
    unfortunately complexd->real() is a const reference, assignment shouldn't be able to work.
    Maybe this? (based on your implementation)

    Code:
    typedef std::complex<double> complexd
    
    complexd & complexd::operator=(complex_ref &u)
    {
    
     *this = u.complexd() ;
      return *this ;
    }
    O wait maybe I'm misunderstanding something.
    Code:
    	operator complexd() const
    	{
    		return (complexd(real,imag)) ;
    	}
    Does this overload a type cast or something? This syntax is new to me. If so is the type cast applied automatically?
    Last edited by SevenThunders; 04-03-2008 at 02:02 PM.

  3. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by SevenThunders View Post
    unfortunately complexd->real() is a const reference, assignment shouldn't be able to work.
    Maybe this? (based on your implementation)

    Code:
    typedef std::complex<double> complexd
    
    complexd & complexd::operator=(complex_ref &u)
    {
    
     *this = u.complexd() ;
      return *this ;
    }
    Close:

    Code:
    *this = u.operator complexd();
    Or:

    Code:
    *this = (complexd)u;
    Actually, does that work? I am not sure if you can invoke a conversion operator through a typedef like that. It is an alias for the same type, but I'm not sure what the standard says. You might have to explicitly specify "std::complex<double>" instead.

    I'm sure laserlight or CornedBee will know the answer to that one.

  4. #19
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    Quote Originally Posted by brewbuck View Post
    Close:

    Code:
    *this = u.operator complexd();
    Or:

    Code:
    *this = (complexd)u;
    Actually, does that work? I am not sure if you can invoke a conversion operator through a typedef like that. It is an alias for the same type, but I'm not sure what the standard says. You might have to explicitly specify "std::complex<double>" instead.

    I'm sure laserlight or CornedBee will know the answer to that one.
    Not only does it work but the cast is not needed and the attempt to overload = for std::complex is unnecessary. Not only is the attempt at overloading unnecessary, it won't let me for some reason. Maybe operator overloadings have to be friends of the underlying class. Anyway this seems to be right as rain on MS Vis. Studio 2008.

    Code:
    	double x = 1.0 , y = 1.0 ;
    	complexref r(x,y) ;
    	complexd u = r ;
    	std::cout << u  << std::endl ;

  5. #20
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by SevenThunders View Post
    Not only does it work but the cast is not needed and the attempt to overload = for std::complex is unnecessary.
    I guess so, since std::complex<double> already has an operator=(const std::complex<double>) and we've provided an implicit conversion to that type.

    Not only is the attempt at overloading unnecessary, it won't let me for some reason.
    What is some reason? What error do you get?

  6. #21
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    Quote Originally Posted by brewbuck View Post
    I guess so, since std::complex<double> already has an operator=(const std::complex<double>) and we've provided an implicit conversion to that type.



    What is some reason? What error do you get?
    It could be a problem with syntax, since it gets a bit squirly, but this code,
    Code:
    std::complex<double> & std::complex<double>::operator=(complexref &cr)
    {
    *this = cr ;
    return(*this) ;
    }
    gives me a compile error
    : error C2511: 'std::complex<double> &std::complex<double>:perator =(complexref &)' : overloaded member function not found in 'std::complex<double>'
    in MS Visual Studio 8. It's probably something stupid I did with the syntax.

  7. #22
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Well, you're trying to create a new member of std::complex<T>, which is impossible without changing the header file.

    I thought you were trying to say that the assignment operator FROM std::complex<T> TO complex_ref was not working -- it should.

  8. #23
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    Quote Originally Posted by brewbuck View Post
    Well, you're trying to create a new member of std::complex<T>, which is impossible without changing the header file.

    I thought you were trying to say that the assignment operator FROM std::complex<T> TO complex_ref was not working -- it should.
    Actually I was just trying to overload the assignment operator for complex<T>. Somehow this doesn't quite work unless you change the header, which I don't need to do thanks to the cast trick.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. problem creating .so
    By k0k33 in forum C Programming
    Replies: 7
    Last Post: 04-27-2009, 05:41 AM
  3. Replies: 7
    Last Post: 10-09-2007, 03:37 PM
  4. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 10:02 AM
  5. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM

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