我正在用 C++ 编写一个数学函数库。我制作了 sqrt 函数并且效果很好。我还尝试制作一个根函数来计算数字的第 n 个根。我有 3 个文件 - emath.cpp、emath.hpp、main.cpp(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);
};
}
数学.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()
调用它作为出色地。您确定执行的是您的代码,而不是标准库中的一些代码吗?
关于c++ - "numrt": identifyer not found despite of defining that function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20363497/