"numrt":尽管定义了该函数,但未找到标识器

"numrt": identifyer not found despite of defining that function

本文关键字:标识器 函数 numrt 定义      更新时间:2023-10-16

我正在用C++编写一个数学函数库。我做了sqrt函数,它运行得很好。我还试图制作一个根函数,计算一个数字的第n个根。我有三个文件-emath.cpp,emath.hpp,main.cpp(emath是我的库的名称)。

emath.cpp:

namespace emath
    {
    class emath
        {
        public:
           /* ... */
        //Returns value of square root of a number.
        static double sqrt(double tur);
        //Rerurns value of a root of a number.
        static double numrt(int stopien, double tur);
    };
}

emath.hpp:

#ifndef EMATH_HPP
#define EMATH_HPP
using namespace std;
namespace emath
    {
    class emath
        {
        public:
        /* ... */
        int emath::derivative(double x)
        {
              return 0;
        }
        //****************************************************************
        double emath::derivative(double number, int pow)
        {
              double result = 1;
              for (int i = 1; i < pow; i++)
              {
                    result *= number;
              }
              result *= pow;
              return result;
         }
        //************************************************************************
        double emath::derivative(double number, int pow, double numbey)
        {
             return ((derivative(number, pow) + derivative(numbey)));
        }
        //************************************************************************
        double emath::sqrt(double tur)          //This function
        {
    int a;
    static int i = 0;
    for (; i * i <= tur; i++)
    {
    }
    if (i * i == tur)
    {
        return i;
    }
    a = i;
    return sqrtl(a);
}
//************************************************************************
double emath::numrt(int stopien, double tur)          //This function
{
    int a;
    static int i = 0;
    for (; i * i <= tur; i++)
    {
    }
    if (i * i == tur)
    {
        return i;
    }
    a = i;
    static int mul = 1;
    for (int p = 0; p < stopien; p++)
    {
        mul *= i;
    }
    return numrtel(mul, i, a);
}



private:
//****************************************************************
double emath::sqrtel(double tur, static int k = 0)  //Implementation of Newton's Method
{
    double a;                                                      //Creates a - our xn.
    if (k == 1000000)
    {
        return a;
    }
    a = a - ((a * a - tur) / derivative(a, 2, - tur));          //Proper implementation os Newton's Method
    ++k;                                                        //k is counter
    return sqrtel(a);
}
//***************************************************************
double emath::numrtel(int mul, int i, double tur, static int k = 0)
{
    double a;                                                      //Creates a - our xn.
    if (k == 1000000)
    {
        return a;
    }
    a = a - ((mul - tur) / derivative(a, i, - tur));          //Proper implementation os Newton's Method
    ++k;                                                        //k is counter
    return numrtel(mul, i, a);
}
};  //end of class
}   //end of namespace
#endif // EMATH_HPP

如果我的main.cpp是

#include <iostream>
#include "emath.hpp"
using namespace std;
using namespace emath;
int main()
{
    double number;
    cin >> number;
    cout << sqrt(number);
}

它运行得很好,但如果main.cpp是

#include <iostream>
#include "emath.hpp"
using namespace std;
using namespace emath;
int main()
{
    double number;
    cin >> number;
    cout << numrt(3, number);
}

我得到一个编译器错误:"numrt":找不到标识符。我使用的是带有Qt Creator的MS VS Studio 2012编译器。为什么?

由于在类emath中声明了numrt(),因此必须在函数调用中声明该类名:

emath::numrt()

(完整调用,包括名称空间名称为emath::emath::numrt()。)

我想知道为什么对sqrt()的调用有效,因为要执行代码,还必须使用emath::sqrt()来调用它。您确定您的代码是执行的,而不是标准库中的某些代码吗?