c++ - 错误 LNK2019 : Unresolved External Symbol chips and salsa program

标签 c++

标题说明了一切。错误是

Error 1 error LNK2019: unresolved external symbol "private: void __thiscall Salsa::getTotal(void)" (?getTotal@Salsa@@AAEXXZ) referenced in function "public: void __thiscall Salsa::getSold(void)" (?getSold@Salsa@@QAEXXZ)

错误 2 error LNK2019: 未解析的外部符号“private: void __thiscall Salsa::getHigh(void)” (?getHigh@Salsa@@AAEXXZ) 在函数“public: void __thiscall Salsa::getSold(void)” ( ?getSold@Salsa@@QAEXXZ)

错误 3 error LNK2019: 未解析的外部符号“private: void __thiscall Salsa::getLow(void)” (?getLow@Salsa@@AAEXXZ) 在函数“public: void __thiscall Salsa::getSold(void)” ( ?getSold@Salsa@@QAEXXZ)

错误 4 error LNK2019: 未解析的外部符号 _main 在函数 ___tmainCRTStartup Chips 和 Salsa 类中引用

代码如下; .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();
            }
        }
    }
}

最佳答案

你的代码有很多问题:

  1. 你没有声明函数的返回类型
  2. 您不应该以 ; 结束函数定义
  3. 您没有注意正确关闭函数,现在它们变成了嵌套函数

请研究您的代码与以下代码之间的差异:

#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();
    }
}

关于c++ - 错误 LNK2019 : Unresolved External Symbol chips and salsa program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28665221/

相关文章:

c++ - 如何在 VC++ 中将 TChar[] 数组转换为 std::string。语法帮助

c++ - 这个真实路径定义的跨平台替代方案?

c++ - 如何有效地将内核 malloc 数据返回给 cpu

c++ - 从 cv::connectedComponents 访问标记区域

c++ - Qt中的图像转换器,彩色图像转黑白

c++ - 声明具有相同args的两个构造函数的最理想的方法是什么?

c++ - 继承(C++)

c++ - 用于 C 密码学的快速伪随机数生成器

c++ - Dll 未在 Firefox 中加载,但在自定义应用程序中加载

c++ - 我应该为 C++ 中的序列中的无重复数字选择哪个随机生成器