gpt4 book ai didi

c++ - 将记录数组的元素传递给临时记录数组的问题

转载 作者:行者123 更新时间:2023-11-30 02:10:54 24 4
gpt4 key购买 nike

我是一名正在接受培训的新代码猴子,目前在使用数组和结构时遇到问题。目前我有一个主文件,其中声明了一个记录数组。我将该数组传递给外部函数,在其中对记录中的字段执行快速排序。主要是名字。我在将记录数组中的元素复制到排序算法的临时数组时遇到问题。我知道 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

制作文件

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

标题.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

主要.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;

}
}

外部数组.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));
}

最佳答案

C 中,您可以使用简单赋值复制整个 struct

tmpElm[first] = emp[secound];

只有当结构包含指针成员时才会出现问题,这不是你的情况。

关于c++ - 将记录数组的元素传递给临时记录数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4189941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com