C++ 错误:对‘;的调用不匹配;(std::string{aka std::basic_string})(’;

C++ 错误:对‘;的调用不匹配;(std::string{aka std::basic_string})(’;,c++,C++,我的代码: #include <stdio.h> #include <iostream> #include <string> using namespace std; class Person { public: string name; int age; }; void print (Person* person) { cout << person->name << &quo

我的代码:

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

class Person
{
    public:
        string name;
        int age;
};

void print (Person* person)
{
    cout << person->name << "is " << person -> age << " years old" << endl;
}

int main()
{
    Person person;
    person.name = "Harry";
    person.age = 23;
    
    cout << "Meet " << person.name();
    print (&person);
    
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
班主任
{
公众:
字符串名;
智力年龄;
};
作废打印(人*人)
{

cout name您需要删除person后面的括号。因为您试图访问名为“name”的变量,但是当您放入括号时,认为它是一个函数。因此您需要这样做

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

class Person
{
    public:
        string name;
        int age;
};

void print (Person* person)
{
    cout << person->name << "is " << person -> age << " years old" << endl;
}

int main()
{
    Person person;
    person.name = "Harry";
    person.age = 23;
    
    cout << "Meet " << person.name;
    print (&person);
    
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
班主任
{
公众:
字符串名;
智力年龄;
};
作废打印(人*人)
{

无法命名通过在变量名后添加括号你想做什么?删除
person.name
后的空括号。我的教授这样写代码。不知道为什么。似乎没有括号就可以工作。谢谢!
请注意,如果老师写
cout
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

class Person
{
    public:
        string name;
        int age;
};

void print (Person* person)
{
    cout << person->name << "is " << person -> age << " years old" << endl;
}

int main()
{
    Person person;
    person.name = "Harry";
    person.age = 23;
    
    cout << "Meet " << person.name;
    print (&person);
    
    return 0;
}