Use of structs

Please I have the following code

struct DHTReading
{
  float temp,humd;
  DHTReading(float t,float h)
  { temp = t; humd=h; }
};


struct SensorReadings
{
  DHTReading air1,air2;
  float water;
  SensorReadings(DHTReading a1,DHTReading a2,float w)
  { air1=a1;air2=a2;water=w;}
};

and I am getting the following compilation error

tem.ino: In constructor 'SensorReadings::SensorReadings(DHTReading, DHTReading, float)':

rack_cooling_system:57: error: no matching function for call to 'DHTReading::DHTReading()'

   { air1=a1;air2=a2;water=w;}

   ^

How about posting a full program so that the problem can be seen in context ?

I may be wrong, but it looks like you're defining a function within the struct...sorta like one of those Russian Christmas boxes. I don't think you can do that.

econjack:
I may be wrong, but it looks like you're defining a function within the struct...sorta like one of those Russian Christmas boxes. I don't think you can do that.

A struct is just like a class, except the default access is public, rather than private.

Personally, if it involves code and data, I'd go for a class.

(It's a copy constructor question, isn't it?)

I guess I'm not a fan of having a struct share the same name as a member function. A class makes more sense to me, too.

econjack:
I guess I'm not a fan of having a struct share the same name as a member function. A class makes more sense to me, too.

A constructor by definition must have the same name as the struct/class.

That's not OP's problem.

we need more code to help him solve his problem.

Again, I'm not accustomed to placing functions in a struct, constructor or otherwise. Given the protection a class can afford for encapsulation, what's the reason for using a struct? Placing any function in a struct is a C++ feature, so why not get the extra protection and use a class?

econjack:
Again, I'm not accustomed to placing functions in a struct, constructor or otherwise. Given the protection a class can afford for encapsulation, what's the reason for using a struct? Placing any function in a struct is a C++ feature, so why not get the extra protection and use a class?

A struct is EXACTLY the same thing as a class, with the one exception that the default access is public. Said another way, a class is nothing more than a struct, with the one excettion that the default access is private. There is absolutely NOTHING wrong with using a struct if you want all the members are public. In terms of "protection" they are identical, with the one exception above.
Regards,
Ray L.

Can you separate the method declaration and implementation in the same way as with a class?

RayLivingston:
A struct is EXACTLY the same thing as a class, with the one exception that the default access is public. Said another way, a class is nothing more than a struct, with the one excettion that the default access is private. There is absolutely NOTHING wrong with using a struct if you want all the members are public. In terms of "protection" they are identical, with the one exception above.
Regards,
Ray L.

I'm not trying to say anything is wrong with it, except to me I'd rather have everything default to private rather than public. That one exception is pretty important when it comes to data hiding/encapsulation.

econjack:
I'm not trying to say anything is wrong with it, except to me I'd rather have everything default to private rather than public. That one exception is pretty important when it comes to data hiding/encapsulation.

If your worried, you can just explicitly identify your class/struct members.

I am sorry I am lost. What is wrong with code?

All I am trying to do is that I need to have an object called DHTReading which has two float attributes and SensorReadings which has two DHTReading objects along with a float object.

I am using those objects to return multiple values

The methods are just constructors for the objects so that I could do the following :
return DHTReading(val1, val2) ;

What is wrong on here?

To get a complete answer, you need to follow Bob's request to post all of the code.

boshkash1151:
I am sorry I am lost. What is wrong with code?

You don't have the required default constructor, copy constructor or assignment operator for DHTReading that your struct needs. You should use references i.e.

struct SensorReadings
{
  DHTReading& air1;
  DHTReading& air2;
  float water;
  SensorReadings(DHTReading& a1,DHTReading& a2,float w) : air1(a1), air2(a2)
  {}
};

Also, I would use a class and not a struct and then only make public those members that actually must be.

Edit: Checking the C++ specification it seems that the default constructor, copy constructor and assignment operator will be generated by the compiler if not explicitly declared so my analysis is wrong. This does not mean my solution to the problem is wrong.

econjack:
I'm not trying to say anything is wrong with it, except to me I'd rather have everything default to private rather than public. That one exception is pretty important when it comes to data hiding/encapsulation.

struct members can be given an access specifier of private, it's just not default (as already noted).

That being said, my (totally arbitrary) preference is to use a struct if the type conforms to the good 'ole C definition of a struct. Otherwise, use a class.