简体   繁体   English

我的代码中出现以下错误。 有人能帮我修一下吗

[英]i got the following error in my code. can some help me fix it

im trying to use parallel arrays and function to get user input for this menu/list im working on.我正在尝试使用并行 arrays 和 function 来获取我正在处理的此菜单/列表的用户输入。 the program should accept the name, surname and hours worked in the same function. the error im facing is down below.该程序应接受在同一个 function 中的姓名和工作时间。我面临的错误在下面。 please help me fix it请帮我修一下

"main.cpp:38:28: error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string'} to 'std::string*' {aka 'std::__cxx11::basic_string*'} " “main.cpp:38:28: 错误:无法将‘std::string’{又名‘std::__cxx11::basic_string’}转换为‘std::string*’{又名‘std::__cxx11::basic_string* '}”

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

//globl const for array size
const int SIZE = 10;

// functions 
void menuDisplay();
void getEmpDetails(string[], string[], int[]);

int main()
{
    // declaring parrell arrays   
    string name[SIZE]; //for storing Employee names
    string surname[SIZE]; // for storing Employee surname
    int hoursWorked[SIZE]; // for storing employee hours worked

   //calling the functions
    menuDisplay();
    getEmpDetails(name[SIZE], surname[SIZE], hoursWorked[SIZE]);

    return 0;
}

void menuDisplay() {
    char choice;

    do { //this makes the menu repeat itself

        cout << "[C]apture Employee details" << endl;
        cout << "[L]ist Employee details" << endl;
        cout << "[A]ll Employee Payslips" << endl;
        cout << "[S]ingle Employee Payslips" << endl;
        cout << "[E]xit" << endl;
        cin >> choice;

        switch (choice) {
        case 0:
            cout << "capture employee detail" << endl;
            break;
        case 1:
            cout << "list employee details" << endl;
            break;
        case 2:
            cout << "All Employee Payslips" << endl;
            break;
        case 3:
            cout << "Single employee payslips" << endl;
            break;
        case 4:
            cout << "Exit" << endl;

        }

    } while (choice == 'C' || choice == 'L' || choice == 'S' || choice == 'E' || choice == 'A'); //for selecting the right options on the menu
    cout << "invaild choice, please choice either C,L,A,S,E" << endl; // if the wrong option is selected this appears
}


void getEmpDetails(string name[SIZE], string surname[SIZE], int hoursWorked[SIZE]) {
    //this function is for capturing employee details
    for (int x = 0; x < SIZE; x++) {
        cout << "enter employee name" << endl;
        cin >> name[SIZE];
        cout << "enter employee surname" << endl;
        cin >> surname[SIZE];
        cout << "enter number of hours worked" << endl;
        cin >> hoursWorked[SIZE];
    }
}
void getEmpDetails(string[],string[], int[]);

This function is declared as taking three pointers as parameters.这个 function 被声明为以三个指针为参数。 In both C and C++ function parameters that get declares as arrays are actually pointers, this is really在 C 和 C++ function 中声明为 arrays 的参数实际上是指针,这真的是

void getEmpDetails(string *,string *, int *);

You attempt to call this function as follows:您尝试按如下方式调用此 function:

 getEmpDetails(name[SIZE],surname[SIZE],hoursWorked[SIZE]);

This name is in array.这个name在数组中。 If X is in array, in C and C++ X[n] means the nth value in the array.如果X在数组中,则在 C 和 C++ 中X[n]表示数组中的nth值。 So the above code attempts to pass a single value in the array to this function. The SIZE th value.所以上面的代码试图将数组中的单个传递给这个 function。第SIZE个值。 Which, to add insult to injury, doesn't even exist so that's undefined behavior right out of the gate.雪上加霜的是,它甚至不存在,所以这是一开始就未定义的行为。

Now, if you go and reread the error message it makes perfect sense.现在,如果您输入 go 并重新阅读错误消息,它就很有意义了。 The compiler is complaining that you're trying to pass a string , that single value in the array, to a function that expects a string * parameter.编译器抱怨您试图将数组中的单个值string传递给需要string *参数的 function。 That's obviously wrong.这显然是错误的。

Your obvious intent here is to pass the arrays, or pointers to the arrays, to this function, and not a single value from each array.您在这里的明显意图是将 arrays 或指向 arrays 的指针传递给这个 function,而不是每个数组中的单个值。 If so, this is, simply:如果是这样,这很简单:

 getEmpDetails(name, surname, hoursWorked);

This should answer the question of the compilation error.这应该回答编译错误的问题。 But that won't solve all of your problem.但这并不能解决你所有的问题。 Looking at what's in this function:查看这个 function 中的内容:

    for (int x =0; x < SIZE; x++){
        cout << "enter employee name" <<endl;
        cin >> name[SIZE];

If you followed my explanation above, the problem here should now be obvious, as well as what it's obvious solution.如果你按照我上面的解释,这里的问题现在应该很明显,以及它的明显解决方案。 Hint: name[SIZE] does not exist.提示: name[SIZE]不存在。 Attempting to set it to anything will result in a crash.尝试将其设置为任何值都会导致崩溃。 Ask yourself what exactly you want to happen here.问问自己,你到底想在这里发生什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您好,我的代码中有语法问题。 你能帮助我吗? - Greetings, I have a syntax problem in my code. Can you help me? 我的代码有错误。 有指针的东西我似乎无法修复它 - I have an error in my code. Something with pointers I can't seem to fix it 有人可以帮我修改此代码。 我正在尝试显示棋盘格C ++ QT - can someone help me modify this code. I'm trying to Display a checkerboard C++ QT 键盘记录程序代码错误。 你能帮我吗? - Error in keylogger code. Would you help me? 我无法使用以下代码反转给定的链表。 有人可以指出代码中的错误吗? - I am unable to reverse the given linked list with the following code. Can someone point out the error in the code? 我有一些错误任何人都可以帮助我 - I have some error can anyone help me in this 有人可以帮助我解决此错误吗? - Can some one help me with this error? 我在本地机器上得到了正确的解决方案,但 Topcoder 不接受这个,谁能帮助我 - I got the correct solution in my local machine but Topcoder didn't accept this, can anyone help me 谁能帮我定制一个链表? 我无法找到或修复段错误 - can anyone help me with a custom linked list? I cannot find or fix a segfault error 我在优化我的代码时遇到了一些麻烦。 某些测试用例由于“超过时间限制”而失败。 如何优化我的代码? - I am having some trouble to optimize my code. Certain test cases are failing due to “exceeded time limit”. How can I optimize my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM