简体   繁体   English

带气泡排序的C ++结构

[英]C++ Structure with Bubble Sort

I'm using Microsoft Visual Studios Express 2012, and I am trying to write a program that gets the rainfall per month and displays it. 我正在使用Microsoft Visual Studios Express 2012,并且试图编写一个程序来获取每月的降雨量并显示出来。 I have to use structures, and I have to use bubble sort to display it from largest to smallest. 我必须使用结构,并且必须使用气泡排序将其从大到小显示。 I seem to have become lost in my code, and I'm confused with where I went wrong. 我的代码似乎迷路了,我对哪里出错了感到困惑。 It is currently telling me I have two unresolved externals. 当前告诉我我有两个未解决的外部问题。

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

Make sure your definitions match your declarations. 确保您的定义与声明匹配。

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

Without looking at all of them I can see a discrepancy here with displayData 如果不看所有这些内容,我可以在这里看到与displayData的差异

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

An unresolved external symbol in this case means that you have declared a function, but during the linking stage there was no definition found for it. 在这种情况下, 未解析的外部符号表示您已经声明了一个函数,但是在链接阶段没有找到该函数的定义

You have declared the displayData function to take a const Months& and const Rainfall& argument. 您已声明displayData函数采用const Months&const Rainfall&参数。 Your definition takes a Rainfall[] , Months[] and int argument. 您的定义采用Rainfall[]Months[]int参数。 These therefore, are not matching, and to the compiler they are different functions. 因此,它们不匹配,并且对于编译器而言,它们是不同的功能。

Thanks to function overloading, we can have functions with the same name but take different arguments. 由于函数重载,我们可以拥有具有相同名称但采用不同参数的函数。

Running on VS2010 , you have the below Linker errors: VS2010运行,您遇到以下链接器错误:

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl displayData(struct Months,struct Rainfall const &)" (?displayData@@YAXUMonths@@ABURainfall@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "double __cdecl getInput(struct Months const &,struct Rainfall &)" (?getInput@@YANABUMonths@@AAURainfall@@@Z) referenced in function _main

Meaning that you need to match the declaration and definition for the 2 functions (your parameters are not the same): 这意味着您需要匹配两个函数的声明和定义(您的参数不相同):

1. displayData
2. getInput

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM