473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you define structs in header files?

7 New Member
Currently I have (a much bigger version of) the code below in my main cpp file:

struct myData
{
int dataCodeNum;
char* dataName;
}

myData arrayOfData1[]={
{ 1, "Data 1.1" },
{ 2, "Data 1.2" },
{ 3, "Data 1.3" },
};

myData arrayOfData2[]={
{ 1, "Data 2.1" },
{ 2, "Data 2.2" },
{ 3, "Data 2.3" },
};

I need the struct definition and the arrays of structs to be accessible to many functions and classes throughout my program, but I was hoping to get them out of the main cpp interface file.

I was thinking about plopping the whole thing in a header file and then #include-ing that file to all other files that would use these arrays of structs, but that just seems wrong to have objects in the header file... I mean, when you have a class, you only put the class definition in the header file, and your objects get declared in some cpp file...

Thus far everything I have read or looked up has shown how to create structs, but I am having a hard time finding any information about putting these structs (and/or their objects) in separate files.

Thanks.
Jul 18 '08 #1
4 19327
newb16
687 Contributor
.h:
Expand|Select|Wrap|Line Numbers
  1. struct myData
  2. {
  3. int dataCodeNum;
  4. char* dataName;
  5. }
  6.  
  7. extern myData arrayOfData1[];
  8.  
.c:
Expand|Select|Wrap|Line Numbers
  1. myData arrayOfData1[] = { {0,"aa"}, {1,"bb"} };
  2.  
Jul 19 '08 #2
KioKrofov
7 New Member
So then, it is not considered poor programming practice to place implementation (by defining an object of the struct) in a header file?

I thought you were not supposed to declare variables (in this case an object of the struct) in header files because you will get a linking error "already defined in *.obj"
Jul 21 '08 #3
oler1s
671 Recognized Expert Contributor
it is not considered poor programming practice to place implementation (by defining an object of the struct) in a header file?
The array was declared (notice the use of extern).

I thought you were not supposed to declare variables
You're not supposed to define something. You can't have multiple definitions of a variable.
Jul 21 '08 #4
KioKrofov
7 New Member
Oh okay! Thanks much
Jul 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
25434
by: srinivas reddy | last post by:
I have defined some variables using #define preprocessor instruction. And then later I checked whether I had defined a variable. Sometimes even though a variable have been defined, #if !defined(var) construct is returning true. I am using gcc3.0.1 on SunOS8. I would appreciate if any body can tell me whether this is a bug or I am doing something wrong. tia, Srinivas
10
2073
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this: typedef struct { char rsvd0; char iadl1;
3
3669
by: MSDousti | last post by:
Hello I want to write a C# (or VB.NET) program which reads the structure of a PE (odinary win32 executable) file. These files have a long header (512 bytes or so). The definition of the header exists in WINNT.h file (you can see many "struct"s there). Here comes my problem: how can I read a file and "fill" the structs?
4
3143
by: MSDousti | last post by:
Hi all, I read some Q&As in the Net, discussing Marshalization of nested structs in C# (or VB.NET). Some guys stated that .NET framework does not support this feature yet. Are they right? BTW, by nested structs I mean structures such as this:
2
1328
by: Stephen Walch | last post by:
I have a Managed C++ library that uses a number of unmanaged libraries and my implementation necessarily includes a number of external C and C++ header files (for example STL includes and third party .h files). The resulting assembly works, but when I look at it under VS.NET's Object Browser (and other tools) I see tons of contants and structs from the include files that I never intended to be part of the public interface. One...
17
2569
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
14
2443
by: raghu | last post by:
Hello I have a doubt plz clarify that #ifndef SYSTEM_H #define SYSTEM_H what will be the value of SYATEM_H after this #define statement and before that statement. Thanking you all Bye
4
2786
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these i am not getting any errors .
23
3913
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
0
8921
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.