所以,我有一个应用程序在DOS中使用一堆数学公式选择数字样式。所以我创建了一个静态库,我现在将它们链接起来,但是当我尝试使用一个函数时,它会说没有检测到成员。这是我的代码(重要部分): //库头文件:
namespace Library_ {
static float min, max, n, rem, i, r, l, w, m, v, k, r1, r2, r3, r4, h, b,
b1, b2, s, value, count = 0; static float f = 1;
static class Main
{
public:
class Geometry_2Dimensional
{
static void rectangle();
static void circle();
static void ETGRadius();
static void ETGSide();
};
class Geometry_3Dimensional
{
static void prismRectangle();
static void prismTriangle();
static void cylinder();
static void sphere();
};
class Computing
{
static void rng();
static void binary(int num);
};
class Number_Theory
{
static void average();
static void primeCheck();
static void primeFactors();
static void factorial();
};
static void Error();
};
}
//库源文件:
include "CalculatorLib.h"
include <stdexcept>
include <iostream>
include <time.h>
include <math.h>
include <cstdlib>
include <conio.h>
using namespace std;
namespace Library_
{
void Main::Error()
{
cout << "Not a valid option." << endl;
system("PAUSE");
}
void Main::Geometry_2Dimensional::rectangle()
{
cout << "Length: "; cin >> l;
cout << "Width: "; cin >> w;
cout << "Area: " << l * w << "\nPerimeter: " << (2 * l) + (2 * w) <<
endl;
system("PAUSE");
}
void Main::Geometry_2Dimensional::circle()
{
cout << "Radius: "; cin >> r;
cout << "Area: " << r * 3.14 * r << "\nCircumference: " << r * 6.28 <<
endl;
system("PAUSE");
}
void Main::Geometry_2Dimensional::ETGRadius()
{
cout << "Integer part: "; cin >> r1;
cout << "Radicant: "; cin >> r2;
r3 = sqrt(r2);
m = (sqrt(3) / 3);
if (r2 == 3)
{
n = r1 * 3;
cout << "The side length of the triangle is: " << n << endl;
v = n / 2;
k = sqrt((n * n) - (v * v));
cout << "The area of the triangle is: " << (k * n) / 2 << endl;
system("PAUSE");
}
else
{
r4 = r3 * r1;
n = r4 / m;
cout << "The side length of the triangle is: " << n << endl;
v = n / 2;
k = sqrt((n*n) - (v*v));
cout << "The area of the triangle is: " << (k * n) / 2 << endl;
system("PAUSE");
}
}
void Main::Geometry_2Dimensional::ETGSide()
{
cout << "Enter the side length: "; cin >> s;
r = (s / 3) * sqrt(3);
cout << "The radius of the inner circle is: " << r << endl;
v = s / 2;
k = sqrt((s*s) - (v*v));
n = k * s;
cout << "The area of the triangle is: " << n / 2 << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::prismRectangle()
{
cout << "Length: "; cin >> l;
cout << "Width: "; cin >> w;
cout << "Height: "; cin >> h;
cout << "Area: " << l*w*h << "\nSurface Area: " << (2 * (l*w)) + (2 *
(l*h)) + (2 * (h*w)) << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::sphere()
{
cout << "Radius: "; cin >> r;
cout << "Volume: " << 3.14 * (4 / 3) * r * r * r << "\nSurface Area: "
<< 12.56 * r * r << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::cylinder()
{
cout << "Radius: "; cin >> r;
cout << "Height: "; cin >> h;
cout << "Volume: " << 3.14 * r * r * h << "\nSurface Area: " << 6.28 * r
* h * (r + h) << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::prismTriangle()
{
cout << "Base of the base: "; cin >> b;
cout << "Height: "; cin >> h;
cout << "Length: "; cin >> l;
cout << "Other sides of base: " << "\nFirst other side: "; cin >> b1;
cout << "Second other side: "; cin >> b2;
cout << "Volume: " << h * b * l << "\nSurface Area: " << (2 * b * h) +
((b + b1 + b2) * l) << endl;
system("PAUSE");
}
void Main::Computing::rng()
{
cout << "Enter the minimum: "; cin >> min;
cout << "Enter the maximumL "; cin >> max;
cout << "Enter the number of generations: "; cin >> n;
int range = max - min + 1;
unsigned first = time(NULL);
srand(first);
for (i = 0; i < n; i++)
{
r = rand() / 100 % range + min;
cout << r << " ";
}
cout << endl;
system("PAUSE");
}
void Main::Computing::binary(int num)
{
if (num <= 1)
{
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}
void Main::Number_Theory::average()
{
cout << "Enter the number of values: "; cin >> n;
float average(0);
for (int i = 0; i < n; ++i)
{
cin >> value;
average += value;
}
average /= n;
cout << "Average is " << average << endl;
system("PAUSE");
}
void Main::Number_Theory::factorial()
{
cout << "Enter the number: "; cin >> n;
for (int a = 1; a <= n; a++)
{
f *= a;
}
cout << "The factorial is: " << f << endl;
system("PAUSE");
}
void Main::Number_Theory::primeCheck()
{
cout << "Enter the number: "; cin >> n;
int count = 0;
for (int a = 1; a <= n; a++)
{
if (fmod(n, a) == 0)
{
count++;
}
}
if (count == 2)
{
cout << "The number " << n << " is prime!" << endl;
system("PAUSE");
}
else
{
cout << "The number " << n << " is not prime." << endl;
system("PAUSE");
}
}
void Main::Number_Theory::primeFactors()
{
cout << "Enter the number: "; cin >> n;
while (fmod(n, 2) == 0)
{
cout << "2, ";
n /= 2;
}
for (int i = 3; i <= sqrt(n); i += 2)
{
while (fmod(n, 1) == 0)
{
cout << i << ", ";
n /= i;
}
}
if (n > 2)
{
cout << n << ", ";
}
}
}
然后是主要的源文件,它不能使用任何函数,因为intellisense没有包含任何成员。 附注:我包括头文件和使用命名空间Library_。 如果这似乎很匆忙,那是因为我必须下车。而且,除了解决这个问题,有人能告诉我何时何时不使用静态? 谢谢!
答案 0 :(得分:1)
你在这里犯了很多错误很难知道从哪里开始,使用类就好像它们是命名空间一样,使用全局变量,在库代码中执行输入和输出,不使用函数参数和函数返回,使用静态不正确等等也无法回答你的问题,因为你没有显示 你试图调用你的功能。
我可以回答一个问题,即'何时使用静电'。根据您的经验,答案是永远不会使用静态。你必须先学习基础知识。
忘记课程。忘记命名空间,忘记全局变量,首先学习如何编写函数,如何使用局部变量,如何将值传递给函数并从函数返回值。
例如,以下是完全可以接受的初学者代码
void Geometry_2Dimensional_ETGRadius(float r1, float r2, float& n, float& k)
{
float r3 = sqrt(r2);
float m = (sqrt(3) / 3);
if (r2 == 3)
{
n = r1 * 3;
float v = n / 2;
k = sqrt((n * n) - (v * v));
}
else
{
float r4 = r3 * r1;
n = r4 / m;
float v = n / 2;
k = sqrt((n*n) - (v*v));
}
}
int main()
{
float r1, r2, n, k;
cout << "Integer part: "; cin >> r1;
cout << "Radicant: "; cin >> r2;
Geometry_2Dimensional_ETGRadius(r1, r2, n, k);
cout << "The side length of the triangle is: " << n << endl;
cout << "The area of the triangle is: " << (k * n) / 2 << endl;
system("PAUSE");
}
查看函数内部仅使用的变量(r3
,m
等)是如何在函数内声明的。函数所需的值被声明为函数的参数(r1
,r2
),函数返回的值被声明为参考参数(n
和k
)。这是基本的东西,在你尝试更高级的东西之前,你应该熟悉它。