• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How do you use header files?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is hard to find a good example of file structure for c++ programs that really show how to use header files. I will post what I expected to work:


I kept things as minimal as possible for reading. Please help. I have researched this and found no answer. This structure is my attempt to mimic Bjarne Stroustrup's.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can think of header files as interfaces to modules which you use to import functionality into your own code...

It typically contain function declarations which are used to operate on some structure that represent a concept...

It may also contain platform configuration details and system-wide values...

One useful way to use header files is to emulate OOP abstraction as follows:

This file will be used by you which you include in your application to work with a person object


This file will be distributed as a binary object file for linking into your application... Its details are unknown to you


This is your application file which uses the interface file for working with person objects in your application...


As you can see, details of how stuff work to use a person in your application is just a matter of including the header file (interface) and using its published services...
 
Eric Mitchell
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What fix does my given code need though? I get these errors on compilation:

Error C2079 'today' uses undefined struct 'Date' Date1 c:...\projects\date1\date1\main.cpp 9
Error (active) incomplete type is not allowed Date1 c:\...\Projects\Date1\Date1\Main.cpp 9

It looks like I have #included it the same way as your example...
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically what is happening in your code that is causing the error is that, in your header file it declares and incomplete type therefore you are unable to use it until you provide a definition for the object that the compiler can use to create an object...

There is a difference with holding the address to an incomplete type and trying to create an object from an incomplete type...

If you were to change you main file to the following it will compile:


Simply because the compiler knows how big a pointer is... Remember that a pointer is simply an address in memory and the type only says how to interpret the memory at that location...

Even if you are allowed to declare a pointer to an incomplete type, it is still unusable because there is no notion defined about what the type is capable of representing... In other words there is no information describing how big the object is and how to partition the memory to represent its individual fields...

For your code to work as is you will have to move the entire definition of the structure into the header file...
 
Eric Mitchell
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this pointer usage what is most commonly used? Is this used only for structs? It seems awkward and unfamiliar to me. I know that a more common implementation of 'Date' is as a class, but I am using a struct as a practice exercise.

The reason I ask is that when I try this with Date as a class, everything works fine. Why does the compiler know the size of the Date class, but not the Date struct?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing that you mentioned the word class I assume you are programming in the C++ Programming Language...

The only difference in this language between a class and a struct is the default visibility of members...

If you know how to use a class then you know how to use a struct... Just switch the default member visibility options...
 
Would you like to try a free sample? Today we are featuring tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!