Header files - where to include

This is a discussion on Header files - where to include within the C++ Programming forums, part of the General Programming Boards category; I'm currently writing some code to read a .ini file and set variables according to its contents. I've decided to ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Header files - where to include

    I'm currently writing some code to read a .ini file and set variables according to its contents. I've decided to wrap this in a very simple class. The class is declared in IniParser.h:
    Code:
    #ifndef INIPARSER_H
    #define INIPARSER_H
    
    class IniParser
    {
    public:
       void Parse(char *fileName);
    };
    
    #endif // INIPARSER_H
    IniParser::Parse is implemented in IniParser.cpp (code is probably crappy and/or buggy, so I've commented it out for now):
    Code:
    void IniParser::Parse(char *fileName)
    {
       /*
       ifstream iniFile (*fileName);
       if(!iniFile.is_open())
       {
          std::cout << "Could not open ini file: " << *fileName << std::endl;
          return 0; // Probably bad since return type is void..
       }
    
       char inputStr[128];
       int inputInt;
       float inputFloat;
    
       while(iniFile >> inputStr)
       {
          switch (*inputStr) {
             case "testVar":
                iniFile >> inputFloat; // Is this possible?
                std::cout << "testVar = " << inputFloat << std::endl;
                //TODO: Set the variable testVar to the content of inputFloat
                break;
             default:
                std::cout << "Unknown variable encountered while reading"<< *fileName << std::endl;
                break;
          }
       }*/
    }
    I'm unsure how/where I should include the header file now. If I include it from the main file like this:
    Code:
    #include "IniParser.h"
    #include "IniParser.cpp"
    I get "iniparser.cpp(5) : error C2653: 'IniParser' : is not a class or namespace name"

    If I include the header inside the IniParser.cpp I get "error LNK2005: "public: void __thiscall IniParser::Parse(char *)" (?Parse@IniParser@@QAEXPAD@Z) already defined in Shard Server.obj" - where Shard Server.obj is the main file.

    What is the 'right' way to use header files?

  2. #2
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    You dont include cpp files.. Just include header files..

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks, it works. I'm new to C++ and in the scripting language I'm used to, no header files are used, just includes of source files (no project manager or linker either).

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by C+/-
    Thanks, it works. I'm new to C++ and in the scripting language I'm used to, no header files are used, just includes of source files (no project manager or linker either).
    No problem.
    What language are you used to?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I'm guessing PHP. Most scripting languages don't include, just reference.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    C-script, the scripting language used in the A6 game engine. The c-script compiler just starts at the entry point source file and pastes the code of included files at the position of the include statements, that's all, no compilation units, linking, etc. It's a very good language to learn programming because of its simplicity (also syntactically), but very restrictive when you want to do more complex things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where to include header files?
    By kaos_frack in forum C++ Programming
    Replies: 6
    Last Post: 12-30-2008, 02:09 PM
  2. Header files
    By GaPe in forum C Programming
    Replies: 6
    Last Post: 02-01-2003, 02:39 PM
  3. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21