Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Operator<< Overloading for std::cout, does not work.
QtWS25 Last Chance

[Solved] Operator<< Overloading for std::cout, does not work.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Galiego710
    wrote on last edited by
    #1

    I have simple console project in Qt, where I am overloading << operator.
    The next files:

    • Overload_operation.pro
    • main.cpp
    • MyClass.cpp
    • MyClass.h

    Contents:
    Overload_operation.pro
    @QT += core
    QT -= gui

    TARGET = Overload_operation
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp
    MyClass.cpp

    HEADERS +=
    MyClass.h
    @
    main.cpp
    @#include <QtCore/QCoreApplication>
    #include <iostream>
    #include "MyClass.h"

    using namespace std;

    int main(int argc, char *argv[])
    {
    cout << "Test" << endl;
    return 0;
    }@
    MyClass.h
    @#ifndef MYCLASS_H
    #define MYCLASS_H

    #include "iostream"
    using namespace std; ///< Using std

    /// My simple Class
    class MyClass
    {
    private:
    int m_val; ///< value
    public:
    MyClass();

    void setVal(int val);   ///< set value
    int val();              ///< get value
    
    /// Set the friend function
    friend ostream& operator<<(ostream& cout_, const MyClass& obj_);
    

    };

    /// overloading operator<<
    ostream& operator<<(ostream& currCout, const MyClass& currObj)
    {
    currCout << currObj.m_val;
    return currCout;
    }

    #endif // MYCLASS_H@
    MyClass.cpp
    @#include "MyClass.h"
    #include "iostream"

    MyClass::MyClass()
    {
    m_val = 0;
    }

    void MyClass::setVal(int val)
    {
    m_val = val;
    }

    int MyClass::val()
    {
    return m_val;
    }@

    But I get the next errors:
    @
    D:\Projects_Qt\MyExamples\Overload_operation-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\debug\MyClass.o:-1: In function `ZlsRSoRK7MyClass':

    D:\Projects_Qt\MyExamples\Overload_operation-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug..\Overload_operation\MyClass.h:24: multiple definition of `operator<<(std::ostream&, MyClass const&)'

    D:\Projects_Qt\MyExamples\Overload_operation-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug..\Overload_operation\MyClass.h:24: error: first defined here

    :-1: error: collect2: ld returned 1 exit status
    @

    Here is written that "multiple definition of `operator<<(std::ostream&, MyClass const&)'", but when was it?
    How I can make it correct?

    Thanks.

    ——————
    Best regards,
    Galiego710

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      The problem is, that you implement the function in the header file, which implies, it is implemented in each include. Implement it inthe cpp file and it is fine.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Galiego710
        wrote on last edited by
        #3

        I put it
        @/// overloading operator<<
        ostream& operator<<(ostream& currCout, const MyClass& currObj)
        {
        currCout << currObj.m_val;
        return currCout;
        }@
        in any cpp file. It's work!
        Strangely, before I did about the same, but did not work.

        I found a few useful links for operator<< qDebug() in Qt doc
        http://doc.trolltech.com/4.6/debug.html#providing-support-for-the-qdebug-stream-operator
        http://doc.trolltech.com/4.6/custom-types.html

        Thank you very much!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          It works in the header file if you have an operator definition inside a class declaration.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved