c++ - 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char

标签 c++ arrays pass-by-reference c2664

当我尝试编译和运行时,我不断收到 C2664 错误,“无法将参数 1 从 char[10] 转换为 char”。我试过用指针替换数组(char answer[] to char * answers)。我可以在不将数组传递给函数的情况下做到这一点,但这就是我正在努力做得更好的事情。

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

//Function prototypes
bool grade(char, char, int, int&, int&);
int goodbye();

int main()
{
    const int TEST_LENGTH = 10;
    char const correctAnswers[TEST_LENGTH] =
    { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
    char studentAnswers[TEST_LENGTH] = {};
    int correct = 0, missed = 0;
    bool passing;

    cout << "Welcome to your Driver's License Exam." << endl;
    cout << "This test is ten multiple choice questions." << endl;
    cout << "Only A, B, C, and D are valid answers." << endl;
    cout << "Please begin.";

    for (int i=0; i < TEST_LENGTH; i++)
    {
        int errorCount = 0;
        cout << "Question " << i + 1 << ": ";
        cin >> studentAnswers[i];
        studentAnswers[i] = toupper(studentAnswers[i]);
        while (studentAnswers[i] < 'A' || studentAnswers[i] > 'D')
        {
            if (errorCount++ > 3)
                goodbye();
            cout << "Only A, B, C, and D are valid input. Reenter. " << endl;
            cout << "Question " << i + 1 << ": ";
            cin >> studentAnswers[i];
        }
    }

    passing = grade(studentAnswers, correctAnswers, TEST_LENGTH, correct, missed);

    if (passing) 
    {
        cout << "Congratulations!" << endl;
        cout << "You have passed the exam." << endl;
        cout << "Total number of correct answers: " << correct << endl;
        cout << "Total number of incorrect answer: " << missed << endl;
    }
    else 
    {
        cout << "You have failed the exam" << endl;
        cout << "Sorry, you have not passed the exam." << correct << endl;
        cout << "Total number of incorrect answer: " << missed << endl;

    }
    return 0;
}

bool grade(char answers[], const char key[], const int size, int& hits, int & misses)
{
    for (int i = 0; i < size ; i++ )
    {
        if (answers[i] == key[i])
            hits++;
        else
            misses++;
    }

    if (hits >= 8)
        return true;
    else
        return false;
}

int goodbye() 
{
    cout << "GOOD BYE" << endl;
    return 1;
}

最佳答案

您的函数原型(prototype)与您的声明不匹配:

//Prototype, takes two chars as first params
bool grade(char, char, int, int&, int&); 

//Declaration, takes two char-arrays (pointers to char) as first params
bool grade(char answers[], const char key[], const int size, int& hits, int & misses)

更改您的原型(prototype)以匹配您的声明,错误应该会消失。

关于c++ - 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61495155/

相关文章:

c++ - 带占位符的 FormatMessage

arrays - 查询所有数组项都大于指定条件的地方

c++ - C++ 中奇怪的自动引用

c - 为什么 C 代码中的引用调用不能交换 2 个值?

arrays - 将数组指针传递给另一个类并为将来保留一个引用 - Swift

c++ - 如何使用 C++ 模板创建具有任意数量 case 的 switch 语句生成器?

c++ - 采用 Bamboo 或 TeamCity 作为原生 Windows C++ 构建自动化/CI 服务器?

c++ - std::vector<std::vector<T>> 与 std::vector<T*>

java - Android opengl 在绘制调用后修改顶点数组

c++ - 理解 C++ 中的数值溢出