tags:

views:

74

answers:

3

Hi all,

I am declaring my struct object inside my class private and i am initializing them using constructors but my style checker says that the member function of my struct type are not initialized. Can any one help me in this regards i will be very thankful to you.

Below is my code please suggest some solution to this problem

class Datastructure{

  //forward decleration
  struct Ship;

public:

  //Constructor DS class
  Datastructure();

 //Destructor DS class
  ~Datastructure();



private:

   struct Ship{
     std::string s_class;
     std::string name;
     unsigned int length;
     Ship();
     Ship(const std::string& shipClass, const std::string& shipName,
          unsigned int len);
   };

  Ship minShip;
  Ship maxShip;
  std::vector<Ship> shipVector;
};

#endif

it is giving me the following warnings

  CIMP, line 17: Uninitialized member variables in class 'Datastructure'.
  FSCH, line 17: No access specifiers at the beginning of class
                 'Datastructure'.
  IVAP, line 62: Field 'minShip' in class 'Datastructure' is not initialized.
  IVAP, line 63: Field 'maxShip' in class 'Datastructure' is not initialized.
  IVAP, line 64: Field 'shipVector' in class 'Datastructure' is not
                 initialized.
A: 

What compiled do you use? VisualStudio 2008 does not show such warnings.

Kiril Kirov
A: 
ArunSaha
Why? Both `Ship` and `std::vector` have user-declared constructors so _value-initialization_ has exactly the same effect as the _default-initialization_ that they would otherwise receive.
Charles Bailey
Why would explicit call of the default ctors be needed? The default ctor is what's used if you **don't** specify otherwise (and in this case the compiler-generated ones are fine). If the OP isn't defining some function he declares he'd get a linker error, not compiler warnings!
Alex Martelli
Probably I am wrong, I got carried away by the "warnings". I will get back, and possibly delete the response.
ArunSaha
I see it now. Calling the `minShip(), maxShip(), shipVector()` from `Datastructure()` constructor is not needed. But, it still seems to me that a constructor for `Ship()` need to be provided, at least for initializing `length`. Is that correct?
ArunSaha
A: 

By the C++ standard, minShip, maxShip, and shipVector have been initialized using their default constructors.

However, you indicated that you are using a style checker. Your style-guide may require explicit calls to the default constructor. One reason to do this is to make sure that you call the correct constructor and not rely on automatic behavior -- this is not what the standard require or even typical C++ programmers do, but if that's your style, and your style checker flags it, you might just need to comply.

There are all kinds of style rules in place in different organizations that aren't necessary for the compiler, but perhaps make it easier for developers to understand each other's code.

Lou Franco
i am using my university style check that is tutg++ style checker... actually i have to submit the assignment and for that i need 100% style and now it is 84%. so anyother valueable ideas will be highly appreciated
Abdul Samad
@user226161: If your university style checker requires you to explicitly initialise all member variables (no relying on language defaults), then there is no way around it. You just have to write the explicit initialisation code, even though the compiler-generated initialisation would have been identical.
Bart van Ingen Schenau