-
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.
-
Your implementation of abs() function is declared within headerfunct context, so you need to create an object of this structure.
-
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