Thread: A reference to separate pairs of values

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    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 01:02 PM.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    141
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    141
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    141
    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.

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, 08:18 PM
  2. problem creating .so
    By k0k33 in forum C Programming
    Replies: 7
    Last Post: 04-27-2009, 04:41 AM
  3. Replies: 7
    Last Post: 10-09-2007, 02:37 PM
  4. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 09:02 AM
  5. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM