2013-04-06 115 views
0

我不知道爲什麼這不會運行。我應該在工資陣列中存儲hours * payrate,然後showResults將此信息和cout它。當我運行它,我得到的錯誤是低於C++通過函數傳遞數組

error LNK2019: unresolved external symbol "void __cdecl showResults(int * const,int,double * const,double * const,double * const)" ([email protected]@[email protected]) referenced in function 
error LNK2019: unresolved external symbol "void __cdecl getEmployeeData(int * const,int,double * const,double * const,double * const)" ([email protected]@[email protected]) referenced in function `_main` 

代碼:

#include <iomanip> 
#include <iostream> 
using namespace std; 

void getEmployeeData(int[], int, double[], double[], double[]); 
void showResults(int[], int, double[], double[], double[]); 


int main() 
{ 
const int ARRAY_SIZE = 7; 
int empId[ARRAY_SIZE] = {565, 845, 452, 130, 789, 758, 877}; 

double hoursWorked[ARRAY_SIZE]; // Holds hours worked 
double payrate[ARRAY_SIZE]; // Holds pay rate 
double wages[ARRAY_SIZE]; // Holds wages 

getEmployeeData(empId, ARRAY_SIZE, payrate, hoursWorked, wages); 

showResults(empId, ARRAY_SIZE, payrate, hoursWorked, wages); 

system("pause"); 
    return 0; 
} 

void getEmployeedata(int nums[],int size,double pay[],double hours[], 
double wages[]) 
{ 

//Hours worked and Pay rate 
for(int index = 0; index < size; index++) 
{ 
    cout << "Enter number of hours worked by employee number " 
     << nums[index] << ": "; 
    cin >> hours[index]; 
    cout << "\nEnter hourly pay rate "; 
    cin >> pay[index]; 
    wages[index] = hours[index] * pay[index]; 
} 
} 

void showResults(int nums[], int size, double pay, double hours, double wages[]) 
{ 
for(int index = 0; index < size; index++) 
{ 
    cout << "Employee Number Gross Wage " << endl 
     << nums[index] << " " << wages[index]; 

} 

} 
+0

C++標準庫提供了一組豐富的[容器類型](http://en.cppreference.com/w/cpp/container)。這些可以很容易地進入和退出功能。你應該使用這些而不是舊式的數組。 – moooeeeep 2013-04-06 18:26:06

回答

1

如果你想將數組傳遞給函數,還嘗試編寫函數爲

void getEmployeeData(int*, int, double*, double*, double*); 
void showResults(int*, int, double*, double*, double*); 

因爲當你傳遞數組時,你傳遞的是指向該數組的第0個索引的指針。 此外,如告知,有一個在您的代碼一個錯字,用getEmployeeData

以下是更正後的代碼替換getEmployeedata

#include <iomanip> 
#include <iostream> 
using namespace std; 

void getEmployeeData(int*, int, double*, double*, double*); 
void showResults(int*, int, double*, double*, double*); 


int main() 
{ 
const int ARRAY_SIZE = 7; 
int empId[7] = {565, 845, 452, 130, 789, 758, 877}; 

double hoursWorked[ARRAY_SIZE]; // Holds hours worked 
double payrate[ARRAY_SIZE]; // Holds pay rate 
double wages[ARRAY_SIZE]; // Holds wages 

getEmployeeData(empId, ARRAY_SIZE, payrate, hoursWorked, wages); 

showResults(empId, ARRAY_SIZE, payrate, hoursWorked, wages); 

system("pause"); 
    return 0; 
} 

void getEmployeeData(int nums[],int size,double pay[],double hours[],double wages[]) 
{ 

//Hours worked and Pay rate 
for(int index = 0; index < size; index++) 
{ 
    cout << "Enter number of hours worked by employee number " 
     << nums[index] << ": "; 
    cin >> hours[index]; 
    cout << "\nEnter hourly pay rate "; 
    cin >> pay[index]; 
    wages[index] = hours[index] * pay[index]; 
} 
} 

void showResults(int *nums, int size, double *pay, double *hours, double *wages) 
{ 
for(int index = 0; index < size; index++) 
{ 
    cout << "Employee Number Gross Wage " << endl 
     << nums[index] << " " << wages[index]; 

} 

} 
+0

指針和我擁有它的方式有什麼區別?在我的書中,我看到它的唯一方式就像[]。 – user1807815 2013-04-06 18:35:34

+0

除語法外沒有區別,請參閱[this](http://stackoverflow.com/questions/5573310/difference-between-passing-array-and-array-pointer-into-function-in-c) – tigerden 2013-04-07 18:16:19

3

聲明和showResults定義不同意有關的參數類型。特別是,payhours的參數在聲明中是double[],但在定義中只有double

void showResults(int[], int, double[], double[], double[]) // Declaration 
//         ↕↕  ↕↕ 
void showResults(int[], int, double , double , double[]) // Definition 

看來聲明是正確的,定義不是,因爲你正在向它們傳遞數組。

您對getEmployeeData的聲明和定義對此名稱持不同意見。

void getEmployeeData(int[], int, double[], double[], double[]) // Declaration 
/    ↕ 
void getEmployeedata(int[], int, double[], double[], double[]) // Definition 

你可能想getEmployeeData能與您的命名的其餘部分保持一致。

+0

謝謝!我一定忽略了那個 – user1807815 2013-04-06 18:24:49

+0

更好的是,'double *'。有數組類型參數是一個方便的謊言,他們變成指針。 (OTOH參考陣列是一個不同的類型) – 2013-04-06 18:24:55

+0

@ Ben Voigt所以,而不是double [],它應該是雙*?這是爲什麼? – user1807815 2013-04-06 18:33:18

1

你函數名都錯字:

getEmployeeData(empId, ARRAY_SIZE, payrate, hoursWorked, wages); 

然而,在定義揮霍:

getEmployeedata(...); 

也是不匹配的位置:

void showResults(int nums[], int size, double pay, double hours, double wages[]) 
              //^^^^^^^^^^^^^^^^^ 
0

既然你知道每個數組到底有多少元素了,你可以嘗試製作ARRAY_SIZE一個全局變量(因此它在相同的範圍內爲你的函數原型),讓你做這樣的事情:

void getEmployeeData(int (&)[ARRAY_SIZE], int, double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE]); 
void showResults(int (&)[ARRAY_SIZE], int, double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE]); 

