简体   繁体   中英

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

Error: 'a' was not declared in the scope

Error: 'UIClass' was not declared in the scope

Error: 'AgeObject' was not declared in the scope

Error: Expected ';' before 'NameObject'

Error: 'NameObject' was not declared in the scope

Error: Expected ';' before 'ResultObject'

Error: 'ResultObject' was not declared in the scope

#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.

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.

So what you could do is declare four additional methods, setAge, setName which will take in arguments as follows:

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()
{
    // 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++.

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. 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. 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. 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!

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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