Ćwiczenie map - jak usunąć z mapy obiekt osoby o danym ID

2012-05-16 13:02
0

Witam. Ćwiczę sobie teraz mapy w C++ i mam z nimi problem. Poniższy kod pokazuje mi błędy, które wkleję niżej.

A moje podstawowe pytanie: jak znaleźć w tej mapie Osobę o podanym ID? Prosze o pomoc

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

class Osoba {

public :
    string imie, nazwisko;
    int ID;

    Osoba(string i, string n, int id) {imie = i; nazwisko = n, ID = id;}
    bool operator==(const Osoba &q) {return ID ==q.ID && imie == q.imie && nazwisko == q.nazwisko;}
    friend ostream & operator<< (ostream &wyjscie, const Osoba &s);
};

ostream & operator<< (ostream &wyjscie, const Osoba s)
{
        return wyjscie << s.imie << " " << s.nazwisko << " " << s.ID << "\n";
}

int main()
{

    Osoba o1("Jan", "Kowalski", 123);
    Osoba o2("Adam", "Nowak", 1234);
    Osoba o3("Anna", "Kowalska", 12345);

    map<const int, Osoba> osoby;
    map<const int, Osoba>::iterator it;

    osoby.insert(make_pair(1, o1));
    osoby.insert(make_pair(2, o2));
    osoby.insert(make_pair(3, o3));

    it = osoby.begin();

    for ( it=osoby.begin() ; it != osoby.end(); it++ )
        cout << (*it).first << " => " << (*it).second << endl;

    return 0;
}

Błędy:

E:\mapa.cpp||In function 'int main()':|
E:\mapa.cpp|41|error: ambiguous overload for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >)std::cout.std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](it.std::_Rb_tree_iterator<_Tp>::operator [with _Tp = std::pair<const int, Osoba>]()->std::pair<const int, Osoba>::first))), ((const char*)" => ")) << it.std::_Rb_tree_iterator<_Tp>::operat|
E:\mapa.cpp|19|note: candidates are: std::ostream& operator<<(std::ostream&, Osoba)|
E:\mapa.cpp|15|note: std::ostream& operator<<(std::ostream&, const Osoba&)|
||=== Build finished: 1 errors, 0 warnings ===|

A chce znalezc w mapie osobe o podanym ID, np 123

edytowany 2x, ostatnio: kondrd, 2012-05-16 13:19

Pozostało 580 znaków

2012-05-16 13:23
0

brak const przy tych ostream& ?


Masz problem? Pisz na forum, nie do mnie. Nie masz problemów? Kup komputer...

Pozostało 580 znaków

2012-05-16 13:28
0

Nie dało to nic. A czy szukanie moze byc tak, jak napisalem?

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

class Osoba {

public :
    string imie, nazwisko;
    int ID;

    Osoba(string i, string n, int id) {imie = i; nazwisko = n, ID = id;}
    bool operator==(const Osoba &q) {return ID ==q.ID && imie == q.imie && nazwisko == q.nazwisko;}
    friend ostream & operator<< (ostream &wyjscie, const Osoba &s);
};

ostream & operator<< (const ostream &wyjscie, const Osoba s)
{
        return wyjscie << s.imie << " " << s.nazwisko << " " << s.ID << "\n";
}

bool szukaj (int szukane, map<const int, Osoba> &osoby) {
    map<const int, Osoba>::iterator it;
    for ( it=osoby.begin() ; it != osoby.end(); it++ )
        if((*it).second.ID == szukane)
            return true;
    return false;
}

int main()
{

    Osoba o1("Jan", "Kowalski", 123);
    Osoba o2("Adam", "Nowak", 1234);
    Osoba o3("Anna", "Kowalska", 12345);

    map<const int, Osoba> osoby;
    map<const int, Osoba>::iterator it;

    osoby.insert(make_pair(1, o1));
    osoby.insert(make_pair(2, o2));
    osoby.insert(make_pair(3, o3));

    it = osoby.begin();

    for ( it=osoby.begin() ; it != osoby.end(); it++ )
        cout << (*it).first << " => " << (*it).second << "\n";

    cout << szukaj(1234, osoby) << "\n";
    cout << szukaj(11, osoby) << "\n";

    return 0;
}

