C++添加数组

C++添加数组,c++,arrays,function,class,C++,Arrays,Function,Class,我在这两个函数中声明了变量,因为在我这样做之前,它没有在每个函数中识别它们,但现在我认为这是因为类没有被识别。对于您遇到的问题,您的程序可以简化为以下内容: int main() { class Question { public: void askQuestion(); }; Question q; q.askQuestion(); } void Question::askQuestion() { } 您将在Question::

我在这两个函数中声明了变量,因为在我这样做之前,它没有在每个函数中识别它们,但现在我认为这是因为类没有被识别。

对于您遇到的问题,您的程序可以简化为以下内容:

int main() 
{
    class Question  {
    public:
        void askQuestion();
    };

    Question q;
    q.askQuestion();
}

void Question::askQuestion() { }
您将在Question::askQuestion的定义中得到一个错误,因为该类是在主函数的范围内声明和定义的,在它之外是无法识别的

将类移到main之外,如下所示:

class Question  {
public:
    void askQuestion();
};

int main() 
{
    // you can use the Question class here, even though you haven't
    // defined all of its methods yet!
    Question q;
    q.askQuestion();
}

void Question::askQuestion() { }
。。。这个项目是godbolt.org

然而,这还不是全部。。。您正在声明您的成员变量,例如每个成员函数中的问题文本。局部定义屏蔽了类成员,因此您只需设置方法的局部变量,该变量在执行完成时会被销毁

其他说明和建议:

如果类已经有方法名,则无需在方法名中再次使用“问题”。i、 问:ask似乎比问:askQuestion更适合选择名字。问题::问题\文本也是如此。 最好只使用std::中您特别想要引用的类、类型或函数。 如果你仔细想想,你对问题类的使用是相当人为的。它的代码主要是提供测验的代码,而不是一个问题本身,一个问题本身。似乎更好的选择对象应该是一组答案和值,或者问题参数。设置值可以通过构造QuestionParameters对象来完成,提问可以是一个接受此类对象的函数。 如果您有答案1、答案2、答案3,那么您可能需要一个容器,例如std::vector answers。然后可以使用答案索引,而不是使用if或switch语句。
在主函数之外定义类时必须进行的更改 如果需要,请使用开关 使用int值而不是E退出程序。 我对代码做了如下更改

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

class Question
{
private:
    string Question_Text;
    string Answer_one;
    string Answer_two;
    string Answer_three;

    int Correct_Answer;
    int Question_Score;

public:
    void setValues(string, string, string, string, int, int);
    void askQuestion();
}; //end of class

//---------------------------
void Question ::askQuestion()
{
    string Question_Text;
    string Answer_one;
    string Answer_two;
    string Answer_three;

    int Correct_Answer;
    int Question_Score;
    int ans;
    int Total;

    cout << endl;
    cout << Question_Text << endl;
    cout << "1. " << Answer_one << endl;
    cout << "2. " << Answer_two << endl;
    cout << "3. " << Answer_three << endl
         << endl;
    cout << "Please enter your answer: " << endl;
    cin >> ans;

    if (ans == Correct_Answer)
    {
        cout << "That is correct!" << endl;
        Total = Total + Question_Score;
    }
    else
    {
        cout << "Sorry, that is incorrect" << endl;
        cout << "The correct answer was " << Correct_Answer << endl;
    }
} //end of askQuestion
//----------------------------------
void Question ::setValues(string q, string a1, string a2, string a3, int ca, int pa)
{
    string Question_Text;
    string Answer_one;
    string Answer_two;
    string Answer_three;

    int Correct_Answer;
    int Question_Score;

    Question_Text = q;
    Answer_one = a1;
    Answer_two = a2;
    Answer_three = a3;
    Correct_Answer = ca;
    Question_Score = pa;
} //end of setValues
//----------------------------------