void getEmployeeData(int (&nums)[ARRAY_SIZE], int size, double (&pay)[ARRAY_SIZE], double (&hours)[ARRAY_SIZE], double (&wages)[ARRAY_SIZE]) { 
// Function body 
} 
void showResults(int (&nums)[ARRAY_SIZE], int size, double (&pay)[ARRAY_SIZE], double (&hours)[ARRAY_SIZE], double (&wages)[ARRAY_SIZE]) { 
// Function body 
} 

現在,這個看起來很笨拙,但它確實我什麼s告訴編譯器該函數需要引用正好爲ARRAY_SIZE的元素。 (具體而言,[]的優先級高於&,因此需要使用括號告訴編譯器該參數是對數組的引用,而不是引用數組。)它還允許您使用ol'C sizeof(nums)/sizeof(nums[0])來確定數組的大小;不像讓陣列衰變成一個指針(在這種情況下,sizeof會給你無用的信息,因爲它看到numsint*而不是int[ARRAY_SIZE]),通過引用傳遞它會保留此信息。


或者,如果你可以自由地重寫代碼和編譯器支持C++ 11,你可以用std::array而不是C風格的數組。

#include <array> 
using namespace std; 

void getEmployeeData(array<int, ARRAY_SIZE>, int, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>); 
void showResults(array<int, ARRAY_SIZE>, int, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>); 


array<int, ARRAY_SIZE> empId = {565, 845, 452, 130, 789, 758, 877}; 

array<double, ARRAY_SIZE> hoursWorked; // Holds hours worked 
array<double, ARRAY_SIZE> payrate; // Holds pay rate 
array<double, ARRAY_SIZE> wages; // Holds wages 

作爲一個容器,std::array可以用相同的語法C風格陣列(即operator[])一起使用,所以不應該需要重寫任何實際使用陣列中的代碼。它還可以讓getEmployeeData()showResults()直接檢查陣列的大小,類似nums.size()

另請注意,如果您使用其中任何一項,則實際上並不需要通過int size,因爲您可以使用sizeofstd::array.size()。儘管如此,這對全局命名空間有點污染,但我個人認爲這是一個公平的權衡,以確保只有在函數被賦予適當大小的數組時,才能編譯代碼。

[請注意,如果您需要能夠在不同的時間通過不同大小的陣列,這些方法都將是你有幫助。]

我,因爲我可能已經錯過了任何typoes道歉。