1

I'm working with QT and I created my custom class (MyClass) as Singleton.

//MyClass.cpp

MyClass(){}

MyClass* MyClass::getInstance()
{
    if(!instance)
        instance = new MyClass();
    return instance;
}


//MyClass.h

public:
    static MyClass* getInstance();

private:
    MyClass();
    static MyClass* instance;

Now I need to overload operator "<<" for it, so i could call it like this in code:

 MyClass::getInstance()<<( QString("Tekst") );

But after many attempts - I can't get how should I do it. I tried in many ways but i still only can get either - incorrect declaration errors (in MyClass.h) or

no match for 'operator<<' (operand types are 'MyClass*' and 'QString')

for line:

 MyClass::getInstance()<<( QString("Tekst") );

EDIT 1

Basicly, i want to use this class to save those QStrings. It shoud be similar to QDebug, but i got my specified "place" to save it - still, way to input data shoud be the same (with <<).

EDIT 2

Sicne few ppl were argueing i didnt een try to code this - here are few of my attempts (just declarations):

    MyClass  &operator<<(QString qds);
    //----
    MyClass* & operator<<(QString text);
    //----
    void  operator<<(QString rhs);
    //----
    MyClass*  operator<<(QString rhs);
    //----
    friend QString &operator<<(QString &input, const MyClass &mc );
    //----
     MyClass & operator<<(MyClass& out, const QString& text);
Asker
  • 89
  • 8
  • 3
    How is your `operator <<` defined? The compiler does not make one for you. – NathanOliver Feb 25 '16 at 14:03
  • It's all i coded for it. I didnt "define" it in anyway, it seems. I thought all classes can basicly use overloading for oeprators, just like in this example: http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm – Asker Feb 25 '16 at 14:08
  • 2
    They can but you have to write the code to overloads the operator. See how they wrote the function `Box operator+(const Box& b)` to allow you to use `+` to add two boxes together. – NathanOliver Feb 25 '16 at 14:10
  • as i wrote - i made many attempts to make it, but none where correct - that's why i'm asking here. Exemp. : MyClass & operator<<( QString text); – Asker Feb 25 '16 at 14:12
  • Note what the compiler expected the parameters to be *(operand types are 'MyClass\*' and 'QString')*. – Bo Persson Feb 25 '16 at 14:37
  • I can clearly see that, but still- I dont know how to use that information – Asker Feb 25 '16 at 14:38
  • @Asker, I think, you need to read some material on operator overloading. We can write the code for you, but it'd do you more good if you learn how to do it yourself. So read the book, code yourself, and if the code doesn't work, show us the code and ask for help. – SergeyA Feb 25 '16 at 14:48
  • [FYI] since you have a public constructor yo do not have a singleton. – NathanOliver Feb 25 '16 at 14:48
  • @NathanOliver, as well as default copy ctor. However, it's a good thing, snce singletons must be banned. – SergeyA Feb 25 '16 at 14:49
  • @SergeyA I might agree with you... But after few hours or searching and trying I convinced i can't make it on my own. I even doubt it's possible. – Asker Feb 25 '16 at 14:51
  • @NathanOliver It's no realy an issuie here. Just small msitakey while moving code here. – Asker Feb 25 '16 at 14:52
  • Possible duplicate of [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) –  Feb 25 '16 at 14:53
  • @Asker, it is possible. Do not search for examples. Instead, read the book on operator overloading, than try to code one. Show us the code. – SergeyA Feb 25 '16 at 14:55
  • @Asker: judging from your second edit, it looks like you're just throwing code at the problem and hoping you'll get lucky. You won't. So, besides actually understanding operator overloading, I suggest you start with a simpler case: get it working with a normal class first, and then we'll move on. So get something like this to build and function correctly: `SomeClass object; object << QString("test");` – Ionut Feb 25 '16 at 15:27

1 Answers1

0

You can either write the operator as function, outside of class MyClass:

void operator<< (MyClass* myClass, QString string);

or, you can change the call to

(*MyClass::getInstance())<<( QString("Tekst") );

or you can change getInstance so that it returns a reference

MyClass& MyClass::getInstance()
{
    if(!instance)
        instance = new MyClass();
    return *instance;
}

In the last to cases, you code the operator as member

void MyClass::operator<< (QString string);

or as function

void operator<< (MyClass& myClass, QString string);

You can change the return type, if you want to use something like

MyClass::getInstance() << QString("Tekst") << QString("other Tekst");