[英]C++ Structure with Bubble Sort
我正在使用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;
}
}
確保您的定義與聲明匹配。
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
參數。 因此,它們不匹配,並且對於編譯器而言,它們是不同的功能。
由於函數重載,我們可以擁有具有相同名稱但采用不同參數的函數。
在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
這意味着您需要匹配兩個函數的聲明和定義(您的參數不相同):
1. displayData
2. getInput
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.