dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Noob question: Copied functions to header file, now tons of errors

  1. #1
    Join Date
    May 2018
    Posts
    2

    Noob question: Copied functions to header file, now tons of errors

    Hello,

    Im a n00b with c++ and was building my entire program in the main.cpp, including all function defenitions.

    This gave troubles though, because i couldnt call functions from functions that were declared below that line. Eg:

    Code:
    string dosomething() {
     dothis(); // errors because dothis isnt declared yet
    }
    void dothis() {
    //do stuff
    }
    I tried to fix this by copying all my functions / includes / variable declarations to a header file, and include that in my .cpp file, but now i have hundreds of strange errors, like:
    string not declared,
    all the function names are suddenly unindentified

    What did i do wrong? Its probably some really big n00b mistake, but still

  2. #2
    Join Date
    May 2018
    Posts
    2

    Re: Noob question: Copied functions to header file, now tons of errors

    nvm, the header file should only include declarations, not the actual function contents

  3. #3
    Join Date
    Feb 2017
    Posts
    351

    Re: Noob question: Copied functions to header file, now tons of errors

    Quote Originally Posted by PresFox View Post
    nvm, the header file should only include declarations, not the actual function contents
    My programs have header files only (and just one .cpp file holding the program entry point). It works fine. This is a typical example of a .h file (called hasher.h) holding free functions (what they do doesn't matter),

    Code:
    #ifndef HASHER_XYZ_ONCE
    #define HASHER_XYZ_ONCE
    
    #include <cstddef>
    #include <initializer_list>
    #include <functional>
    
    namespace maux {
    
    		// combines the hash values of seed and value (modified from Boost)
    	inline std::size_t hash_combine(const std::size_t seed, const std::size_t value) {
    		return seed ^ (value + 0x9e3779b9 + (seed<<6) + (seed>>2));
    	}
    
    		// combines all hash values in the initializer list
    	inline std::size_t hash_combine(const std::initializer_list<std::size_t> ilst) {
    		std::size_t seed = 0;
    		for (std::size_t value : ilst) seed = hash_combine(seed, value);
    		return seed;
    	}
    }
    
    #endif
    The important components are:

    1. The include guard.

    #ifndef HASHER_XYZ_ONCE
    #define HASHER_XYZ_ONCE
    .....
    #endif

    This ensures this .h file is included once only. The identifier HASHER_XYZ_ONCE must be unique for one .h file and not appear anywhere else in the whole of the program. Define an include guard in every .h file.

    2. Necessary includes.

    #include <cstddef>
    #include <initializer_list>
    #include <functional>

    This makes sure the functions of this particular .h file have access to everything they need to work.

    3. A namespace

    namespace maux {
    .....
    }

    Not strictly necessary but it's a good idea to subdivide a program into a number (not too many) of namespaces in order to avoid name clashes. maux:: works and is used just like std::. It represents a collection of language elements (like functions, classes, enums, constants, etcetera) that naturally belong together. One can use the same namespace in several .h files. I use the maux:: namespace for all auxiliary/utility functions.

    4. Functions are declared inline.

    inline std::size_t hash_combine(const std::size_t seed, const std::size_t value)

    This is probably the main reason your code didn't work. If you have many translation units (essentially .cpp files) and don't declare a free function inline then the compiler may generate code for it in several translation units and the linker will complain about name clashes. So declare free functions inline.
    Last edited by wolle; May 30th, 2018 at 01:35 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,461

    Re: Noob question: Copied functions to header file, now tons of errors

    Quote Originally Posted by PresFox View Post
    Hello,

    Im a n00b with c++ and was building my entire program in the main.cpp, including all function defenitions.

    This gave troubles though, because i couldnt call functions from functions that were declared below that line. Eg:

    Code:
    string dosomething() {
     dothis(); // errors because dothis isnt declared yet
    }
    void dothis() {
    //do stuff
    }
    I tried to fix this by copying all my functions / includes / variable declarations to a header file, and include that in my .cpp file, but now i have hundreds of strange errors, like:
    string not declared,
    all the function names are suddenly unindentified

    What did i do wrong? Its probably some really big n00b mistake, but still
    The 'easy' fix for this is to forward declare functions at the start of the program before their use and define them where required. Consider

    Code:
    void dothis();
    string dosomething();
    ...
    
    string dosomething() {
     dothis(); // errors because dothis isnt declared yet
    }
    void dothis() {
    //do stuff
    }
    Now dothis() and dosomething() can be used before they are defined. It is common practice to produce such function forward declarations for every function defined and put these declarations into a header file which is then included. The function definitions can now be placed where required - in the header (but see Wolle's post), in the main .cpp file (either before or after main()) or in a different compilation unit (.cpp file).

    See 1.9 and other sections of http://www.learncpp.com/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)