0

C++/CX で値構造を実装しました。構造体のベクトルを初期化するには、Collection.h にあるベクトルの IndexOf() 演算子に必要な等価演算子を実装する必要があります。

しかし、私はそれを作成することができません.誰かがそれを手伝ってくれますか?

4

1 に答える 1

2

次に例を示します。

struct typePerson {
  string firstName;
  string lastName;

  typePerson(string firstName,string lastName) : firstName(firstName), lastName(lastName) {}  

  bool operator==(const typePerson& p2) const {
    const typePerson& p1=(*this);
    return p1.firstName == p2.firstName && p1.lastName == p2.lastName;
  }
};

サンプル使用法:

int main() {
  typePerson p1("John","Doe");
  typePerson p2("Jane","Doe");
  typePerson p3("John","Doe");

  if (p1 == p2)
    cout<<"p1 equals p2"<<endl;
  else
    cout<<"p1 does not equal p2"<<endl;

  if (p1 == p3)
    cout<<"p1 equals p3"<<endl;
  else
    cout<<"p1 does not equal p3"<<endl;


  return 0;
}

---------- Capture Output ----------
p1 does not equal p2
p1 equals p3

> Terminated with exit code 0.
于 2012-07-06T12:14:01.193 に答える