简体   繁体   English

C ++简单程序初学者

[英]Beginner in c++ simple program

Hi Everyone I am trying to learn the basics of classes and objects. 大家好,我正在尝试学习类和对象的基础知识。 As far as I know my syntax is correct but I get these error messages with my program ... 据我所知,我的语法是正确的,但是我在程序中收到了这些错误消息...

Error: 'A' was not declared in the scope 错误:范围中未声明“ A”

Error: 'a' was not declared in the scope 错误:范围中未声明“ a”

Error: 'UIClass' was not declared in the scope 错误:范围中未声明“ UIClass”

Error: 'AgeObject' was not declared in the scope 错误:范围中未声明“ AgeObject”

Error: Expected ';' 错误:预期为“;” before 'NameObject' 在“ NameObject”之前

Error: 'NameObject' was not declared in the scope 错误:范围内未声明“ NameObject”

Error: Expected ';' 错误:预期为“;” before 'ResultObject' 在“ ResultObject”之前

Error: 'ResultObject' was not declared in the scope 错误:范围内未声明“ ResultObject”

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

class UI{

public:

void Age(){
int a;
cout << "Age?" << endl;
cin >> a;}

void Name(){
string A;
cout << "Name" << endl;
cin >> A;}

void Results(){
cout << "Your name is " << A << "and you are " << a << " years old." << endl;
 }


};


int main ()

{

cout << "Enter Your Name and Age?" << endl;

UIClass; AgeObject;
AgeObject.Age();

UIClass NameObject;
NameObject.Name();

UIClass ResultObject;
ResultObject.Results();

return 0;

}

So in your code, in the Results method, you're trying to access variables that are not declared in there. 因此,在您的代码的Results方法中,您试图访问未在其中声明的变量。

So you have: 所以你有了:

void age()
{
    // does stuff with age
} 

void name()
{
    // does stuff with name
}

The variables only exist in these methods. 变量仅存在于这些方法中。 So when you try to get to them from Results() you'll get an "Out of scope" error. 因此,当您尝试从Results()获取它们时,会出现“超出范围”错误。

So what you could do is declare four additional methods, setAge, setName which will take in arguments as follows: 因此,您可以做的是声明四个其他方法setAge和setName,这些方法将采用以下参数:

class UI
{
    private:
        int age;
        string name;

    public:
        void setAge(int ag)
        {
            age = ag;
        }

        int getAge()
        {
            return age;
        }

Then you'd change your void age() method to something like this: 然后,将您的void age()方法更改为如下所示:

void age()
{
    // Do the stuff you've already done
    setAge(a);
}

Then when you try to get the output done: 然后,当您尝试完成输出时:

cout << "Your name is " << getName() << " and you are " << getAge() << " years old." << endl;

Which ever book you're using, they really should have explained this sort of stuff. 您使用的是哪本书,他们确实应该已经解释了这类内容。 If not, I'd get a new one. 如果没有,我会换一个新的。 This is one of the most basic programs you'll ever write in C++. 这是您将用C ++编写的最基本的程序之一。

I've not given you the complete answer, but this should encourage you and give you a starting point. 我没有给您完整的答案,但这应该会鼓励您并为您提供一个起点。 Hope it all helps. 希望对您有帮助。

Happy coding. 快乐的编码。

Error clearly says the variables are declared out of scope. 错误明确表明变量声明超出范围。 The variable int a and string A are declared inside the function, they are out of scope when you try to use other than that function. 变量int astring A在函数内部声明,当您尝试使用该函数以外的变量时,它们不在范围内。 Declare as the public variables of a class. 声明为类的公共变量。 Also you have instantiated 3 UI objects to call three functions,you should not do that because each object has its own memory. 另外,您已经实例化了3个UI对象来调用三个函数,因此不要这样做,因为每个对象都有自己的内存。 Instantiate one object and call the functions. 实例化一个对象并调用函数。

 class UI
   {

     public:
     int a;
     string A;
     void Age()
     {
       //int a;  remove the local varaible,  'a' can't be used outside function Name
       cout << "Age?" << endl;
       cin >> a;
     }

     void Name()
     {
       //string A;  remove the local varaible, 'A' can't be used outside function Name
       cout << "Name" << endl;
       cin >> A;
     }

      void Results()
      {
        cout << "Your name is " << A << "and you are " << a << " years old." << endl;
      }   

    };

You have declared a and A to be local variables of the Name and Age methods, so they are not available in the Results method. 您已将aA声明a NameAge方法的局部变量,因此它们在Results方法中不可用。 You probably wanted to make them member variables instead. 您可能想让它们成为成员变量。 Move their declarations to the class scope instead of the method scope. 将它们的声明移到类范围而不是方法范围。 Also, a and A are the worst names ever! 另外, aA是有史以来最差的名字!

Then, you declare three separate instances of the class (except you added a semi-colon between the class and instance name), and call each method on a different instance. 然后,您声明该类的三个单独的实例(除非您在类和实例名称之间添加了分号),然后在不同的实例上调用每个方法。 Try creating a single instance, and calling all three methods on it. 尝试创建一个实例,并在其上调用所有三个方法。

Oh, and please please please learn how to indent code... 哦,请请学习如何缩进代码...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM