Assignment from scalar to user-defined type

Discussion in 'C++' started by Richard Cavell, Mar 6, 2005.

  1. #include <stdint.h>

    class uint128bit
    {
    public:
    uint64_t bits0to63;
    uint64_t bits64to127;

    uint128bit operator= (uint128bit op2) { bits0to63 = op2.bits0to63;
    bits64to127 = op2.bits64to127; return *this; }
    uint128bit operator= (uint64_t op2) { bits0to63 = op2; bits64to127 =
    0; return *this; }
    };


    int main (int argc, char * const argv[]) {
    uint64_t a = 1LL;
    uint128bit i128_a = a;
    return 0;
    }

    This last line of code fails, saying: conversion from 'uint64_t' to
    non-scalar type 'uint128bit' requested. What have I done wrong?
     
    Richard Cavell, Mar 6, 2005
    #1
    1. Advertising

  2. "Richard Cavell" <> skrev i en meddelelse
    news:d0ealn$8dv$...
    > #include <stdint.h>
    >
    > class uint128bit
    > {
    > public:
    > uint64_t bits0to63;
    > uint64_t bits64to127;
    >
    > uint128bit operator= (uint128bit op2) { bits0to63 = op2.bits0to63;
    > bits64to127 = op2.bits64to127; return *this; }
    > uint128bit operator= (uint64_t op2) { bits0to63 = op2; bits64to127 = 0;
    > return *this; }
    > };
    >
    >
    > int main (int argc, char * const argv[]) {
    > uint64_t a = 1LL;
    > uint128bit i128_a = a;
    > return 0;
    > }
    >
    > This last line of code fails, saying: conversion from 'uint64_t' to
    > non-scalar type 'uint128bit' requested. What have I done wrong?


    You need a way to construct a uint128bit from a uint64_t ;-)

    class uint128bit
    {
    ....
    uint128bit(uint64_t rhs): bits0to63(rhs),bits64to127(0) {}
    ....


    /Peter
     
    Peter Koch Larsen, Mar 6, 2005
    #2
    1. Advertising

  3. Richard Cavell wrote in news:d0ealn$8dv$ in
    comp.lang.c++:

    > #include <stdint.h>
    >
    > class uint128bit { public:
    > uint64_t bits0to63; uint64_t bits64to127;
    >
    > uint128bit operator= (uint128bit op2) { bits0to63 = op2.bits0to63;
    > bits64to127 = op2.bits64to127; return *this; }
    > uint128bit operator= (uint64_t op2) { bits0to63 = op2; bits64to127
    > = 0; return *this; } };
    >
    > int main (int argc, char * const argv[]) {
    > uint64_t a = 1LL;


    This is an initialization *not* an assignment, as it isn't an
    assignment it doesn't use the assignment operator (operator = ()).

    > uint128bit i128_a = a;


    > return 0;
    > }
    >
    > This last line of code fails, saying: conversion from 'uint64_t' to
    > non-scalar type 'uint128bit' requested. What have I done wrong?
    >


    You haven't provided a converting constructor.

    uint128bit( uint64_t a ) { *this = a; }

    you will need:

    uint128bit() {}

    too, as the user defined constructor suppresses the compiler
    generated default constructor.

    In case you don't know, <stdint.h> isn't C++ (its C) so your
    code isn't Standard (portable) C++.

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Mar 6, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  2. Thelma Roslyn Lubkin

    scalar(0) for a user defined type

    Thelma Roslyn Lubkin, Nov 18, 2007, in forum: C++
    Replies:
    4
    Views:
    365
    Thelma Roslyn Lubkin
    Nov 19, 2007
  3. Clint Olsen
    Replies:
    6
    Views:
    365
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    168
    Arndt Jonasson
    Jan 27, 2005
  5. emrefan
    Replies:
    4
    Views:
    366
    emrefan
    Jun 5, 2007
Loading...

Share This Page