带有冒泡排序的 C++ 结构

标签 c++ visual-studio-2012 structure bubble-sort

我正在使用 Microsoft Visual Studios Express 2012,我正在尝试编写一个程序来获取每月的降雨量并显示它。我必须使用结构,我必须使用冒泡排序从大到小显示它。我似乎迷失在我的代码中,我对我哪里出错了感到困惑。它目前告诉我我有两个 Unresolved external 问题。

//This program shows the months of the year with their associated rainfall amount.
#include <iostream> 
#include <string>
using namespace std;

const int SIZE = 12;

struct Rainfall
{
double rain[SIZE];

double getValue()
{
    double value = rain[SIZE];
    return value;
}

};

struct Months
{
const string MONTHS[SIZE];

Months()
{
    string MONTHS[SIZE] = {"January", "Feburary", "March",
                         "April", "May", "June", "July", 
                         "August","September","October",
                         "November","December"};
}

string getNames()
{
    string names = MONTHS[SIZE];
    return names;
}
};

//Function Prototypes
double getInput(const Months &, Rainfall &);
void sortData(Rainfall[],int);
void displayData(const Months, const Rainfall &);

int main()
{
Months timePeriod;
Rainfall amount;
Rainfall rain[SIZE];

getInput(timePeriod,amount);
sortData(rain,SIZE);
displayData(timePeriod,amount);

cin.ignore();
cin.get();
return 0;
}

/*****getInput*****/
double getInput(Months &timePeriod, Rainfall &amount)
{
cout << "\nPlease enter the amount of rainfall per month for the following  months:\n";

for(int counter = 0; counter <= 11; counter++)
{
    cout << timePeriod.MONTHS[counter] << ": ";
    cin >> amount.rain[counter];
    cout << endl;
    return amount.rain[counter];
}

}

/*****sortData*****/
void sortData(Rainfall array[], int SIZE)
{
Rainfall temp;
bool swap;

do
{
    swap = false;
    for(int count = 0; count < (SIZE-1); count++)
    {
        if(array[count].getValue() > array[count +1].getValue())
        {
            temp = array[count];
            array[count] = array[count +1];
            array[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

/*****displayData*****/
void displayData(Rainfall number[], Months names[], int SIZE)
{
for(int index = 0; index < SIZE; index++)
{
    cout << names[index].getNames() << endl;
    cout << number[index].getValue() << endl;
}
}

最佳答案

确保您的定义与您的声明相符。

double getInput(const Months &, Rainfall &);
void sortData(Rainfall[],int);
void displayData(const Months, const Rainfall &);

无需查看所有这些,我就可以看到这里与 displayData

的差异
void displayData(const Months, const Rainfall &); // Declaration.
void displayData(Rainfall number[], Months names[], int SIZE) // Definition.

在这种情况下,未解析的外部符号 表示您声明 一个函数,但在链接阶段没有找到定义它。

您已声明 displayData 函数采用 const Months&const Rainfall& 参数。您的定义采用 Rainfall[]Months[]int 参数。因此,它们不匹配,并且对于编译器而言它们是不同的函数。

由于函数重载,我们可以拥有具有相同名称但采用不同参数的函数。

关于带有冒泡排序的 C++ 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648512/

相关文章:

c++ - atomic_load/atomic_store on std::shared_ptr in VC11 - 为什么全局自旋锁?

c++ - C++ 错误代码样板的模板与宏

c++ - 如何将 QKeyEvent::nativeModifiers() 转换为 UINT fsModifiers(用于 winapi RegisterHotKey)

visual-c++ - Visual Studio 确定字节顺序

c - 如何在头文件中声明一个结构,该结构将被 c 中的多个文件使用?

c++ - 对于 std::vector,如何在没有 .resize() 的情况下使用有效的 .begin() 和 .end()?

f# - 让绑定(bind)支持用双反引号括起来的标点符号,但类型不支持?

c++ - 如何将 double 转换为两位小数?

c++ - 如何从包含 c 字符串、int 和 float 的键盘中批量读取结构变量?

c++ - 需要帮助使用递归将文本文件中的信息输入到结构中吗?