相同的代码在 Dev C++ 上编译,但在 Visual Studio 2017 上不编译
Same code compiles on Dev C++ but not on 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
。
- 编译并运行后,输出未出现在 Visual Studio 代码中
- 第三方 API 中的编译错误 - Visual Studio
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 如何在 CMake 中添加预编译标头用于 Visual Studio 生成器
- 如何使用msbuild和设置C++17命令行编译Visual Studio项目?
- 如何使用 GLFW 预编译的二进制文件编译 Visual Studio 2019 发布版本
- 我想使用 CMake 生成和编译 Visual Studio projet,我需要从构建中排除单元测试文件。有没有办法做到这一点?
- 如何在 Linux 上高效地编译 Visual Studio c++ 项目
- 如何通过Windows上的命令行编译Visual Studio的C 项目
- 在不安装 Visual Studio 的情况下编译 Visual Studio 项目
- 在LNK2038中编译Visual Studio 2010到2012 RC的MFC应用程序
- 在使用 Xbuild 编译 Visual C++ 项目时定义目标
- 在Qt Creator中编译Visual Studio 2010项目
- 如何从 C# 以编程方式编译 Visual C++ 项目
- 编译Visual c++ DLL时出现奇怪错误
- 从命令行编译Visual c++
- 如何从命令提示窗口构建和编译visual studio c++代码
- 在amd64模式下编译(visual studio 2010)
- 使用CMake编译Visual Studio 2012的SFML
- IncrediBuild在一个线程中编译Visual Studio 2010/2012解决方案的一些项目