Porównanie wektora obiektów, wektor zawiera obiekty własnej klasy

0

Mam klasy:

Pierwsza{
         //...
         vector<Druga> w;
         friend bool	operator==(const Pierwsza&, const Pierwsza&);
};
Druga{
         //...
         friend bool	operator==(const Druga&, const Druga&);
};

Czy wystarczy porównanie:

Pierwsza p1, p2; cout << p == p2; 

?

0

sprawdzic sprawdze, ale jeszcze bym sie chcial upewnic:}

0
#include <cstdlib>
#include <iostream>
using std::cout;

class B{
    public:
    int a;
    friend bool operator==(const B&, const B&);
};

bool operator==(const B& b, const B& b){
    return b.a == b.a;
}

class A{
public:
    vector<B> wektor;
    friend bool operator==(const A&, const A&);
};

bool operator==(const A& a, const A& a){
    return a == a;
}


int main(){

    A a1,a2;
    B b1,b2,b3;
    b1.a = 1;
    b2.a = 2;
    b3.a = 3;

    a1.wektor.push_back(b1);
    a1.wektor.push_back(b3);

    a2.wektor.push_back(b2);

    bool c = a1 == a2;
    cout << c;

    return 0;
}

G:\Untitled1.cpp|11|error: redefinition of 'const B& b'|
G:\Untitled1.cpp|11|error: 'const B& b' previously declared here|
G:\Untitled1.cpp|11|error: 'bool operator==(const B&)' must take exactly two arguments|
G:\Untitled1.cpp|17|error: ISO C++ forbids declaration of 'vector' with no type|
G:\Untitled1.cpp|17|error: expected ';' before '<' token|
G:\Untitled1.cpp|21|error: redefinition of 'const A& a'|
G:\Untitled1.cpp|21|error: 'const A& a' previously declared here|
G:\Untitled1.cpp|21|error: 'bool operator==(const A&)' must take exactly two arguments|
G:\Untitled1.cpp||In function 'int main()':|
G:\Untitled1.cpp|34|error: 'class A' has no member named 'wektor'|
G:\Untitled1.cpp|35|error: 'class A' has no member named 'wektor'|
G:\Untitled1.cpp|37|error: 'class A' has no member named 'wektor'|
||=== Build finished: 11 errors, 0 warnings ===|

?

0

a ponoc c/c++ jest case sensitive:

#include <cstdlib>
#include <vector>
#include <iostream>
using std::cout;
using namespace std;

class B{
    public:
    int a;
    friend bool operator==(const B&obb1, const B&obb2);
};

bool operator==(const B& ob1, const B& ob2){
    return ob1.a == ob2.a;
}

class A{
public:
    vector<B> wektor;
    friend bool operator==(const A& obb1, const A&obb2);
};

bool operator==(const A& ob1, const A& ob2){
    return ob1 == ob2;
}


int main(){

    A a1,a2;
    B b1,b2,b3;
    b1.a = 1;
    b2.a = 2;
    b3.a = 3;

    a1.wektor.push_back(b1);
    a1.wektor.push_back(b3);

    a2.wektor.push_back(b2);

    bool c = a1 == a2;
    cout << c;

    return 0;
}

daje w C:

Process returned -1073741819 (0xC0000005) execution time : 0.422 s
Press any key to continue.

0

ja wiem, temat wyglada jak monolog, ale to rozwiazanie jakos mnie nie przekonuje (aczkolwiek dziala:]):

#include <cstdlib>
#include <vector>
#include <iostream>
using std::cout;
using namespace std;

class B{
    public:
    int a;
    friend bool operator==(const B&obb1, const B&obb2);
};

bool operator==(const B& ob1, const B& ob2){
    return ob1.a == ob2.a;
}

class A{
public:
    vector<B> wektor;
    friend bool operator==(const A& obb1, const A&obb2);
};

bool operator==(const A& ob1, const A& ob2){
    for(int i=0; i<ob1.wektor.size(); i++)
        if(!(ob1.wektor[i] == ob2.wektor[i]))
           return false;
    return true;
}


int main(){

    A a1,a2;
    B b1,b2,b3;
    b1.a = 1;
    b2.a = 2;
    b3.a = 3;

    a1.wektor.push_back(b1);
    a1.wektor.push_back(b3);

    a2.wektor.push_back(b2);

    bool c = a1 == a2;
    cout << c << "\n";


    a1.wektor.clear();
    a2.wektor.clear();

    a1.wektor.push_back(b2);
    a2.wektor.push_back(b2);

    c = a1 == a2;
    cout << c;

    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1