Whats wrong with the folowing simple sample?

struct mystruct
{
  double x, y;
  mystruct(double z) { x=z; y=z; }
};

template <class Real>
class myclass
{
public:
  Real x;
  myclass(Real x) { this->x=x; }
};

int main()
{
  mystruct s(1.,2.);
  myclass<mystruct> obj(s); 
  return 0;
}

The error message look for mystruct::mystruct() constructor,
but I do not know from which point want to call this constructor!

This is your call :

mystruct s(1.,2.);

This is mystruct constructor :

mystruct(double z)

Either make the constructor :

mystruct(double z , double z2);

Or make the call :

mystruct s(1.0);

Sorry about that, but it was just a missprint, the problem is the same with this

struct mystruct
{
  double x;
  mystruct(double x1) : x(x1) {};
};

template <class T>
class myclass
{
public:
  T x;
  myclass(T x) { this->x=x; }
};

int main()
{
  mystruct s(1.);
  myclass<mystruct> obj(s); 
  return 0;
}

Sorry about that, but it was just a missprint, the problem is the same with this

struct mystruct
{
  double x;
  mystruct(double x1) : x(x1) {};
};

  T x;    [b] Here you are trying to use a default constructor for your  
                 struct and there isn't one [/b]

I couldn't configure it to work using your 1 parameter constructor however, so this may be a blind alley.

Thank you!
But this is very inconvenient, bacause I dont wan to define mystruct();

So the problem with templates is that for builtin types you dont need a constructor, but my user defined struct needs a constructor if have more than 0, therefoe have to define constructor without parameters also.
(if I want to use builtin types also in my templated class)

Now I defined a constructor without parameters:

struct real
{
  double value;
  double error;
  real(double,double);
  real(double);
  real();
  operator double() { return value; }
};

I noticed that

real x;

works

but

real x();

does not work, why?

Cause you don't need the () on a default constructor. It thinks you defining a function.

Sorry but I have an other question, as you can see I defined a casting operator in the real class. It works, but it s possible to define it outside the class (not inline)? I tried but cannot.

Cause you don't need the () on a default constructor. It thinks you defining a function.

defining a new functioninside main? it is possible?

defining a new functioninside main? it is possible?

Reread the replies from AD and Narue to your post from yesterday. I believe it's the same situation (and they are quite a bit more clear on it than me).

You defined the casting operator in the struct not the class. I'm not 100% on what your options are.

Struct=class, so my question is that it is possible to write the cast operator body outside the struct (or class), not inline?

mystruct::operator double()
{
	return x;
}

Seems to...

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.