Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 铿锵++;生成失败,但gcc生成成功_C++_Clang - Fatal编程技术网

C++ 铿锵++;生成失败,但gcc生成成功

C++ 铿锵++;生成失败,但gcc生成成功,c++,clang,C++,Clang,我在学C++。下面是一个简单的演示。我可以在MacOS上使用gcc-10.2成功编译,但是clang-12.0失败了 class Person{ public: string name; int sex; int age; Person(string name, int sex, int age){ this -> name = name; this -> sex = sex; this -> ag

我在学C++。下面是一个简单的演示。我可以在MacOS上使用gcc-10.2成功编译,但是clang-12.0失败了

class Person{
public:
    string name;
    int sex;
    int age;
    Person(string name, int sex, int age){
        this -> name = name;
        this -> sex = sex;
        this -> age = age;
    }
public:
    bool operator==(const Person &person)
        if ((name == person.name) && (sex==person.sex) && (age=person.age)){
            return true;
        }else{
            return false;
        }
    }
};
int main()
{
    vector<Person> v;
    v.push_back(Person("tom",1,20));
    v.push_back(Person("tom",1,20));
    v.push_back(Person("jessei",0,21));
    vector<Person>::iterator it = adjacent_find(v.begin(),v.end());
    cout << it->name<<":" << it->age<<":" << it-> sex << endl;
    return 0;
}

源代码中有几个错误,但我认为最好让您自己发现并更正它们

回到您的问题,运算符==之后缺少一个常量

bool operator==(const Person &person) const {
   return xxx;
}
更好的方法可能还会添加constexpr和noexcept(对于现代c++)


当你想创建一个新的对象时,你应该在C++中使用<代码>新的< /Cord>关键字。例如,写
v.push_back(新人(“汤姆”,1,20))@Marc AndréBrochu完全错了。这是一个Person的向量,而不是Person*。是的,
new
返回一个无效的指针。顺便说一句,如果你写v.emplace_back(“tom”,1,20)而不是push_back会更好。注意
age=Person.age
是赋值,而不是
=
。是的,有一些错误。谢谢,你的解决方案是正确的,它起作用了。
bool operator==(const Person &person) const {
   return xxx;
}
constexpr bool operator==(const Person &person) const noexcept {
   return xxx;
}