2016-04-26 7 views
1

Ich benutze for_each und mem_fun_ref als Beispiel, aber es gibt einige Fehler in der Kompilierung, was ist das Problemfor_each und mem_fun_ref Mühe

#include<iostream> 
#include<algorithm> 
#include<set> 
#include<iterator> 
using namespace std; 

class Tst 
{ 
public: 
    Tst(int a, string b):n(a),s(b) 
{} 

    bool operator<(const Tst& t)const 
    { 
     return this->n < t.n; 
    } 

    int GetN()const 
    { 
     return n; 
    } 

    string GetS()const 
    { 
     return s; 
    } 

    void SetN(int a) 
    { 
     n = a; 
    } 

    void SetName(string name) 
    { 
     s = name; 
    } 

    void Print(void) 
     { 
     cout <<"n is:" << n <<"\ts is:" << s << endl; 
    } 

private: 
    int n; 
    string s; 
}; 

int main(void) 
{ 
    typedef set<Tst> TstSet; 
TstSet tst; 

tst.insert(Tst(10, "abc")); 
tst.insert(Tst(1, "def")); 
for_each(tst.begin(), tst.end(), mem_fun_ref(&Tst::Print)); 
return true; 
} 

: 4200: 错误: 对 '(std :: mem_fun_ref_t) (const Tst &) '的 调用 没有 匹配, 是 什么 原因

Antwort

4

std::set' s enthaltenen Objekte const sind, so können Sie const Funktionen auf sie nur anrufen. Ihre Print Funktion sollte const markiert werden.

+0

danke funktioniert, ist es verstehen –

0

sollte die Funktion const sein, weil std::set nur mit const Objekte

void Print(void)const 
{ 

} 
Verwandte Themen