Home > Blockchain >  Is there a way to deal with "error: use of undeclared identifier '' "?
Is there a way to deal with "error: use of undeclared identifier '' "?

Time:04-12

I have the following pieces of code and I am getting a bunch of same error of undeclared identifier. According to other codes I have written, the #include "rec_fun.h" on the main file should enable the use of functions in that file. Since I am compiling both rec_fun.cpp and main.cpp, this should generate an executable name FINAL.

erros:

MaryMcBeth@unknown88665a207c1a Recursion_Program % g   -std=c  14 rec_fun.cpp main.cpp -o FINAL

main.cpp:17:12: error: use of undeclared identifier 'fib_recursion'
           fib_recursion(i);// Replace with call to your function.
           ^
main.cpp:18:12: error: use of undeclared identifier 'fib_iterative'
           fib_iterative(i);
           ^
main.cpp:28:6: error: use of undeclared identifier 'triangle'
     triangle(cout, 3, 5);
     ^
main.cpp:31:6: error: use of undeclared identifier 'numbers'
     numbers(cout, "THERBLIG", 2);
     ^
4 errors generated.
MaryMcBeth@unknown88665a207c1a Recursion_Program % 

main.cpp:

#include <iostream>
#include <chrono>
#include "rec_fun.h"


using namespace std;
using namespace std::chrono;

int main() 
{
     high_resolution_clock::time_point startTime = high_resolution_clock::now();
     int i;
     for (i = 0; i < 1000000; i  )
     {
           fib_recursion(i);// Replace with call to your function.
           fib_iterative(i);
                        
     }
     high_resolution_clock::time_point endTime = high_resolution_clock::now();
     duration<float> time_span = endTime - startTime;

     std::cout << fixed;
     std::cout << "It took " << time_span.count() << " seconds.";
     std::cout << std::endl;
     cout<<"---------------"<<endl;
     triangle(cout, 3, 5);
     return 0;
     cout<<"------------------"<<endl;
     numbers(cout, "THERBLIG", 2);
     cout<<"-------------------------"<<endl;

}

rec_fun.cpp:

#include<iostream>
#include <chrono>
#include "rec_fun.h"
#include <string>

using namespace savitch2;
using namespace std;
using namespace std::chrono;



void recursion::numbers(ostream &outs, const string& prefix, unsigned int levels)

{


    if(levels == 0)

        outs << prefix << endl;

    else

    {

        for(char c = '1'; c <= '9'; c  )

        {

            s = prefix   c   '.';

            numbers(outs, s, levels-1);

        }

    }

}



bool recursion::bears(int n)
{
    

    if (n < 42) return false;
    if (n == 42) return true;
    if ((n%2) == 0)
        if (bears(n/2)) return true;
    if (((n%3)==0) || ((n%4)==0))
    {
        ones = n % 10;
        tens = (n % 100)/10;
        if ((ones != 0) && (tens != 0) && (bears(n-ones*tens)))
            return true;
    }
    if ((n%5) == 0)
        if (bears(n-42)) return true;
    return false;
}

//recursive helper method to return a string containing count number of '*'
string recursion::asterisks(int count)
{
        //if count is above 0, returning one '*' followed by the value returned
        //from recursive call to asterisks passing count-1 as argument
        if(count>0){
                return "*" asterisks(count-1);
        }
        //else, if count is 0 or negative, returning empty string
        else{
                return "";
        }
}

//required method
void recursion::triangle(ostream &outs, unsigned int m, unsigned int n)
{
        //proceeding only if m<=n.
        //if m>n, recursion stops (base case)
        if(m<=n){
                //making a string containing m number of asterisks
                string ast=asterisks(m);
                //printing it to outs
                outs<<ast<<endl;
                //making a recursive call passing m 1 as new m
                triangle(outs,m 1,n);
                //printing ast to outs once again
                outs<<ast<<endl;
        }
}


int recursion::fib_iterative(int n) {
    if(n == 1 || n == 2)
        return 1;
    int A[2][2] = { { 1, 1 },{ 1, 0 } };
    int B[2][2] = { { 1, 1 },{ 1, 0 } };
    int temp[2][2];
    while (n >= 2) {
         for (int i = 0; i < 2; i  )
             for (int k = 0; k < 2; k  ) {
                 temp[i][k] = 0;
                 for (int j = 0; j < 2; j  )
                     temp[i][k]  = A[i][j] * B[j][k];
              }
         for (int i = 0; i < 2; i  )
             for (int j = 0; j < 2; j  )
                 B[i][j] = temp[i][j];
         n--;
    }
    return B[0][1];
}

int recursion::fib_recursion(int n)
{
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return fib_recursion(n-1)   fib_recursion(n-2);
}

rec_fun.h:

#include<iostream>
#include <string>

using namespace std;

namespace savitch2
{
    class recursion
    {
        public:
            bool bears(int n);
            void triangle(ostream &outs, unsigned int m, unsigned int n);
            string asterisks(int count);
            void numbers(ostream &outs, const string& prefix, unsigned int levels);
            int fib_iterative(int n);
            int fib_recursion(int n);
        private:
            int ones;
            int tens;
            string s;

    };




}

Expected Output:

It took 8 seconds
...

CodePudding user response:

You need to do

 int i;
 recursion rec;
 for (i = 0; i < 1000000; i  )
 {
       rec.fib_recursion(i);// Replace with call to your function.
       rec.fib_iterative(i);

Since your functions are class methods, you have to create an instance of the class to use them

CodePudding user response:

You will need to create a class instance. Essentially, the functions in your program are not independent functions, they are the methods of a class(in your case, class=recursion), so getting the error: 'use of undeclared identifier' does make sense.

Create an instance of class recursion in the main.cpp file and call those functions via that instance inside the for loop.

  •  Tags:  
  • c
  • Related