简体   繁体   English

在 C++ 中遇到对象和类的问题

[英]Having trouble with objects and classes in C++

So, I defined the method displayStudInfo in the 'Student' Class and called it in the main function.因此,我在“Student”类中定义了方法 displayStudInfo 并在主函数中调用它。 But I'm getting the error "Function not declared in this scope".但是我收到错误“未在此范围内声明函数”。 Can anyone please tell me why this is happening and what I can do to solve this problem?谁能告诉我为什么会发生这种情况以及我可以做些什么来解决这个问题?

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


class Student{

  public:
    int age;
    string name;


    void enterInfo(){

    cout << "Enter your age = " ; cin >> age;
    cout << "Enter your name = "; cin >> name;

    }


    void displayStudInfo(Student s)
    {
        cout << "Age = " << s.age << ", name=" << s.name << endl;
    }

};




int main(){

    int size;
    Student stud[100];
    Student abir;

    abir.enterInfo();
    displayStudInfo(abir);

}

void displayStudInfo(Student s) hidden in side class. void displayStudInfo(Student s)隐藏在副课中。 So, its not accessible in main().所以,它不能在 main() 中访问。

Try:尝试:

 void displayStudInfo()
    {
        cout << "Age = " << age << ", name=" << name << endl;
    }

call in main():在 main() 中调用:

abir.displayStudInfo();

In your case void displayStudInfo(Student s) is a member function of Student so you have to call it on an instance of Student , the same way you did with enterInfo .在你的情况下void displayStudInfo(Student s)是的成员函数Student ,所以你必须调用它的一个实例Student ,你也做了同样的方式enterInfo

You can solve that in different ways.你可以用不同的方式解决这个问题。 One way is to make that member function a free function by moving it out of the body of the Student一种方法是通过将该成员函数移出Student的主体来使该成员函数成为自由函数

class Student{

  public:
    // … 
};

void displayStudInfo(Student s)
{
  cout << "Age = " << s.age << ", name=" << s.name << endl;
}

int main(){
    // … 

    displayStudInfo(abir);

}

displayStudInfo is, in fact, a good candidate for a free function.实际上, displayStudInfo是免费函数的一个很好的候选者。 Or you make it static which is similar to a free function, and access the static member function using Student::displayStudInfo(abir) .或者您将其设为static ,类似于自由函数,并使用Student::displayStudInfo(abir)访问静态成员函数。

The other way would be to call displayStudInfo on abir in that case you don't need the Student argument, as abir is implicitly passed to displayStudInfo .另一种方法是在abir上调用displayStudInfo ,在这种情况下您不需要Student参数,因为abir被隐式传递给displayStudInfo

class Student{

  public:
    // … 

    void displayStudInfo()
    {
      cout << "Age = " << age << ", name=" << name << endl;
    }

};

int main(){
    // … 

    abir.displayStudInfo();
}

In C++, all member functions implicitly receive a parameter which points to the current object.在 C++ 中,所有成员函数都隐式地接收一个指向当前对象的参数。 This parameter is the this pointer .这个参数就是this 指针

Therefore, it doesn't make sense for you to specify an additional (explicit) parameter for the object in your definition of the function displayStudInfo .因此,在函数displayStudInfo定义中为对象指定附加(显式)参数是没有意义的。

It would make sense to rewrite the function definition to将函数定义重写为

void displayStudInfo()
{
    cout << "Age = " << age << ", name=" << name << endl;
}

and to call it with并调用它

abir.displayStudInfo();

instead of代替

displayStudInfo(abir);

Alternatively, you could make the function displayStudInfo a non-member function, by putting it outside the declaration of class Student.或者,您可以将函数displayStudInfo置于非成员函数中,方法是将其放在类 Student 的声明之外。 In that case, you would have to keep the explicit parameter, because that parameter is only passed implicitly to member functions.在这种情况下,您必须保留显式参数,因为该参数仅隐式传递给成员函数。

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

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