Link to home
Create AccountLog in
Avatar of sean-keys
sean-keys

asked on

Create an instance of a strucure and initialize its members in c++ header.

Hi I have the following c++ code, but it wont compile.  I'm used to reg c initialization and the compiler doesn't like it. Any input would be appreciated.

 error: ‘s19Table’ was not declared in this scope
typedef struct s19Info{
  int type;
  const char *description;
  const char *s19ID; // type in ascii
  int addressBytes;
  bool dataSequence;
}s19Desc;

.......

private:
.......
	s19Desc s19Table[] {
	    {S0, "Block header", "S0", 2, true},
	    {S1, "Data sequence", "S1", 2, true},
	    {S2, "Data sequence", "S2", 3, true},
	    {S3, "Data sequence", "S3", 4, true},
	    {S5, "Record count", "S5", 2, false},
	    {S7, "End of block", "S7", 0, false},
	    {S8, "End of block", "S8", 3, false},
	    {S9, "End of block", "S9", 2, false},
	    { 0,              0,    0, 0, false}
	};
};

Open in new window

SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
= missed...

s19Desc s19Table[] = {
          {S0, "Block header", "S0", 2, true},
          {S1, "Data sequence", "S1", 2, true},
          {S2, "Data sequence", "S2", 3, true},
          {S3, "Data sequence", "S3", 4, true},
          {S5, "Record count", "S5", 2, false},
          {S7, "End of block", "S7", 0, false},
          {S8, "End of block", "S8", 3, false},
          {S9, "End of block", "S9", 2, false},
          { 0,              0,    0, 0, false}
      };
Again,
You can't initialize data members like this way. This is not allowed in C++ class header.
Work arround would be keep it inside the constructor.
Is there an echo?
Avatar of sean-keys
sean-keys

ASKER

So by global you just mean create it in another header file and include it correct ? Or is that not a proper definition for global?
sorry for the delay
Yes, pretty much like that.
>> So by global you just mean create it in another header file and include it correct ?
You will need to define it in a .cpp file and declare it in a header otherwise you will violate the "One Definition Rule".

You know, unless this struct specifically needs to be a pod you would be much better off just using a vector and initialising it in the constructor.
evilrix
thanks for the vector advice, coming from c I should take the time to learn vectoring.

For now to use a pod I would want this?

custom_types.h  -> define the sturct
custom_types.cpp -> initialize the struct

main.h -> include custom_types.cpp
main.cpp -> no include
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks guys !