C++ 如何解决这些cpp错误?
使用VisualStudio2019 我收到的错误有: E0065-第104行 E0112-第133行 C2601-第134行 C2601-第146行 我尝试过重新排列代码段,删除或修改其他代码段,并且用尽了我能想到的选项。我真的不知道哪里出了问题,任何帮助都是值得感谢的。如果需要,我还可以提供我遵循的方向的屏幕截图C++ 如何解决这些cpp错误?,c++,visual-c++,compiler-errors,C++,Visual C++,Compiler Errors,使用VisualStudio2019 我收到的错误有: E0065-第104行 E0112-第133行 C2601-第134行 C2601-第146行 我尝试过重新排列代码段,删除或修改其他代码段,并且用尽了我能想到的选项。我真的不知道哪里出了问题,任何帮助都是值得感谢的。如果需要,我还可以提供我遵循的方向的屏幕截图 // bring in libraries #include <iostream> #include <conio.h> #include <str
// bring in libraries
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream> // read/write to files
#include <ctime> // time(0)
#include <iomanip> // setprecision( )
using namespace std;
// prototypes
void deposit(double* ptrBalance);
void withdrawal(double* ptrBalance, float dailyLimit); // overloaded method this version does not take withdrawal amount
void withdrawal(double* ptrBalance, float dailyLimit, float amount); // overloaded method that takes withdrawal amount
/// Entry point to the application
int main()
{
// Create constant variables
const int EXIT_VALUE = 5;
const float DAILY_LIMIT = 400.0f;
const string FILENAME = "Account.txt";
// create loop variable BEFORE the loop
short choice = 0;
// Create balance variable
double balance = 0.0;
// Look for the starting balance; otherwise generate a random starting balance
ifstream iFile(FILENAME.c_str());
if(iFile.is_open())
{
// Did the file open? If so, read the balance.
iFile >> balance;
iFile.close();
}
else
{
// If the file did not open or does not exist, create a random number for the starting balance
srand(time(0));
const int MIN = 1000;
const int MAX = 10000;
balance = rand() % (MAX - MIN + 1) + MIN;
}
std::cout << fixed << setprecision(2) << "Starting Balance: $" << balance << endl;
// Let's create a pointer and set it to the balance variable location
double* ptrBalance = &balance; // & means "address of"
// start the application loop
do
{
// show the menu
system("cls"); // clears the console screen -- for MAC, use system("clear");
std::cout << "Menu\n" << endl;
std::cout << "1) Deposit " << endl;
std::cout << "2) Withdrawal" << endl;
std::cout << "3) Check Balance" << endl;
std::cout << "4) Quick $40" << endl;
std::cout << "5) Exit" << endl;
// get user input
std::cout << "\nEnter your choice: ";
cin >> choice;
// run code based on user input
switch (choice)
{
case 1:
deposit(ptrBalance); // Passing a pointer so only 4 bytes have to cross the system bus
break;
case 2:
withdrawal(ptrBalance, DAILY_LIMIT);
break;
case 3:
std::cout << "Showing current balance..." << endl;
break;
case 4:
std::cout << "Getting quick $40..." << endl;
break;
case 5:
std::cout << "\nGoodbye" << endl;
break;
default:
std::cout << "\nError. Please select from the menu." << endl;
break;
}
/// Make a deposit
void deposit(double* ptrBalance)
{
// get deposit and validate it
float deposit = 0.0f;
do
{
std::cout << "\nEnter deposit amount";
cin >> deposit;
if (cin.fail()) // did they give us a character instead of a number?
{
cin.clear(); // clears fail state
cin.ignore(INT16_MAX, '\n'); // clears keyboard buffer
std::cout << "\nError. Please input numbers only.\n" << endl;
deposit = -1; // set deposit to a "bad" number
continue; // restart the loop
}
else if (deposit < 0.0f) // check for negative number
std::cout << "\nError. Invalid deposit amount.\n" << endl;
} while (deposit < 0.0f);
// How do we get the double value located at the pointer?
// Dereference it using an asterisk
*ptrBalance += deposit; // same as: *ptrBalance = ptrBalance + deposit;
std::cout << fixed << setprecision(2) << "\nCurrent ptrBalance: $" << *ptrBalance << endl; // notice asterisk
}
/// Make a withdrawal
void withdrawal(double* ptrBalance, float dailyLimit)
{
// get the withdrawal (you should validate this input
float amount = 0.0f;
std::cout << "\nEnter withdrawal amount: ";
cin >> amount;
// call the overloaded method version that takes the balance, dailyLimit, and withdrawal amount
withdrawal(ptrBalance, dailyLimit, amount);
}
/// Make a withdrawal - this overload accepts balance, dailyLimit, and withdrawal amount
void withdrawal(double* ptrBalance, float dailyLimit, float amount)
{
// take money away from account and show the balance
if (amount > dailyLimit)
{
cout << "\nError. Amount exceeds daily limit." << endl;
}
else if (amount > * ptrBalance) // notice the asterisk to dereference the pointer!
{
cout << "\nError. Insufficient funds." << endl;
}
else
{
*ptrBalance -= amount; // same as: *ptrBalance = *ptrBalance - amount;
cout << "\nHere is your cash: $" << amount << endl;
}
}
cout << fixed << setprecision(2) << "\nCurrent ptrBalance: $" << *ptrBalance << endl;
// pause
std::cout << "\nPress any key to continue...";
_getch();
while (choice != EXIT_VALUE);
return 0;
// now that the application has ended, write the new balance to the file
ofstream oFile(FILENAME.c_str());
oFile << balance << endl;
oFile.close();
return 0;
// pause before we clear the screen
std::cout << "n\Press any key to continue...";
_getch();
}
//引入库
#包括
#包括
#包括
#包括//读取/写入文件
#包括//时间(0)
#include//setprecision()
使用名称空间std;
//原型
无效存款(双倍*PTR余额);
无效提款(双倍*PTRBance,每日浮动限额);//重载方法此版本不接受取款金额
无效提款(双倍*PTR平衡、每日浮动限额、浮动金额);//获取取款金额的重载方法
///应用程序的入口点
int main()
{
//创建常量变量
const int EXIT_值=5;
恒浮日极限=400.0f;
常量字符串FILENAME=“Account.txt”;
//在循环之前创建循环变量
短选择=0;
//创建平衡变量
双平衡=0.0;
//查找起始余额;否则生成随机起始余额
ifstream iFile(FILENAME.c_str());
如果(iFile.is_open())
{
//文件打开了吗?如果打开了,请阅读余额。
i文件>>平衡;
iFile.close();
}
其他的
{
//如果文件未打开或不存在,请为起始余额创建一个随机数
srand(时间(0));
const int MIN=1000;
常数int MAX=10000;
平衡=兰德()%(最大-最小+1)+最小;
}
std::cout在您的代码版本中,您已经在main()
中定义了函数。在C++
中,这是不允许的,即在另一个函数中定义一个函数。您不能在另一个函数的范围内实现一个函数
由于main()
也是一个函数,因此会出现链接器错误
尝试在main()
外部定义函数。在外部定义后,可以从main()调用它们
C++不允许在另一个函数中定义函数。这样做的唯一方法是使用。因此,在定义函数时,不要在另一个函数中定义它,而是在其他地方定义它。还有一些其他错误(和警告),我已经更正并注释掉了
//引入库
#包括
#包括
#包括
#包括//读取/写入文件
#包括//时间(0)
#include//setprecision()
使用名称空间std;
//原型矿床;
if(cin.fail())//他们给我们的是字符而不是数字吗?
{
cin.clear();//清除失败状态
cin.ignore(INT16_MAX,'\n');//清除键盘缓冲区
STD::CUT不能像C++一样,在C++中嵌套函数定义,所以移动那些外部的代码>主< /代码>。创建一个移除噪声和红色鲱鱼,这有助于你更具体地关注触发错误的代码(除了MRE是要求调试帮助的问题的要求)之外。.编译错误通常在十几行以下重复出现;在“第104行”中看到错误表示在将实际代码简化为本问题中使用的示例方面几乎没有付出任何努力。很少有人记住错误代码。添加错误消息既会增加可以帮助您解答问题的人数,也会增加您可以帮助的人数(有类似问题的人在搜索错误消息后会发现此问题)。非常感谢您的帮助,我非常感谢。完全错过了这些错误