C++;头文件交互 P>,所以我对C++很陌生,我有几个关于页眉文件的问题……/P>
1.)哪些变量应该声明,哪些不应该声明在头文件中 2.)在头文件中声明变量时,是否应使用extern 这是我的头文件:C++;头文件交互 P>,所以我对C++很陌生,我有几个关于页眉文件的问题……/P>,c++,file,variables,header,global,C++,File,Variables,Header,Global,1.)哪些变量应该声明,哪些不应该声明在头文件中 2.)在头文件中声明变量时,是否应使用extern 这是我的头文件: #ifndef MAIN_H #define MAIN_H class Main { public: int main(); //Constructor virtual ~Main(); //Destructor double initialVelocity; double initialAngle; pri
#ifndef MAIN_H
#define MAIN_H
class Main
{
public:
int main(); //Constructor
virtual ~Main(); //Destructor
double initialVelocity;
double initialAngle;
private:
double degToRad(double angle);
void simulate(double angle, double velocity);
};
#endif
这是我的主要.cpp
/*******************************************************************
* This program will take input for initial velocity (fps), and a launch angle
* based on this information, the current posotion of the object thrown will be
* calculated until it hits the ground.
*
*
* Date: 30 August 2013
* Version 1.0
*
**/
# include "Main.h"
# include <iostream>
# include <fstream>
# include <cmath>
using namespace std;
/******************************************************************
* General Variables
**/
const int GRAVITY_FACTOR = -16;
const int GROUND = 0;
const double PI = atan(1.0)*4;
double initialVelocity;
double initialAngle;
/******************************************************************
* degToRad function.
*
* This function takes in an angle in degrees, and converts it to
* radians.
*
**/
double degToRad(double angle){
return angle * (PI/180);
}
/******************************************************************
* simulate function.
*
* Takes in the angle in radians, and the velocity. Calculates the
* path of the projectile, and displays it in the terminal.
*
**/
void simulate(double angle, double velocity){
cout << "Entering Simulation" << endl;
double time = 0;
double x = 1;
double y = 1;
double veloUp = 0;
double veloFo = 0;
veloUp = (velocity*sin(angle));
veloFo = (velocity*cos(angle));
cout << "Angle in radians: " << angle << endl;
cout << "Initial velocity upwards (fps): " << veloUp << endl;
cout << "Initial velocity forward (fps): " << veloFo << endl;
while(y >= GROUND){
x = veloFo * time;
y = GRAVITY_FACTOR*(time*time) + (veloUp * time);
cout << "(x, y) at time " << time << " is (" << x << ", " << y << ")" << endl;
++time;
} //while
cout << "Leaving Simulation" << endl;
} //simulate
/***************************************************************************
* The main function.
*
* Produces output to the console in order to coach the user on what to input.
**/
int main()
{
cout << "Execution Beginning" << endl;
cout << "Enter the inital velocity (feet per second):" << endl;
cin >> initialVelocity;
if(initialVelocity > 0){
cout << "Good. " << initialVelocity << " is a valid value for the initial velocity." << endl;
}
else{
cout << "ERROR: " << initialVelocity << " is not a valid value for the initial velocity." <<endl;
return 0;
}
cout << "Enter the initial angle in degrees (from the horizontal):" << endl;
cin >> initialAngle;
if(initialAngle >= 0 && initialAngle <= 90){
cout << "Good. " << initialAngle << " is a valid value for the initial angle." << endl;
}
else{
cout << "ERROR: " << initialAngle << " is not a valid value for the initial angle." << endl;
return 0;
}
simulate(degToRad(initialAngle), initialVelocity);
cout << "Ending Execution" << endl;
return 0;
}
/*******************************************************************
*该程序将获取初始速度(fps)和发射角度的输入
*基于此信息,将显示抛出对象的当前位置
*直到它落地。
*
*
*日期:2013年8月30日
*版本1.0
*
**/
#包括“Main.h”
#包括
#包括
#包括
使用名称空间std;
/******************************************************************
*一般变量
**/
重力系数常数=-16;
常数int地面=0;
常数双PI=atan(1.0)*4;
双初始速度;
双初始角;
/******************************************************************
*degToRad函数。
*
*此函数以度为单位获取角度,并将其转换为
*弧度。
*
**/
双除气器(双角度){
返回角*(PI/180);
}
/******************************************************************
*模拟功能。
*
*以弧度表示的角度和速度。计算
*投射物的路径,并在终端中显示。
*
**/
孔隙模拟(双角度、双速度){
cout在头文件中,可以声明将在cpp文件中实现的所有类字段和函数。
您不应该声明将在类函数中使用的变量(因为有类似于临时变量的变量),但是您可以声明(并且应该声明)将在程序中使用的常量变量
你不应该使用extern,extern用于你没有定义的变量,编译器也不会为此而困扰你,因为当它是extern时,这意味着变量是在你的程序中的其他地方定义的。
ExtEnter也用于处理C++代码,就像在C.一样。
比如:
外部“C”{//code.}
或extern“C”int func(int a);
总之-
.h文件中的delcare const变量,以及cpp中的classes.else-变量(除非是内联变量)。
就你而言:
const int GRAVITY_FACTOR = -16;
const int GROUND = 0;
const double PI = atan(1.0)*4;
are better to be in the .h file
+
您不应再次申报:
double initialVelocity;
double initialAngle;
它已经在.h文件中
希望它有用。我想你需要知道两件事:
- 有什么问题吗。
- 您需要一个声明来使用类或函数(让编译器知道它在那里)
- 定义是编译的代码(函数体、初始化)
包括哪些功能。
#include
只需将头的内容复制到包含它的源文件中即可
这意味着您可能不希望在头文件中包含定义,因为如果多个文件包含头文件,则会违反,从而在链接阶段导致多个定义错误
哪些变量应该声明,哪些不应该声明在头文件中
头文件是要调用函数/使用类的其他源文件的接口。
因此,您为其他源文件工作所需的内容添加声明
在头文件中声明变量时,是否应使用extern
extern
关键字指定变量的声明而不是定义。
这意味着您的变量定义在其他地方(例如,您的源文件)
如果要在另一个源文件中使用GRAVITY\u FACTOR
,其中包括标题:
- 在源代码中:
const int GRAVITY\u FACTOR=-16;
- 小时标题:
extern const int GRAVITY\u FACTOR;
备注:
- 当前头文件中有一个类定义
- 公共的和私人的都是你班上的成员
- 您可能更希望在此处使用名称空间或普通函数
main
函数与类定义内部无关
你那里有一个有趣的“构造函数”名称。(顺便说一句,它不是构造函数)@WhozCraig它吓到我了**注意,你从来没有真正创建过class对象main
。这个类有什么意义?你也没有实现class main
@Trevor请更改int main()中的'm'是不是有更好的方法来构造一个构造函数?或者是必须的?整个头文件让我困惑,我来自java java,在那里,一切看起来都更简单。是的,谢谢你。安顿。总结一下,在Java中,你可以在类的顶部声明类变量,C++中同样的东西是与头文件类似?