0

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

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

2 回答 2

3

确保您的定义与您的声明相匹配。

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&andconst Rainfall&参数。您的定义需要一个Rainfall[],Months[]int参数。因此,这些不匹配,并且对于编译器来说,它们是不同的函数。

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

于 2013-10-29T02:21:13.793 回答
2

运行时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

这意味着您需要匹配 2 个函数的声明和定义(您的参数不一样):

1. displayData
2. getInput
于 2013-10-29T02:45:18.573 回答