int main()
{
    string username = "";
    int choice;
    char c;
    char answer;
    int x = 4;
    int y = 5;
    int z = x + y;
    int ans;
    int total;

    //welcome message
    cout << "Hello user, please enter your name:";
    cin >> username;
    cout << "Welcome to the programming tutorial " << username << "." << endl;

    //menu selection

    while (choice != 5)
    {
        cout << "What would you like to do? (Unit 1 - Declaring Variables (1), Unit 2 - Input/ Output (2), Unit 3 - Conditionals (3), Quizzes (4) or Exit (5))";
        cin >> choice;

        switch (choice)
        {
        case 1:
        {
            cout << "We will begin with defining variables. The first step to doing this is choosing which datatype your variable is.\n";
            cout << "The following are a few of the common datatypes used in programming.\n";
            cout << "Character ==> char\n";
            cout << "Integer ==> int, long, double\n";
            cout << "Boolean ==> bool\n";
            cout << endl;
            cout << "When declaring a variable, you must put its datatype before the variable name.\n";
            cout << "An example of this would be if we wanted to declare the value of x as 4.\n";
            cout << "We would write this as: \n";
            cout << "int x = 4\n";
            cout << "The program will now use the value 4 for the variable name 'x'\n";
            cout << endl;
            cout << "Now let's assume we assigned the value of 5 to the variable 'y'\n";
            cout << "If we wanted to add x and y and assign the sum to the variable 'z', we would write:\n";
            cout << "int z = x + y\n";
            cout << "Now when we use the variable 'z' in our program, it will perform the calculation given x=4 and y=5 and declare 9 as the value of the variable 'z'.\n";
            cout << "To test our code, we would write: " << endl;
            cout << "cout<<'x + y'<< z << endl; \n";
            cout << "If written correctly, it will display as: \n";
            cout << "x + y = " << z << "." << endl;
            break;
        }

        case 2:
        {
            cout << "Now that we understand the basics of declaring variables, let's discuss displaying, or output of, information to a user.\n";
            cout << "If you wanted to display a welcome message, for example, you would type:\n";
            cout << "cout << 'Welcome';\n";
            cout << "The line of code would start with 'cout' followed by two less than signs and then the message you wish to display in quotes.\n";
            cout << "Using this, you can ask the user for input.\n";
            cout << "Enter c to continue...";
            cin >> c;
            cout << "Let's say we have a program that flips a coin. You may want to ask the user how many times to flip the coin.\n";
            cout << "Assuming we previously declared this amount variable as 'int timesFlipped', we would 'cout' our question and the next line would read:\n";
            cout << "cin>> timesFlipped; \n";
            cout << "This will store the users input for the variable 'timesFlipped'\n";
            cout << "You almost always end a line of code with a semi colon." << endl;
            break;
        }
        case 3:
            cout << "This unit will cover conditional expressions." << endl;
            break;
        case 4:
        {
            string Question_Text;
            string Answer_one;
            string Answer_two;
            string Answer_three;

            int Correct_Answer;
            int Question_Score;
            Question q1;
            Question q2;
            Question q3;

            cout << username << ", you have chosen to take a quiz." << endl
                 << endl;
            int ans, score = 0;
            cout << "Unit One Quiz - Variables " << endl
                 << endl;

            q1.setValues("How would you declare the value of 'x' as 12? ",
                         "x=12()",
                         "x==12()",
                         "x=12;()",
                         3,
                         1);
            q2.setValues("What do you need to put before a variable when declaring it?",
                         "a name()",
                         "a value()",
                         "a datatype()",
                         3,
                         1);
            q3.setValues("Which data type would you use for a number that includes a decimal value?",
                         "int()",
                         "double()",
                         "float()",
                         2,
                         1);

            q1.askQuestion();
            q2.askQuestion();
            q3.askQuestion();
            cout << "Your score out of a possible 3 is " << total << endl;
            break;
        }

        case 5:
            cout << "Have a good day!";
            break;
        } //end of switch
    } //end of while

    system("pause");

    return 0;
} //end of main

把你的课带到主楼外面。并且不要重新声明成员变量。在不同的作用域中具有相同名称的变量仍然是完全不同的独立变量。他们不会有相同的价值观或任何东西。@cigien:我相信这是一个答案。注意:这比讽刺更重要。唐娜,@cigien几乎解决了你正在经历的问题。但是,在将来,请尝试将示例缩短到屏幕可读性更强的长度-这通常有助于缩小问题的范围。此外,当询问编译失败时,显式指定要得到的错误以及在哪一行上是很重要的。最后,你的问题在语法上应该是一个问题。欢迎来到StackOverflow:-