标题说明了一切。错误是



错误2错误LNK2019:未解析的外部符号“专用:无效__thiscall Salsa::getHigh(void)”(?getHigh @ Salsa @@ AAEXXZ)在函数“公共(public):无效__thiscall Salsa::getSold(void)”中引用(?getSold @ Salsa @@ QAEXXZ)

错误3错误LNK2019:未解决的外部符号“专用:无效__thiscall Salsa::getLow(void)”(?getLow @ Salsa @@ AAEXXZ)在函数“公共(public):无效__thiscall Salsa::getSold(void)”中引用(?getSold @ Salsa @@ QAEXXZ)

错误4错误LNK2019:函数___tmainCRTStartup Chips and Salsa Class中引用了无法解析的外部符号_main

代码如下: .h文件,然后是.cpp;

#ifndef SALSA_H
#define SALSA_H

class Salsa
{
private:
    void getTotal();
    void getHigh();
    void getLow();
    int count;
    int total;
    int highest;
    int lowest;
    int flavor;
public:
    void getSold();
};
#endif

现在是.cpp;
#include "Salsa.h"
#include <iostream>
#include <string>
using namespace std;


void Salsa::getSold()
{
    for (count = 0; count < 5; count++)
    {
        cout << "Jar sold last month of ";
        cout << count + 1;
        cin >> flavor;

        while (flavor <= 0)
        {
            cout << "Jars sold must be greater than or equal to 0.";
            cout << "Re-enter jars sold for last month ";
            cin >> flavor;
            cout << endl;


        }
        Salsa::getTotal();
        Salsa::getHigh();
        Salsa::getLow();
    }

    Salsa::getTotal();

    total = 0;

    for (count = 0; count < 5; count++)
    {
        total += flavor;

        cout << "Total Sales: " << total << endl;
    }

    Salsa::getHigh();
    {
        highest = flavor;
        int index = 1;

        for (count = 0; count < 5; count++)
            if (flavor > highest)
            {
            highest = flavor;
            index = count + 1;
            }

        cout << "High Seller: " << flavor << endl;
    }

    Salsa::getLow();
    {
        lowest = flavor;
        int index = 1;

        for (count = 0; count < 5; count++)
        {
            if (flavor < lowest)
            {
                lowest = flavor;
                index = count + 1;
            }

            cout << "Low Seller: " << flavor << endl;
        }

        int main();
        {
            const int SALS_FLAV = 5;
            string flavor[SALS_FLAV] = { "mild", "medium", "sweet", "hot", "zesty" };

            Salsa sold;

            for (int index = 0; index < SALS_FLAV; index++)
            {
                sold.getSold();
            }
        }
    }
}

最佳答案

您的代码有很多问题:

  • 您未声明函数的返回类型
  • 您不应以;结束函数定义;
  • 您没有适当地关闭函数,现在它们变成了嵌套函数

  • 请研究您的代码与以下代码之间的区别:
    #include "Salsa.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void Salsa::getSold()
    {
        for (count = 0; count < 5; count++)
        {
            cout << "Jar sold last month of ";
            cout << count + 1;
            cin >> flavor;
    
            while (flavor <= 0)
            {
                cout << "Jars sold must be greater than or equal to 0.";
                cout << "Re-enter jars sold for last month ";
                cin >> flavor;
                cout << endl;
    
            }
            getTotal();
            getHigh();
            getLow();
        }
    }
    
    void Salsa::getTotal()
    {
        total = 0;
    
        for (count = 0; count < 5; count++)
        {
            total += flavor;
    
            cout << "Total Sales: " << total << endl;
        }
    }
    
    void Salsa::getHigh()
    {
        highest = flavor;
        int index = 1;
    
        for (count = 0; count < 5; count++)
            if (flavor > highest)
            {
            highest = flavor;
            index = count + 1;
            }
    
        cout << "High Seller: " << flavor << endl;
    }
    
    void Salsa::getLow()
    {
        lowest = flavor;
        int index = 1;
    
        for (count = 0; count < 5; count++)
        {
            if (flavor < lowest)
            {
                lowest = flavor;
                index = count + 1;
            }
    
            cout << "Low Seller: " << flavor << endl;
        }
    }
    
    int main()
    {
        const int SALS_FLAV = 5;
        string flavor[SALS_FLAV] = { "mild", "medium", "sweet", "hot", "zesty" };
    
        Salsa sold;
    
        for (int index = 0; index < SALS_FLAV; index++)
        {
            sold.getSold();
        }
    }
    

    10-07 22:17