驾驶执照考试计划。无法将std :: string转换为std :: string和其他错误

时间:2018-03-11 19:13:54

标签: c++ visual-studio

我正在做一个驱动程序许可程序,用户必须输入一系列答案,然后将其与实际答案进行比较,然后确定用户是通过还是失败。以下是我的程序,我收到了很多错误。我在Visual Studios中使用C ++

Error   C2440   'initializing': cannot convert from 'initializer list' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' line 69
Error   C2664   'void TestGrader::setKey(std::string [])': cannot convert argument 1 from 'std::string' to 'std::string []' line 73
Error (active)  E0289   no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list       line 69
Error (active)  E0413   no suitable conversion function from "std::string" to "std::string *" exists   line 73

以下是我目前的代码。 如果有人可以向我解释为什么我会收到这些错误以及如何解决这些问题,我们将非常感激。

#include <iostream> 
#include <iomanip>
#include <string>
#include <cctype> 

using namespace std;

const int STRING_COUNTER = 20;

class TestGrader 
{
  private:
    string answers[20];

public:
    void setKey(string right[])
    {
        for (int i = 0; i < STRING_COUNTER; i++)
        {
            answers[i] = right[i];
        }

    };
    void grade(string exam[])
    {
        int correct = 0;
        int wrong = 0;

        for (int i = 0; i < STRING_COUNTER; i++)
        {
            if (exam[i] == answers[i])
            {
                correct += 1;
            }
            else
            {
                wrong += 1;
            }
        }

        if (correct >= 15)
        {
            cout << "You passed the test!" << endl;
        }
        else
        {
            cout << "You failed the test." << endl;
        }
        cout << endl;
        cout << "You got " << correct << " questions right and got " << wrong << " questions wrong." << endl << endl;

        for (int x = 0; x < STRING_COUNTER; x++)
        {
            if (exam[x] != answers[x])
            {
                cout << "You got question number " << x << " wrong.";
                cout << endl;
            }
        }
    };

};


int main()
{
    string answer = {     "B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A" };

TestGrader test;

test.setKey(answer);

string stuTest[STRING_COUNTER];

int choice = 1;

do
{
    for (int i = 0; i < STRING_COUNTER; i++)
    {
        cout << "Please enter your answer for question number " << i + 1 << ": ";
        cin >> stuTest[i];
        cout << endl;

        while (stuTest[i] > "D" || stuTest[i] < "A")
        {
            cout << "Error, only letters A,B,C and D are accepted: ";
            cin >> stuTest[i];
            cout << endl;
        }
    }

    test.grade(stuTest);

    cout << "Would you like to Retake the test? 1. Yes 2. No";
    cin >> choice;

} while (choice == 1);


return 0;
}

1 个答案:

答案 0 :(得分:1)

Error C2440 'initializing': cannot convert from 'initializer list' to 'std::basic_string,std::allocator>' line 69 - 意味着您有一个列表(或数组),您尝试初始化std::string,这是不允许的。字符串是字符串,而不是字符串数组。如果变量是一个数组就可以了:

string answer[] = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A" };

Error C2664 'void TestGrader::setKey(std::string [])': cannot convert argument 1 from 'std::string' to 'std::string []' line 73 - set key需要一个字符串数组 - 你给它一个字符串 - 上面的修复就可以解决这个问题。