【问题标题】:How to resolve these cpp errors? [closed]如何解决这些 cpp 错误? [关闭]
【发布时间】:2021-02-01 02:53:25
【问题描述】:

使用 2019 年视觉工作室

我收到的错误是:

E0065 - 104 行

E0112 - 133 行

C2601 - 134 行

C2601 - 146 行

我尝试过重新排列代码段,删除或更改其他代码段,并且已经用尽了我能想到的选项。不太确定出了什么问题,感谢您提供任何帮助。如果需要,我也可以提供我遵循的方向的屏幕截图。

// 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();

}

【问题讨论】:

  • 您不能在 C++ 中嵌套函数定义,就像 C2601 所说的那样。所以把那些移到外面main
  • 创建minimal reproducible example 可以消除噪音和红鲱鱼,这有助于您更专注于触发错误的代码(除了 MRE 是要求调试帮助的问题的要求)。编译错误通常可以在十几行内重现;在“第 104 行”中看到错误表明几乎没有付出任何努力来将您的真实代码简化为用于此问题的示例。
  • 很少有人能记住错误代码。添加错误消息会增加可以帮助您提供答案的人数,并增加您可以帮助的人数(有类似问题的人会在搜索错误消息后找到此问题)。

标签: c++ visual-c++ compiler-errors


【解决方案1】:

C++ 不允许您在另一个函数中定义一个函数。这样做的唯一方法是使用lambdas。所以在定义一个函数时,不要在另一个函数中定义它,而是在其他地方定义它。还有一些其他错误(和警告),我已经更正并注释掉了。

// 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   <-- You dont need these.
//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

/// 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 - this overload accepts balance, dailyLimit, and withdrawal amount
void withdrawal(double* ptrBalance, float dailyLimit, float amount) // Place this before the other overload as its used by the other one.
{
    // 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;
    }
}

/// 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);
}

/// 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(static_cast<unsigned int>(time(0)));  // Cast it to unsigned int.
        const int MIN = 1000;
        const int MAX = 10000;
        balance = static_cast<double>(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;
        }


        cout << fixed << setprecision(2) << "\nCurrent ptrBalance: $" << *ptrBalance << endl;


        // pause
        std::cout << "\nPress any key to continue...";
        _getch();

    } while (choice != EXIT_VALUE);

    //return 0; <-- You cannot have multiple returns.

    // now that the application has ended, write the new balance to the file
    ofstream oFile(FILENAME.c_str());
    oFile << balance << endl;
    oFile.close();

    // pause before we clear the screen
    std::cout << "\nPress any key to continue...";  // It should be \n, not n\
    _getch();

    return 0;   // Place the return at the end.
}

【讨论】:

  • 非常感谢您的帮助,非常感谢。完全错过了那些错误
【解决方案2】:

在您的代码版本中,您已经在main() 中定义了函数。在C++ 中,这是不允许的,即在另一个函数中定义一个函数。你不能在另一个函数的范围内实现一个函数。

由于main() 也是一个函数,因此您会收到链接器错误。

尝试定义main()之外的函数。在外部定义后,您可以通过main() 调用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多