I would like to initialize a user defined struct with the = operator and a scalar type (for example: double) on its right hand side, like this:

``````struct real
{
double value;
double error;
...
};

int main()
{
real x = 1.; // supposing error=0.
}``````

It is possible somehow?

I defined my struct like this:

``````struct real
{
double value;
double error;
real(double val, double err);
};

real::real(double val, double err=0);``````

This allows initializatin like this:

``````real x(1.,0.);
real y(1.);``````

``````real operator=(const real param);
real operator=(const double param);``````

but I can not initialize with it!

I can overload the cast operator double(),
but I need the oposite one!
I need to convert double -> real,
and not real -> double!
How can I solve this problem?

I'm not sure, but try:

``real x = {1.};``

With this, you wouldn't need to overload any operator...

Hope it helped!

Yes I know that, but my question is how to use x = 1. syntax!
I dont want to change x = 1. !

``````#include <iostream>

using namespace std;

struct real
{
double value;
double error;

real(const double val) {
value = val;
error = 0;
}
};

int main()
{
real x = 1.;

cout << x.value << ' ' << x.error;

return 0;
}``````

Is this what you meant?

This is just a constructor, I have already written at the begining.

With this constructor you has to ue the syntax:

``real x(1.);``

As I know!

This is just a constructor, I have already written at the begining.

With this constructor you has to ue the syntax:

``real x(1.);``

As I know!

I'm afraid I'm a bit confused by what you want to do... This is my understanding of your question:

1. You'd like to initialize objects of real using this syntax: `real x = 1.;` , where `error` is always 0

2. You'd like to achieve this using any method

Secondly, I'm sure you haven't written this particular constructor, which accepts only one argument of type double, as it isn't in the code you provided.

And with constructors, you don't always have to use the format `real x(1.);` , and this will work fine: `real x = 1.;` , as I've shown in the code in my last post.

And lastly, have you tried compiling the code I gave? It'll work just fine using the same type of initializing you require, i.e `real x = 1.;` ...

Yes I'd like to initialize objects of real using this syntax: real x = 1.;
(where error is 0 by default, in case of this sytax)
And I'd like to achieve this using any method!

Yes you are right, I havent written down that constructor,
but I've mantioned this

``real::real(double val, double err=0);``

which is almost the same (this can accept also one argument)

I use MinGW and x = 1.; does not work with such a constructor!?

Sorry, you are right, but I dont know what is the difference!
With

``real::real(double val);``

real x = 1; works! But with

``real::real(double val, double err=0);``

do not work!

Which version of minGW are you using? I've tried the code on both my minGW and GCC Compilers, and they don't come up with any sort of error...

To the best of my knowledge(which might be bad), this is the only way to achieve this... Try compiling your programs with GCC, or a newer version of minGW

With real(double val); constructor, its OK.
But with real(double val, double err=0); constructor, I get
error: conversion from 'double' to non-scalar type 'real' requested

With real(double val); constructor, its OK.
But with real(double val, double err=0); constructor, I get
error: conversion from 'double' to non-scalar type 'real' requested

Then use both the constructors in the program :), like:

``````struct real
{
/*...*/
real(const double val);
real(double val, double err);
/* ... other constructors ... */
};``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.