相同的代码在 Dev C++ 上编译,但在 Visual Studio 2017 上不编译


#include<cstdlib>           //Required for compatibility
#include<cmath>             //Required for pow
#include<fstream>           //Required for data files
#include<iostream>          //Required for cout and cin
#include<iomanip>           //Required for setw
using namespace std;
int choice, loops, count;
double initTime, timeIncrement, finalTime, time, A1, B1, C1, D1, E1, 
altitude, A2, B2, C2, D2, E2, velocity;         //This is line 20 as the error notes            
//Declare variables
ifstream inFile;
ofstream outFile;

void menuFunction() {                                                                               
//Main menu function where user selects which option they would like to proceed with, not relevant to question
}
double altitudeFunction(double& altitude) {                                                                         
//Altitude calculation
altitude = A1 * pow(time, 4.0) + B1 * pow(time, 3.0) + C1 * pow(time, 2.0) + D1 * time + E1; //This is line 36 as the error notes
}
double velocityFunction(double& velocity) {                                                                         
//Velocity calculation
velocity = A2 * pow(time, 4.0) + B2 * pow(time, 3.0) + C2 * pow(time, 2.0) + D1 * time + E1; // This is line 41, as the error notes
}
void parameters() {                                                                     
//Function to enter time parameters to save space, not relevant to errors      
}
int main() {
menuFunction();
while (choice != 4) {
switch (choice) {
case 1: {
parameters();
if (finalTime < 5 || finalTime > 24) {                                  
//Required invalid entry error message                                             
//Redisplay menu to allow user another try
}
else {
//Open input file for option 1
//Find variables in input file
//close input file and open output file                                                      
//Make output file look neat
//Column headers for output file for readability
loops = (int)((finalTime - initTime) / timeIncrement);
for (count = 0; count <= loops; count++) {                                      //For loop for incremental calculations, this is line 86 as the error notes
time = initTime + count * timeIncrement;                                                                
//Run both calculation functions
//Print results to output file during for loop
}
//close output file
system("CLS");
//Print message to screen saying data was recorded to output file
}
break;
}
case 2: {
parameters();
if (finalTime < 5 || finalTime > 24) {                                  
//Required invalid entry error message                                             
//Redisplay menu to allow user another try
}
else {
//Open input file for option 1
//Find variables in input file
//close input file and open output file                                                      
//Make output file look neat
//Column headers for output file for readability
loops = (int)((finalTime - initTime) / timeIncrement);
for (count = 0; count <= loops; count++) {                                      //For loop for incremental calculations, this is line 118 as the error notes
time = initTime + count * timeIncrement;                                                                
//Run both calculation functions
//Print results to output file during for loop
}
//close output file
system("CLS");
//Print message to screen saying data was recorded to output file
}
break;
}
case 3: {
parameters();
if (finalTime < 5 || finalTime > 24) {                                  
//Required invalid entry error message                                             
//Redisplay menu to allow user another try
}
else {
//Open input file for option 1
//Find variables in input file
//close input file and open output file                                                      
//Make output file look neat
//Column headers for output file for readability
loops = (int)((finalTime - initTime) / timeIncrement);
for (count = 0; count <= loops; count++) {                                      //For loop for incremental calculations, this is line 150 as the error notes
time = initTime + count * timeIncrement;                                                                
//Run both calculation functions
//Print results to output file during for loop
}
//close output file
system("CLS");
//Print message to screen saying data was recorded to output file
}
break;
}
default: {
cout << "tInvalid Entry!nn";                                     //Error message if user enters an invalid menu option
menuFunction();
}
}
}
if (choice == 4) {
//end program
system("pause");
return 0;
}
}

在我C++编码的第一学期,我不明白为什么,但由于某种原因,当我运行这个确切的程序时,它在 Dev C++ 中编译得很好,但我在 Visual Studio 2017 中遇到了许多错误。我尝试复制并粘贴错误,但它的格式不正确,所以基本上每个"时间"和"计数"实例都会给我一个错误,说"'时间/计数'是模棱两可的"和以下不太常见的错误:

错误 C2659 '=':用作左操作数 86、118、150

错误 C2365 "时间":重新定义:以前的定义是 "功能" 20

错误 C2297"*":非法的右操作数的类型为"time_t (__cdecl *)(time_t *常量)' 36, 41

错误 C2665 'pow':6 个重载中的任何一个都无法转换所有 参数类型 36, 41

该程序应该从输入文件中提取气象气球数据输入,执行一些数学运算以获取特定时间的高度和速度值,并将这些值输出到新的数据文件中。它可以在Dev C++上完美编译、运行和工作,但不能在Visual Studio上编译。我只是问,因为我为一门课程提交了这个并得到了 0,因为它不能在教授的计算机上编译,但在我的计算机上工作正常。有什么想法吗?

编辑以删除我的名字以及代码中不相关的部分。代码中被注释替换的所有内容都可以正常工作,代码中包含错误的部分被保留。

这可能表明using namespace std;是一个坏主意的一个原因。

标准库包含一个std::time函数,由using引入。

现在编译器不知道纯time是指该程序中声明的::time变量还是std::time函数。

它可能在某些系统上编译的原因是允许C++标准标头间接包含任何其他标准标头。因此,与 Dev-C++一起使用的标准库标头可能会偶然包含std::time

最新更新