2010-11-15 66 views
1

我是訓練中的新代碼猴,我目前遇到了使用數組和結構的問題。 目前我有一個主要文件,我有一個聲明數組。我將該數組傳遞給外部函數,在該函數中對記錄中的字段執行快速排序。主要是名字。我遇到了一些問題,我將記錄數組中的元素複製到排序算法的臨時數組中。我知道C++內置了一個qsort函數,但對於我現在正在處理的內容,我需要將算法寫出來。我能夠讓這個工作只使用任何數組。將數組記錄傳遞給臨時數組記錄的問題

嘗試使用make文件進行編譯時出現以下錯誤。

make 
g++ -c -o main2.o main2.cpp 
g++ -c externArray2.cpp -o externArray2.o 
externArray2.cpp: In function ‘void copytemp(EmployeeRecord*, EmployeeRecord*, int, int)’: 
externArray2.cpp:52: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:53: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:54: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:55: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:56: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:57: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:58: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:59: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
externArray2.cpp:60: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’ 
make: *** [externArray2.o] Error 1 

make文件

test1: main.o ExternArray.o 
     g++ main.o ExternArray.o -o test1 

externArray.o: ExternArray.cpp 
     g++ -c ExternArray.cpp -o ExternArray.o 

main.o: main.cpp 
     g++ -c main.cpp -o main.o 

Header.h

#ifndef _INCL_GUARD 
#define _INCL_GUARD 
const int maxEmployee =10; 
const int NAMES = 5; 
const int LENGTH = 15; 


typedef struct EmployeeRecord 
{ 
char first[10]; 
char last[10]; 
float reghours; 
float ovrhours; 
float pay; 
float gross; 
float defer; 
float state; 
float fed; 
float ssi; 
float net; 
} EmployeeRecord; 
#endif 

的main.cpp

#include <iostream> 
#include <cstring> 
using namespace std; 
#include "./Header2.h" 

void showArray(EmployeeRecord employees[], int, const char*); //Function Prototype 3.1 

extern void qsortArray(EmployeeRecord employees[], int, int);  //Funvtion Prototype  3.2 

int main(void) 
{ 
    EmployeeRecord myEmployee[maxEmployee]; 
    strcpy(myEmployee[0].first,"John"); 
    strcpy(myEmployee[0].last,"Doe"); 
    strcpy(myEmployee[1].first,"Ed"); 
    strcpy(myEmployee[1].last, "Whittle"); 
    strcpy(myEmployee[2].first, "Louise"); 
    strcpy(myEmployee[2].last, "Marion"); 
    strcpy(myEmployee[3].first,"Paula"); 
    strcpy(myEmployee[3].last, "Prentiss"); 
    strcpy(myEmployee[4].first, "Carl"); 
    strcpy(myEmployee[4].last, "Davidson"); 


    showArray(myEmployee, NAMES, "Before Sort"); 
    qsortArray(myEmployee, 0, 4); 
    showArray(myEmployee, NAMES, "After Sort"); 

return 0;    
} 



void showArray(EmployeeRecord employees[], int emp, const char *message) 
{ 
cout << message << endl; 
for (int test = 0; test < emp; test++) 
    { 
    cout << "First Name: " << employees[test].first << endl; 
    cout << "Last Name: " << employees[test].last << endl; 

    } 
} 

ExternArray.cpp

#include <cstring> 
#include <iostream> 
#include <iomanip> 
#include <stdio.h> 
using namespace std; 
#include "./Header2.h" 

void qsortArray(EmployeeRecord employees[], int, int);  //Funvtion Prototype   3.2 

void copytemp(EmployeeRecord tmpEmp[], EmployeeRecord emp[], int, int); 

void qsortArray(EmployeeRecord employees[], int start, int finish) 
{ 

int left=start, 
    right=finish; 
char pivot[15]; 
strcpy(pivot, employees[(start+finish)/2].first); 
    while (left < right) { 
    cout << pivot << " pivot " << endl; 
    cout << "outer loop" << endl; 
    // find left candidate 
    while (strcmp(employees[left].first,pivot) <0) left++; 
    // find right candidate 
    cout << "First Inner Loop" << endl; 
    while (strcmp(employees[right].first,pivot) > 0)right--; 
    cout << "Inner Loop" << endl; 
    if (left <= right) 
     { 
     EmployeeRecord tmpEmployee[1]; 
     cout << "Create new struct" << endl; 
     copytemp(tmpEmployee, employees, 0, left); 
     cout << "copy to temp" << endl; 
     copytemp(tmpEmployee, employees, 1, right); 
     copytemp(employees, tmpEmployee, left, 1); 
     copytemp(employees, tmpEmployee, right, 0); 

     left++; 
     right--; 
     cout << "All copy done" <<endl; 
     } 
    } // while left < right 
    cout << "Back out of outer Loop" << endl; 
    if (start < right) qsortArray(employees,start,right); 
    if (left < finish) qsortArray(employees,left,finish); 
} 

void copytemp(EmployeeRecord tmpEmp[], EmployeeRecord emp[], int first, int secound) 
{ 
memcpy(tmpEmp[first].first, emp[secound].first, sizeof(emp[secound].first)); 
memcpy(tmpEmp[first].last, emp[secound].last, sizeof(emp[secound].last)); 
memcpy(tmpEmp[first].reghours, emp[secound].reghours, sizeof(emp[secound].reghours)); 
memcpy(tmpEmp[first].ovrhours, emp[secound].ovrhours, sizeof(emp[secound].ovrhours)); 
memcpy(tmpEmp[first].pay, emp[secound].pay, sizeof(emp[secound].pay)); 
memcpy(tmpEmp[first].gross, emp[secound].gross, sizeof(emp[secound].gross)); 
memcpy(tmpEmp[first].defer, emp[secound].defer, sizeof(emp[secound].defer)); 
memcpy(tmpEmp[first].state, emp[secound].state, sizeof(emp[secound].state)); 
memcpy(tmpEmp[first].fed, emp[secound].fed, sizeof(emp[secound].fed)); 
memcpy(tmpEmp[first].ssi, emp[secound].ssi, sizeof(emp[secound].ssi)); 
memcpy(tmpEmp[first].net, emp[secound].net, sizeof(emp[secound].net)); 
} 
+0

+1表示問題。歡迎來到SO :-) – pmg 2010-11-16 00:15:10

回答

2

C你可以用一個簡單的任務

tmpElm[first] = emp[secound]; 

複製整個struct這僅僅是有問題的,如果結構包含的成員是指針,這是不是你的情況。

+1

+1,你也可以用C++來做到這一點。 – 2010-11-15 23:57:14

+0

我已經做出了您所建議的更改,現在該程序已經解決了問題。現在問題似乎是我運行該程序時。我現在正在收到中止陷阱錯誤消息。 「退出外部循環中止陷阱」 – JoshP 2010-11-16 00:31:36

+0

我第一次看到該消息...但我不做C++。也許你最好問一個新問題 – pmg 2010-11-16 00:33:23

2

嘗試:

memcpy(&(tmpElm[first].first, &(emp[second].first), sizeof(emp[second].first)); 

不同的是,你需要通過一個浮動,而不是浮點值的一個地址。

您可能會發現,使用C++,你需要轉換的浮動指針運算符(&)地址給你一個空白,因爲C++有更強的類型規則,而不是C.

memcpy((void *)&(tmpElm[first].first, (void *)&(emp[second].first), sizeof(emp[second].first)); 

有一個在涉及數組時,很少有其他方法可以獲得某些東西的地址,但這對於新程序員來說是最簡單的。