变量未在作用域中声明?

问题描述 投票:0回答:4

我的程序收到这些错误消息...

错误:“A”未在范围内声明

错误:'a'未在范围内声明

错误:“UIClass”未在范围内声明

错误:“AgeObject”未在范围内声明

错误:应为“;”在“NameObject”之前

错误:“NameObject”未在范围内声明

错误:应为“;”在“ResultObject”之前

错误:“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;

}
c++ object
4个回答
2
投票

因此,在您的代码中,在 Results 方法中,您尝试访问未在其中声明的变量。

所以你有:

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

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

变量仅存在于这些方法中。因此,当您尝试从 Results() 获取它们时,您将收到“超出范围”错误。

所以你可以做的是声明四个额外的方法,setAge,setName,它们将接受如下参数:

class UI
{
    private:
        int age;
        string name;

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

        int getAge()
        {
            return age;
        }

然后你可以将 void Age() 方法更改为如下所示:

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

然后当您尝试完成输出时:

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

无论你使用哪本书,它们确实应该解释这类内容。如果没有,我就买一个新的。这是您用 C++ 编写的最基本的程序之一。

我没有给你完整的答案,但这应该会鼓励你并给你一个起点。希望一切有帮助。

快乐编码。


0
投票

您已将

a
A
声明为
Name
Age
方法的局部变量,因此它们在
Results
方法中不可用。您可能想让它们成为成员变量。将它们的声明移至类作用域而不是方法作用域。另外,
a
A
是有史以来最糟糕的名字!

然后,声明该类的三个单独实例(除非您在类和实例名称之间添加了分号),并在不同的实例上调用每个方法。尝试创建一个实例,并调用它的所有三个方法。

哦,请学习如何缩进代码...


0
投票

错误清楚地表明变量被声明超出范围。变量

int a
string A
在函数内部声明,当您尝试使用该函数以外的其他函数时,它们超出了范围。声明为类的公共变量。另外,您还实例化了 3 个 UI 对象来调用三个函数,您不应该这样做,因为每个对象都有自己的内存。实例化一个对象并调用函数。

 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;
      }   

    };

0
投票

类的优点是它们的访问级别(公共、私有、受保护),因此一定要研究这些,因为您正在使用类来创建要使用的用户定义数据类型(这是 C++ 如此受欢迎的主要动力) )。如果您不打算将成员对象设为私有并将它们分组以形成唯一的对象,您也可以使用命名空间。查看 learncpp.com 第 15 课和第 16 课。继续编码!

#include <iostream>
#include <string>

using namespace std;

class UI {
    int m_age{ 0 };
    string m_name{"???"};

public:
    void userInput() {
        cout << "Age?" << endl;
        cin >> m_age;
        cout << "Name" << endl;
        cin >> m_name;
    }

    void results() {
        cout << "Your name is " << m_name << " and you are " << m_age << " years old." << endl;
    }
};

int main()
{
    cout << "Enter Your Name and Age?" << endl;

    UI person;
    person.userInput();
    person.results();

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.