我真的希望这符合指导原则,但我有一个潜在的愚蠢问题:我试图很好地了解多个头文件和源文件在C ++程序中是如何组合在一起的。为此,我为简单的物理公式制作计算器。
#include "stdafx.h"
#include "rotationalKinematics.h"
#include "Kinematics.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//declare local variables:
int fieldChoice = 0;
//query field of equations:
cout << "What physical properties would you like to solve for?\n (1) kinematics\n (2) rotational kinematics\n Please enter number: ";
cin >> fieldChoice;
if (fieldChoice == 1) {
int mainKinematics();
return 0;
}
if (fieldChoice == 2) {
int mainRotationalKinematics();
return 0;
}
return 0;
}
程序编译时没有错误,两个函数mainKinematics()和mainRotationalKinematics()分别在单独的.cpp和.h文件中定义和声明。例如,我在下面的mainKinematics()中包含了一个标题和一个源文件:
#include "stdafx.h"
#include "Kinematics.h"
#include <iostream>
#include <cmath>
using namespace std;
//function declaration:
double kinDeltaX_acceleration(double velocityInitial, double deltaTime, double acceleration);
int mainKinematics()
{
//local variable declaration:
double velocityInitial = 0;
double velocityFinal = 0;
double velocityFinalSquared = 0;
double acceleration = 0;
double deltaX = 0;
double deltaTime = 0;
int solveFor = 0;
//query variable to solve for:
cout << "What variable would you like to solve for?\n (1) DeltaX w.r.t. acceleration\n (2) DeltaX w.r.t. velocity\n (3) Final velocity\n (4) Final velocity squared\n Please enter number: ";
cin >> solveFor;
//query for values based on solveFor choice:
if (solveFor == 1) {
cout << "Please enter value for velocityInitial: ";
cin >> velocityInitial;
cout << "Please enter value for deltaTime: ";
cin >> deltaTime;
cout << "Please enter value for acceleration: ";
cin >> acceleration;
//function call:
deltaX = kinDeltaX_acceleration(velocityInitial, deltaTime, acceleration);
cout << deltaX;
}
if (solveFor != 1 && solveFor != 2 && solveFor != 3 && solveFor != 4)
{
return 0;
}
return 0;
}
//functions:
double kinDeltaX_acceleration(double velocityInitial, double deltaTime, double acceleration)
{
//local variable declaration:
double result;
result = velocityInitial*deltaTime + .5 * acceleration * pow(deltaTime, 2);
cin.clear();
cin.sync();
cin.get();
return result;
}
和相关的Kinematics.h头文件:
#ifndef Kinematics
#define Kinematics
#pragma once
//function declarations:
double kinDeltaX_acceleration(double velocityInitial, double deltaTime, double acceleration);
double kinDeltaX_velocity(double velocityInitial, double velocityFinal, double deltaTime);
double kinVelocityFinal(double velocityInitial, double acceleration, double deltaTime);
double kinVelocityFinalSquared(double velocityInitial, double acceleration, double deltaX);
int mainKinematics();
#endif
当我单独编译Kinematics.cpp文件(上面)时,它运行正常。但是,当我试图将Kinematics.cpp文件和rotKinematics.cpp文件绑定到一个总体主文件中时(在第一个代码示例中显示)我遇到了以下问题:代码编译没有错误,它需要用户输入要访问哪个函数,然后立即关闭而不会从单独的源文件中执行被调用的函数。我在调用函数的方式上做错了吗?
非常感谢任何帮助,感谢您的时间!!
P.S。我意识到我在.cpp和.h文件中声明了这些函数;当我不在.cpp文件中包含声明时,编译器返回错误。这表明我做错了什么,虽然我还没弄清楚是什么。
更新
您好!非常感谢你们对此的重视!通过提供的资源,我找到了一个解决方法:
if (fieldChoice == 1) {
int val = -1;
val = mainKinematics();
return 0;
在原始脚本中引用该函数并没有实际返回任何内容;通过创建一个中间对象(val)来保存最终值,脚本现在似乎按预期工作。你们有什么想法?这是一个好的解决方案吗?