Including header

This is a discussion on Including header within the C++ Programming forums, part of the General Programming Boards category; Hi,, I am trying to include a function from a header file named headerfunt.h . The code of my header ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Including header

    Hi,, I am trying to include a function from a header file named headerfunt.h . The code of my header file is
    Code:
    #ifndef HEADERFUNCT_H_INCLUDED
    #define HEADERFUNCT_H_INCLUDED
    #include <iostream>
    using namespace std;
    struct headerfunct{
       int abs(int arg)
    {
        if(arg<0)
        {
            return -arg;
        }
        else{return arg;}
    }
    
     };
    #endif // HEADERFUNCT_H_INCLUDED
    and the source code is
    Code:
    #include <iostream>
    using namespace std;
    #include "headerfunct.h"
    int main()
    {
    int j;
    while(cin>>j)
    {
        cout<<"Abs of "<<j<<" is "<<abs(j)<<endl;
    }
    }
    But, while compiling it says abs was not declared... I have included the file. so wheres the problem. Thanks in advance.

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    102
    Your implementation of abs() function is declared within headerfunct context, so you need to create an object of this structure.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Thanks. It worked.
    Code:
    #ifndef HEADERFUNCT_H_INCLUDED
    #define HEADERFUNCT_H_INCLUDED
    #include <iostream>
    using namespace std;
    
       int abs(int arg)
    
    {
    
        if(arg<0)
    
        {
    
            return -arg;
    
        }
    
        else{return arg;}
    
    }
    
     
    
     #endif // HEADERFUNCT_H_INCLUDED

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including header files
    By 127.0.0.1 in forum C Programming
    Replies: 2
    Last Post: 06-13-2011, 08:48 AM
  2. header file including
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 01-16-2011, 07:31 AM
  3. Including my own header files
    By bushymark in forum C Programming
    Replies: 17
    Last Post: 11-03-2009, 08:09 PM
  4. Including header files
    By Emeighty in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2008, 03:02 PM
  5. Including string with header?
    By theJ89 in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 01:43 AM

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