简体   繁体   English

有没有办法处理“错误:使用未声明的标识符''”?

[英]Is there a way to deal with "error: use of undeclared identifier '' "?

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.根据我编写的其他代码,主文件中的#include "rec_fun.h" 应该允许使用该文件中的函数。 Since I am compiling both rec_fun.cpp and main.cpp, this should generate an executable name FINAL.因为我正在编译 rec_fun.cpp 和 main.cpp,所以这应该生成一个可执行文件名称 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:主.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: 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: 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:预计 Output:

It took 8 seconds
...

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由于您的函数是 class 方法,因此您必须创建 class 的实例才能使用它们

You will need to create a class instance.您将需要创建一个 class 实例。 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.在 main.cpp 文件中创建 class 递归实例,并通过 for 循环内的该实例调用这些函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM