许多语言(如Java,C#)不会将声明与实现分开.C#有一个部分类的概念,但实现和声明仍然保留在同一个文件中.
为什么C++没有相同的型号?拥有头文件更实用吗?
我指的是当前和即将推出的C++标准版本.
我已经学习了 Java 课程,并正在尝试使用 K&R 自学 C。到目前为止一切顺利,但我不明白原型的目的。请参阅下面代码中的 2 // 注释:
#include <stdio.h>
float convert(int); **//Why is this needed...**
main()
{
int i;
for(i = 0; i <= 300; i += 20)
printf("F: %3d C: %6.1f\n",i,convert(i));
system("Pause");
return 0;
}
float convert(int f) **//When we already have this?**
{
float c = (5.0/9.0) * (f-32.0);
return c;
}
Run Code Online (Sandbox Code Playgroud)
在 Java 中,您可以声明类似的函数public static float convert(int f)
,但根本不需要原型。这对我来说似乎简单得多。为什么有区别?
我收到所有这些错误:
未定义引用'getLength()'
未定义引用'getWidth()'
未定义引用'getArea(double,double)'
未定义引用'displayData(double,double,double)'
这是我的代码:
#include <iostream>
using namespace std;
double getLength();
double getWidth();
double getArea(double,double);
void displayData(double,double,double);
int main()
{
double length;
double width;
double area;
length = getLength();
width = getWidth();
area = getArea(length,width);
displayData(length,width,area);
return 0;
}
//getLength function
double getLength();
{
double length;
cout << "Length: ";
cin >> length;
return length;
}
//getWidth function
double getWidth();
{
double width;
cout << "Width: ";
cin >> width;
return width;
}
//GetArea function
double getArea(double lenght, …
Run Code Online (Sandbox Code Playgroud)