Bledy:

E:\mapa.cpp||In function 'std::ostream& operator<<(const std::ostream&, Osoba)':|
E:\mapa.cpp|21|error: no match for 'operator<<' in 'wyjscie << s.Osoba::imie'|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|117|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (
)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|127|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|165|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|169|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|173|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|91|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|180|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|105|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|191|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|200|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|204|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|209|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|213|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|221|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|225|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void
) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|119|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]|
E:\mapa.cpp|19|note: std::ostream& operator<<(const std::ostream&, Osoba)|
||=== Build finished: 1 errors, 0 warnings ===|

Pozostało 580 znaków

2012-05-16 13:49
0
ostream & operator<< (const ostream &wyjscie, const Osoba s)
{
        wyjscie << s.imie << " " << s.nazwisko << " " << s.ID << "\n";
        return wyjscie;
}

Masz problem? Pisz na forum, nie do mnie. Nie masz problemów? Kup komputer...

Pozostało 580 znaków

2012-05-16 14:01
0

E:\mapa.cpp||In function 'std::ostream& operator<<(const std::ostream&, Osoba)':|
E:\mapa.cpp|21|error: no match for 'operator<<' in 'wyjscie << s.Osoba::imie'|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|117|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (
)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|127|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|165|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|169|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|173|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|91|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|180|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|105|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|191|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|200|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|204|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|209|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|213|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|221|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\ostream|225|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void
) [with _CharT = char, _Traits = std::char_traits<char>]|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|119|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]|
E:\mapa.cpp|19|note: std::ostream& operator<<(const std::ostream&, Osoba)|
E:\mapa.cpp|22|error: invalid initialization of reference of type 'std::ostream&' from expression of type 'const std::basic_ostream<char, std::char_traits<char> >'|
||=== Build finished: 2 errors, 0 warnings ===|

Pozostało 580 znaków

2012-05-16 14:20

Ech bo skopiowałem z tego twojego ostatniego kodu, a tam kolejne kwiatki ;]

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

class Osoba {

public :
    string imie, nazwisko;
    int ID;

    Osoba(string i, string n, int id) {imie = i; nazwisko = n, ID = id;}
    bool operator==(const Osoba &q) {return ID ==q.ID && imie == q.imie && nazwisko == q.nazwisko;}
    friend ostream & operator<< (ostream &wyjscie, const Osoba &s);
};

ostream &operator<< (ostream &wyjscie, const Osoba& s)
{
        wyjscie << s.imie << " " << s.nazwisko << " " << s.ID << "\n";
        return wyjscie;
}

bool szukaj (int szukane, map<const int, Osoba> &osoby) {
    map<const int, Osoba>::iterator it;
    for ( it=osoby.begin() ; it != osoby.end(); it++ )
        if((*it).second.ID == szukane)
            return true;
    return false;
}

int main()
{

    Osoba o1("Jan", "Kowalski", 123);
    Osoba o2("Adam", "Nowak", 1234);
    Osoba o3("Anna", "Kowalska", 12345);

    map<const int, Osoba> osoby;
    map<const int, Osoba>::iterator it;

    osoby.insert(make_pair(1, o1));
    osoby.insert(make_pair(2, o2));
    osoby.insert(make_pair(3, o3));

    it = osoby.begin();

    for ( it=osoby.begin() ; it != osoby.end(); it++ )
        cout << (*it).first << " => " << (*it).second << "\n";

    cout << szukaj(1234, osoby) << "\n";
    cout << szukaj(11, osoby) << "\n";

    return 0;
}

http://ideone.com/uChMi


Masz problem? Pisz na forum, nie do mnie. Nie masz problemów? Kup komputer...

Pozostało 580 znaków

2012-05-16 14:22
1
Shalom napisał(a):

brak const przy tych ostream& ?

Przy ostream & nie może być przecież const. On ma zamiar pisać do tego strumienia!

Bez const wszystko działa: http://ideone.com/sLz28


"(...) otherwise, the behavior is undefined".
edytowany 1x, ostatnio: Endrju, 2012-05-16 14:24

Pozostało 580 znaków

2012-05-16 14:35
0

Dziękuję Wam, wszystko działa

Pozostało 580 znaków

Liczba odpowiedzi na stronę

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