2011-06-27 22 views
14

Böyle, operator[] sahip bir sınıf yaşıyorum:Operatör [] ile bir alay sınıfı nasıl oluşturulur?

class Base 
{ 
    public: 
    virtual ~Base(){} 
    virtual const int & operator[](const unsigned int index) const = 0; 
}; 

nasıl bu yöntem için sahte bir çerçeve google kullanarak sahte sınıfını oluşturabilir?

class MockBase : public Base 
{ 
public: 
    MOCK_CONST_METHOD1(operator[], 
         const int& (const unsigned int) 
        ); 
}; 

ama bu önümüzdeki hataları üretir:

Bu çalıştı

error: pasting "]" and "_" does not give a valid preprocessing token 
error: pasting "]" and "_" does not give a valid preprocessing token 
error: pasting "]" and "_" does not give a valid preprocessing token 
error: pasting "]" and "_" does not give a valid preprocessing token 

cevap

18

MOCK_METHOD# makro operatörler üzerinde çalışmaz. this message'da verilen çözüm, alay için düzenli bir yöntem oluşturmayı söyler:

class Base { 
public: 
virtual ~Base() {} 
virtual bool operator==(const Base &) = 0; 
}; 

class MockBase: public Base { 
public: 
MOCK_METHOD1(Equals, bool(const Base &)); 
virtual bool operator==(const Base & rhs) { return Equals(rhs); } 
}; 
+0

Teşekkürler. Bu iyi çalışıyor –

+0

Bu, görev operatörü için nasıl çalışır? – Mawg

İlgili konular