You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, March 31st 2012, 1:35am

Array of pointers to class object problem (C++)

Hello everyone.

I'm gonna get straight to the point.

I'm trying to dynamically create an array of pointers to my objects.

Here's my Class(lex.h):

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Lex {

private:
    QString m_LHS;
    QString m_RHS;

protected:
    static int lexCount;

public:
    Lex(const QString LHS, const QString RHS);
    Lex();
    void setLHS(QString LHS)
    {
        this->m_LHS = LHS;
    }
    QString getLHS()
    {
        return this->m_LHS;
    }
    void setRHS(QString RHS)
    {
        this->m_RHS = RHS;
    }
    QString getRHS()
    {
        return this->m_RHS;
    }
};

lex.cpp:

Source code

1
2
3
4
5
6
7
8
9
10
11
Lex::Lex(const QString LHS, const QString RHS)
{
    this->m_LHS = LHS;
    this->m_RHS = RHS;
}

Lex::Lex()
{
    this->m_LHS = "";
    this->m_RHS = "";
}


This is my code:

Source code

1
2
3
4
5
6
7
8
9
10
    int lexObjectCount = 10;
    Lex **lexs;
    lexs = new Lex *[lexObjectCount];

    int i;
    for(i=0;i<10;i++)
    {
        lexs[i]->setLHS(QString::number(i));
        qDebug() << lexs[i]->getLHS();
    }


The output for this code is:

Quoted

"0"
"1"

Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly

RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly
I also tried doing this statically:

Source code

1
2
3
4
5
6
7
    Lex *lexs[10];
    int i;
    for(i=0;i<10;i++)
    {
        lexs[i]->setLHS(QString::number(i));
        qDebug() << lexs[i]->getLHS();
    }


But i got no change in the output..Sometimes the output stops after "0".But never have i gotten more than two results..

What am I doing wrong? Any help would be greatly appreciated.

Thanks in advance,
Mukunku

This post has been edited 1 times, last edit by "mukunku" (Mar 31st 2012, 1:43am)


2

Sunday, April 1st 2012, 1:18am

Changing the code to this, solved my problem:

Source code

1
2
3
4
5
6
7
8
9
10
11
   int lexObjectCount = 10;
    Lex **lexs;
    lexs = new Lex *[lexObjectCount];

    int i;
    for(i=0;i<10;i++)
    {
        lexs[i] = new Lex;
        lexs[i]->setLHS(QString::number(i));
        qDebug() << lexs[i]->getLHS();
    }

3

Monday, April 2nd 2012, 5:28pm

Hello everyone.

I'm gonna get straight to the point.

I'm trying to dynamically create an array of pointers to my objects.


Why? use QList or std::vector.

and why even use pointers at all?

what is wrong with

std::vector<Lex> ?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar threads