After a few months of learning and practicing C++ with the book C++ Primer Plus through Chapter 7, I decided to do myself a little simple program.

I started with the decision to try splitting my program into multiple files, and I decided to collect all shared declarations/definitions (*) into header files, and that is where the problem started.

The first header file created (let's call it headerFile.h), the one I'm working on, has declarations/definitions like these:

enum QUALITY {QUALITY_1, QUALITY_2, QUALITY_3};
enum QUANTITY {QUANTITY_1, QUANTITY_2, QUANTITY_3, QUANTITY_4};

struct myStruct
{
	int myInt;
	int myOtherInt1, myOtherInt2;
	QUALITY myQuality;
	QUANTITY myQuantity;
	std::string myString;
};

myStruct myStructures[13];

Then, below those lines, I did something like these:

myStructures[1].myInt = 1;
myStructures[1].myOtherInt1 = 2; myStructures[1].myOtherInt2 = 3;
myStructures[1].myQuality = QUALITY_1;
myStructures[1].myQuantity = QUALITY_3;

myStructures[1].myString = "Sparkles! Wee!";

I'm using VS2010, with this header file added in the Header Files folder of an empty project, a main .cpp in the source files project.

In the editor tab for headerFile.h, the lines in the upper code are underlined (by the red curvy one that underlines errors) like this:

myStructures[1].myInt = 1;
(The structure array name and the dot)

Every single statement that assign a value to a structure field (**) are all underlined that way. When hovered over those underlined structure array name, VS2010 says something like this:

int myStructures[1]
Error: This declaration has no storage class or type specifier.

And something like this over the underlined dots: Error: expected a declaration What is wrong? What does those errors mean? I don't understand them one bit.

I searched around the net and there are mentions of typedef, but aren't myStruct, QUALITY and QUANTITY names of new types already? Does these have anything to do with header files?

And, why is myStructures[1] identified as int?

(*) Can anyone help me to understand deeply the concepts of declaration, definition,
(**) and initialization? Is assigning something to a structure field initialization?

One more thing. C++ Primer Plus says that C++ header files are without .h extension. Why do we have to add .h to our headers, if we are writing C++, not C, programs?

The one thing i would advise first off, is that in headers files the tradition is to ONLY declare function prototypes, classes/structures and types.

The actual objects themselves should be delcared in a cpp file.
So the code to create the variables of these types and operate them should not be in declerations so

//header.h 
struct myStruct
{
... vars of mystruct
};

//main.cpp
int main(void)
{
   myStruct myStructArray[10]; //create an array of myStruct

   myStructArray[x].someVar = someValue;
}

I think your issue comes from this partitioning issue.

Hope this brief explination helps

Oh also, about your questions

(**) and initialization? Is assigning something to a structure field initialization?

One more thing. C++ Primer Plus says that C++ header files are without .h extension. Why do we have to add .h to our headers, if we are writing C++, not C, programs?

The thing about initilisation as i showed above is that in a decleration you are making a type, you should know that types are not executable code, but the code in a function is executable so this is were you can define variables and assign to them (with the exception of global variables which can be defined but code to operate them must be in a body of executable code)

About the .h extensions this applies to standard includes, the lach of .h is to emphasise the fact that you are including a cpp header not a normal c header. examples include the following

//c version
#include <stdlib.h>
//cpp version of this header
#include <cstdlib> /*note the c prefix to say it is from standard c and the lack of .h to say its a cpp header*/

#include <vector> /* this header lacks the .h to say its a cpp header but no c prefix as it is a new header for cpp that c did not have */

However normally when you create a headder in your own code is is normal to include the .h to show a file is a header and some people use .hpp to emphasise that it is a cpp header

The one thing i would advise first off, is that in headers files the tradition is to ONLY declare function prototypes, classes/structures and types.

The actual objects themselves should be delcared in a cpp file.
So the code to create the variables of these types and operate them should not be in declerations so

//header.h 
struct myStruct
{
... vars of mystruct
};

//main.cpp
int main(void)
{
   myStruct myStructArray[10]; //create an array of myStruct

   myStructArray[x].someVar = someValue;
}

I think your issue comes from this partitioning issue.

Hope this brief explination helps

Thanks for the reply <3 So, I rearranged my files this way:

headerFile.hpp:

#include <string>
enum QUALITY {QUALITY_1, QUALITY_2, QUALITY_3};
enum QUANTITY {QUANTITY_1, QUANTITY_2, QUANTITY_3, QUANTITY_4};
 
struct myStruct
{
int myInt;
int myOtherInt1, myOtherInt2;
QUALITY myQuality;
QUANTITY myQuantity;
std::string myString;
};

objects.cpp

#include <string>
#include "headerFile.hpp"

std::string Description[3] = 
{
	"One", "Two", "Three"
};

myStruct myStructArray[13];

Yet, the statement

myStructures[1].myInt = 1;

below those lines of objects.cpp still errors

int myStructures[1]
Error: This declaration has no storage class or type specifier.

and

Error: expected a declaration

like the previous partitioning.

What is wrong? Isn't all the assigning and declaring (whatever it is called x.x) in a .cpp now?

Im sorry i dont think i was very clear about that last post let me expand the code i posted to try make this clearer.

//header.h 
struct myStruct
{
... vars of mystruct
};

myStruct[10] stArray; //this code is not within an executable body but a variable can be created here and it will have a global scope.

stArray[x].someMember = someValue; //this code will not compile as you cannot put executable code outside of an executable body ie a function


//main.cpp
int main(void)
{
   myStruct myStructArray[10]; //create an array of myStruct

   myStructArray[x].someVar = someValue; //this time as we are withn an executable body we can work with our variable.
}

The above should help to explain that code that operates upon variables cannot be created outside of an executable body. This makes sence if you think about it, as it is not in a body,.. when is it called? by whom ?and where does the compiler put this code into the executable?

I hope this clears things up for you, if for whatever reason you still do not understand then i think your best option is to study more code snippets/examples and see how they partition code to grasp these concepts.


Edit:

Ooh also sorry i misread/understood your last post ,...

int myStructures[1]
Error: This declaration has no storage class or type specifier.

this line make me curious ,... i wuld like to see more of your code if that is possible, is this the line you have typed? because that line would be declaring a single element array of type int as it is typed there (which is just an int)

Also in what you posted above you have declared as i called it myStruct myStructArray[13]; but this line myStructures[1].myInt = 1; is working on a different array is this how your code actually is?

Im sorry i dont think i was very clear about that last post let me expand the code i posted to try make this clearer.

//header.h 
struct myStruct
{
... vars of mystruct
};

myStruct[10] stArray; //this code is not within an executable body but a variable can be created here and it will have a global scope.

stArray[x].someMember = someValue; //this code will not compile as you cannot put executable code outside of an executable body ie a function


//main.cpp
int main(void)
{
   myStruct myStructArray[10]; //create an array of myStruct

   myStructArray[x].someVar = someValue; //this time as we are withn an executable body we can work with our variable.
}

The above should help to explain that code that operates upon variables cannot be created outside of an executable body. This makes sence if you think about it, as it is not in a body,.. when is it called? by whom ?and where does the compiler put this code into the executable?

I hope this clears things up for you, if for whatever reason you still do not understand then i think your best option is to study more code snippets/examples and see how they partition code to grasp these concepts.


Edit:

Ooh also sorry i misread/understood your last post ,...

this line make me curious ,... i wuld like to see more of your code if that is possible, is this the line you have typed? because that line would be declaring a single element array of type int as it is typed there (which is just an int)

Also in what you posted above you have declared as i called it myStruct myStructArray[13]; but this line myStructures[1].myInt = 1; is working on a different array is this how your code actually is?

No, the above codes are hand-typed xD It is not the real ones. In my code there is only one array, so no, that is not how my code is.

About the line you are curious about, yes, it is what I retyped from what VS tells me with the bubble that appears when I hover over the array name. The struct type is defined, then the array, then the statement assigning data to it. What confuses me is why the array structure got identified as an int =.=

Oh well. Obviously I am not equipped enough to deal with multiple source files yet. I'll go back to single source file first, then I'll break them up later. Or perhaps trying to re-structure my program in another way.

I'll mark this thread as solved for now, and will update it later if I find out anything. Thanks to you, Kanoisa, for sticking with me thus far =D

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.