未初始化变量错误,在Linux中编译,不在Visual Studio中编译 这是我为C++课写的一个程序。我能够在学校的Linux服务器上编译它,但当我尝试在Microsoft Visual Studio上编译它时,我得到一个错误,即main函数括号内的变量是如何“未初始化”的
有没有办法解决这个问题未初始化变量错误,在Linux中编译,不在Visual Studio中编译 这是我为C++课写的一个程序。我能够在学校的Linux服务器上编译它,但当我尝试在Microsoft Visual Studio上编译它时,我得到一个错误,即main函数括号内的变量是如何“未初始化”的,c++,C++,有没有办法解决这个问题 #include <iostream> #include <iomanip> using namespace std; double getIncome(double income); double getBudgetedLiving(double budgetedLivingExpenses); double getActualLiving(double actualLivingExpenses); double getActualTax(do
#include <iostream>
#include <iomanip>
using namespace std;
double getIncome(double income);
double getBudgetedLiving(double budgetedLivingExpenses);
double getActualLiving(double actualLivingExpenses);
double getActualTax(double actualTaxesWithheld);
double getActualTithing(double actualTitheOfferings);
double getActualOther(double actualOtherExpenses);
double display(double income, double budgetedLivingExpenses,
double actualLivingExpenses, double actualTaxesWithheld,
double actualTitheOfferings, double actualOtherExpenses);
/************************************************************************
* MAIN organizes all of the functions in the correct order.
* starting with the 6 category-specific functions, and then
* lastly the display function which recalls the values and
* projects them onto a table.
**********************************************************************/
int main()
{
cout << "This program keeps track of your monthly budget\n"
<< "Please enter the following:" << endl;
double income = getIncome(income);
double budgetedLivingExpenses = getBudgetedLiving(budgetedLivingExpenses);
double actualLivingExpenses = getActualLiving(actualLivingExpenses);
double actualTaxesWithheld = getActualTax(actualTaxesWithheld);
double actualTitheOfferings = getActualTithing(actualTitheOfferings);
double actualOtherExpenses = getActualOther(actualOtherExpenses);
display(income, budgetedLivingExpenses, actualLivingExpenses,
actualTaxesWithheld, actualTitheOfferings, actualOtherExpenses);
return 0;
}
double display(double income, double budgetedLivingExpenses,
double actualLivingExpenses, double actualTaxesWithheld,
double actualTitheOfferings, double actualOtherExpenses)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double budgetedTaxesWithheld = 0.0;
double budgetedTitheOfferings = (income * .10);
double budgetedOtherExpenses = (income - budgetedTaxesWithheld -
budgetedTitheOfferings -
budgetedLivingExpenses);
double actualDifference = (income - actualTaxesWithheld -
actualTitheOfferings -
actualLivingExpenses -
actualOtherExpenses);
double budgetedDifference = 0.0;
cout << endl;
cout << "The following is a report on your monthly expenses\n"
<< "\tItem" << setw(24) << "Budget" << setw(17) << "Actual\n";
cout << "\t=============== =============== ===============\n";
cout << "\tIncome" << setw(11) << "$" << setw(11)
<< income << " $" << setw(11) << income << endl;
cout << "\tTaxes " << "$" << setw(11)
<< budgetedTaxesWithheld << " $" << setw(11)
<< actualTaxesWithheld << endl;
cout << "\tTithing " << "$" << setw(11)
<< budgetedTitheOfferings << " $" << setw(11)
<< actualTitheOfferings << endl;
cout << "\tLiving " << "$" << setw(11)
<< budgetedLivingExpenses << " $" << setw(11)
<< actualLivingExpenses << endl;
cout << "\tOther " << "$" << setw(11)
<< budgetedOtherExpenses << " $" << setw(11)
<< actualOtherExpenses << endl;
cout << "\t=============== =============== ===============\n";
cout << "\tDifference " << "$" << setw(11)
<< budgetedDifference
<< " $" << setw(11) << actualDifference << endl;
return 0;
}
/****************************************************************
* The 6 category functions take inputs such as income,
* budgeted living expenses, etc. from the user and sends them
* to display function.
***************************************************************/
double getIncome(double income)
{
cout << "\tYour monthly income: ";
cin >> income;
return income;
}
double getBudgetedLiving(double budgetedLivingExpenses)
{
cout << "\tYour budgeted living expenses: ";
cin >> budgetedLivingExpenses;
return budgetedLivingExpenses;
}
double getActualLiving(double actualLivingExpenses)
{
cout << "\tYour actual living expenses: ";
cin >> actualLivingExpenses;
return actualLivingExpenses;
}
double getActualTax(double actualTaxesWithheld)
{
cout << "\tYour actual taxes withheld: ";
cin >> actualTaxesWithheld;
return actualTaxesWithheld;
}
double getActualTithing(double actualTitheOfferings)
{
cout << "\tYour actual tithe offerings: ";
cin >> actualTitheOfferings;
return actualTitheOfferings;
}
double getActualOther(double actualOtherExpenses)
{
cout << "\tYour actual other expenses: ";
cin >> actualOtherExpenses;
return actualOtherExpenses;
}
如果有一个声明同时声明和引用一个变量,如double budgetedLivingExpenses=getBudgetedLivingbudgetedLivingExpenses;,传递给函数的变量未初始化。MSVC可以为此显示警告,因为它通常表示存在问题。使用未初始化变量时,编译器不需要发出诊断。当你这样做的时候,你确实会有未定义的行为 在您的情况下,由于在读取get函数中的参数之前为其指定了一个新值,因此会覆盖传入的未初始化值。所以你根本不需要这个参数。只需声明一个局部变量来保存函数中的值,并像返回一样返回它