- #1
JonnyG
- 233
- 30
I am trying to call a function declared in a .hpp file and defined in the corresponding .cpp file, from my main.cpp file, but I keep getting an error. From what I have googled it seems as if I am doing this the right way, so I was hoping you guys could help out. Here's my code:
The error that I'm getting is:
C:/Users/J/Desktop/CPP/CppPrimer/CppPrimer/main.cpp: In function 'int main()':
C:/Users/J/Desktop/CPP/CppPrimer/CppPrimer/main.cpp:19:56: error: 'absoluteVal' was not declared in this scope
For what it's worth I am using the CodeLite IDE and a mingw-w64 compiler. I'd appreciate any help. Hopefully it's just me making a silly mistake.
Chap.hpp:
#ifndef CHAP_HPP
#define CHAP_HPP
class Chap
{
public:
double absoluteVal (double n) ;
unsigned fact (unsigned n) ;
};
#endif // CHAP_HPP
Chap.cpp:
#include "Chap.hpp"double absoluteVal (double n) {
return (n >= 0) ? n : -n;
}
unsigned fact (unsigned n) {
unsigned product {1};
for (unsigned i = 1; i <= n; ++i)
product *= i;
return product;
}
main.cpp:
#include <iostream>
#include "Chap.hpp"
using std::cout;
using std::cin;
using std::endl;
int main() {
cout << "Enter a number: ";
double n;
cin >> n;
cout << "The absolute value of " << n << " is " << absoluteVal(n) << endl;
return 0;
}
The error that I'm getting is:
C:/Users/J/Desktop/CPP/CppPrimer/CppPrimer/main.cpp: In function 'int main()':
C:/Users/J/Desktop/CPP/CppPrimer/CppPrimer/main.cpp:19:56: error: 'absoluteVal' was not declared in this scope
For what it's worth I am using the CodeLite IDE and a mingw-w64 compiler. I'd appreciate any help. Hopefully it's just me making a silly mistake.