Question

Trying to use header file and getting [Error] 'factorialIterative' was not declared in this scope I...

Trying to use header file and getting [Error] 'factorialIterative' was not declared in this scope

I am learning how to use more than just one source code while programming and I keep getting this error even though I believe I have declared my function. Heres my code

BinomialFunctions.hpp

#ifndef _BINOMIALFUNCTIONS_H_
#define _BINOMIALFUNCTIONS_H_

#include <iostream>
#include <cmath>
using namespace std;

// global definitions
typedef long int bigint; // give long int an alias name of bigint

// Function prototypes for our BinomialFunctions library go here
class BinomialFunctions
{
   private:
       bigint n;
       bigint i;
      
      
   public:
       bigint factorialIterative(bigint n);
       bigint factorialRecursive(bigint n);
       bigint countCombinationsDirectly(bigint n, bigint i);
       bigint countCombinationsRecursive(bigint n, bigint i);
};
#endif // _BINOMIALFUNCTIONS_H_

BinomialFunctions.cpp

#include "BinomialFunctions.hpp"

using namespace std;

bigint BinomialFunctions::factorialIterative(bigint n)
{
   bigint sum;
   sum = 0;
   if(n<=0)
   {
       return 1;
   }
  
   for(int index =0; index<n; index++)
   {
       sum += index * n;
   }
   return sum;
}

Assignment04.cpp

#include <iostream>
#include <cassert>
#include <chrono> // measure elapsed time of functions using high resolution clock
#include "BinomialFunctions.hpp"

using namespace std;

int main(int argc, char** argv)
{
// test iterative version of factorial
cout << "Testing iterative version factorialIterative() " << endl;
cout << "-------------------------------------------------------------" << endl;
bigint res;
  
res = factorialIterative(0); //[Error] 'factorialIterative' was not declared in this scope
cout << "Test base case: factorialIterative(0) = " << res << endl;
assert(res == 1);
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The problem was with your code in main(). All the methods in header file BinomialFunctions.hpp are written inside the class BinomialFunctions, and you were trying to access it without creating an object of that class. So all I had to do was create an object of BinomialFunctions class and use it to call factorialIterative(). You can only call a method directly if it is written outside a class or any other structures. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

/*corrected code for Assignment04.cpp, modified part is highlighted in bold text*/

#include <iostream>

#include <cassert>

#include <chrono> // measure elapsed time of functions using high resolution clock

#include "BinomialFunctions.hpp"

using namespace std;

int main(int argc, char** argv)

{

    // test iterative version of factorial

    cout << "Testing iterative version factorialIterative() " << endl;

    cout << "-------------------------------------------------------------" << endl;

    bigint res;

    BinomialFunctions object; //creating an object of BinomialFunctions

    //calling factorialIterative() using object.

    res = object.factorialIterative(0);

    cout << "Test base case: factorialIterative(0) = " << res << endl;

    assert(res == 1);

    return 0;

}

/*OUTPUT*/

Testing iterative version factorialIterative()

-------------------------------------------------------------

Test base case: factorialIterative(0) = 1

Add a comment
Know the answer?
Add Answer to:
Trying to use header file and getting [Error] 'factorialIterative' was not declared in this scope I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT