[英]c++ same code compiles/runs never in Visual Studio and sometimes in Qt Creator
[英]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!\n\n"; //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中出現了很多錯誤。我試圖復制並粘貼錯誤,但格式不正確,因此基本上每個'time'和'count'實例都給我一個錯誤,指出“'time / count'不明確”以及以下較不常見的錯誤:
錯誤C2659'=':用作左操作數86、118、150
錯誤C2365“時間”:重新定義:先前的定義是“功能” 20
錯誤C2297'*':非法,右操作數的類型為'time_t(__cdecl *)(time_t * const)'36,41
錯誤C2665'pow':6個重載均不能轉換所有參數類型36、41
該程序應該從輸入文件中提取氣象氣球數據輸入,進行一些數學運算以獲取特定時間的高度和速度值,並將這些值輸出到新的數據文件中。 它可以在Dev C ++上編譯,運行和完美運行,但不能在Visual Studio上編譯。 我之所以這么問,是因為我提交了這門課程,並給了0,因為它不能在教授的計算機上編譯,但在我的計算機上可以正常工作。 有任何想法嗎?
編輯后刪除了我的名字以及代碼中不相關的部分。 在代碼中用注釋替換的所有內容都可以正常工作,保留了包含錯誤的代碼部分。
這可能表明using namespace std;
原因之一using namespace std;
是個壞主意。
標准庫包含一個std::time
函數,該函數由using
。
現在,編譯器不知道普通time
意味着在此程序或std::time
函數中聲明的::time
變量。
它可能在某些系統上編譯的原因是允許C ++標准標頭間接包含任何其他標准標頭。 因此,可能偶然地與Dev-C ++一起使用的標准庫頭文件不包含std::time
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.