Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED]no error reporting,but cout doesn't work
Forum Update on Monday, May 27th 2025

[SOLVED]no error reporting,but cout doesn't work

Scheduled Pinned Locked Moved C++ Gurus
10 Posts 5 Posters 3.2k 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.
  • N Offline
    N Offline
    nimingzhe2008
    wrote on last edited by
    #1

    my code

    @#include<QApplication>
    #include<iostream>
    using namespace std;

    class IntArray
    {
    int length;
    int *data;
    public: IntArray():length(0),data(0)
    {}
    IntArray(int x)
    {
    length=x;
    data=new int[x];
    }
    int& operator[](int x)
    {
    return data[x];
    }

    };

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    IntArray m(6);
    m[3]=9;

    cout<<m[3];

    return app.exec();
    }@

    when I compiled it,nothing happened.Help,please.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are not actually using QApplication here, so you can drop those 2 lines. Please wrap your code in '@' tags, it's hard to read it in it's current form.

      (Z(:^

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

        well, first of all please use '@ '@ code markers. So you code will be more readable.

        Regarding you example, I am also using cout and I did not have any problems. You code also looks correct, with only one exception. If you are using cout and then goes into main loop

        @
        app.exec()
        @

        then you will not see output on console. If you will just call

        @
        return 0;
        @

        then you will see your output. You can always use qDebug() to see output if you need main loop in you application.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          What does this line:
          m=9;
          mean?

          And:
          cout << m;
          This won't work because your IntArray has no "<<" operator.
          (ostream& operator<<(ostream& os, const IntArray& ia);)

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            I think it's m [ 3 ], but the forum software changed that into an anchor.

            (Z(:^

            1 Reply Last reply
            0
            • B Offline
              B Offline
              broadpeak
              wrote on last edited by
              #6

              I've found you a similar code,
              from this you can understand the essentials:
              @
              #include <iostream>
              #include <sstream>
              #include <cstring>

              using namespace std;

              class IntArray {
              enum { sz = 5 };
              int i[sz];
              public:
              IntArray() { memset(i, 0, sz* sizeof(*i)); }
              int& operator[](int x) { return i[x]; }
              friend ostream& operator<<(ostream& os, const IntArray& ia);
              friend istream& operator>>(istream& is, IntArray& ia);
              };

              ostream& operator<<(ostream& os, const IntArray& ia)
              {
              for(int j = 0; j < ia.sz; j++)
              {
              os << ia.i[j];
              if(j != ia.sz -1) os << ", ";
              }
              os << endl;
              return os;
              }

              istream& operator>>(istream& is, IntArray& ia)
              {
              for(int j = 0; j < ia.sz; j++) is >> ia.i[j];
              return is;
              }

              int main()
              {
              stringstream input("47 34 56 92 103");
              IntArray I;
              input >> I;
              I[4] = -1;
              cout << 1;
              cout << I;
              }
              @

              1 Reply Last reply
              0
              • G Offline
                G Offline
                guziemic
                wrote on last edited by
                #7

                In my opinion, you do not have to define operator for << because you return int which is of know type for cout in overloaded operator []

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  issam
                  wrote on last edited by
                  #8

                  my code

                  @
                  #include<QApplication>
                  #include<iostream>
                  using namespace std;

                  class IntArray
                  {
                  private:
                  int length;
                  int *data;

                  public:
                  IntArray():length(0),data(0)
                  {
                  }

                           IntArray(int x) 
                           { 
                               length=x; 
                               data=new int[x]; 
                           } 
                  
                           int& operator[](int x) 
                           { 
                             return data[x]; 
                           }
                  

                  };

                  int main(int argc, char *argv[])
                  {
                  QApplication app(argc, argv);

                  IntArray m(6);
                  m[3]=9;

                  cout<<m[3];

                  return app.exec();
                  }
                  @

                  :)

                  is it clear no ! :(

                  http://www.iissam.com/

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    issam
                    wrote on last edited by
                    #9

                    And where is the implementation of

                    @operator=()@

                    ?
                    m() is an object not an array !

                    You have not a GUI so you can write :

                    @return 0;@

                    http://www.iissam.com/

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nimingzhe2008
                      wrote on last edited by
                      #10

                      Thanks everyone.There is something wrong with my .pro file.Now it works.

                      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