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



  • 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.


  • Moderators

    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.



  • 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.



  • 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);)


  • Moderators

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



  • 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;
    }
    @



  • 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 []



  • 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 ! :(



  • 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;@



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.