开发者

unable to construct a complex structure in c++

开发者 https://www.devze.com 2022-12-15 21:11 出处:网络
I have two base structures like following : struct stuSectionProperties { int Field1; // Row | BoxNo| SplitterNo

I have two base structures like following :

struct stuSectionProperties
{

    int Field1; // Row | BoxNo   | SplitterNo
    int Field2; // Col | Adapter |     -
    double Latitude;
    bool IsEast;
    int Band;
    int CableNo;
    SDP::Global::enuSections::Type Section;

    stuSectionProperties()
    {
        this->Field1 = -1;
        this->Field2 = -1;
        this->Latitude = -1;
        this->Band = -1;
        this->Section = SDP::Global::enuSections::None;
        this->Cab开发者_如何学GoleNo = -1;
    }
    const char* toStr()
    {
        return ((QString) (QString::number(this->Field1) + " , " + QString::number(this->Field2) + " , " + QString::number(Latitude) + " , " + QString::number(IsEast) + " , " + QString::number(Band) + " , "
                        + QString::number((int) Section) + QString::number((int) CableNo))).toStdString().c_str();
    }
};

and

struct stuSearchResult
{
    stuSectionProperties MyData;
    QList<stuSectionProperties> Connections;

    stuSearchResult()
    {
        this->MyData.Field1 = -1;
        this->MyData.Field2 = -1;
        this->MyData.Latitude = -1;
        this->MyData.Band = -1;
        this->MyData.Section = SDP::Global::enuSections::None;
        this->MyData.CableNo = -1;

        stuSectionProperties stuDummy;
        stuDummy.Band=-1;
        stuDummy.CableNo=-1;
        stuDummy.Field1=-1;
        stuDummy.Field2=-1;
        stuDummy.IsEast=-1;
        stuDummy.Latitude=-1;

        stuDummy.Section= SDP::Global::enuSections::None;
        this->Connections.append(stuDummy);

    }
    const char * toStr()
    {
        return ((QString) (QString::number(this->MyData.Field1) + " , " + QString::number(this->MyData.Field2) + " , " + QString::number(this->MyData.Latitude) + " , " + QString::number(this->MyData.IsEast) + " , " + QString::number(this->MyData.Band) + " , "
                        + QString::number((int) this->MyData.Section) + QString::number((int) this->MyData.CableNo)) + " , " + QString::number(this->Connections[0].Field1) + " , " + QString::number(this->Connections[0].Field2) ).toStdString().c_str();
    }
};

whenever I try to create an instance out of second the structure and then try to call its toStr() member I'll get an erro which is saying that these lines have some issues :

+ QString::number(this->Connections[0].Field1) + " , " + QString::number(this->Connections[0].Field2)

can you please tell me whats my problem exactly? regards.


the assert error says Connections doesn't have the element you referenced. if you print out Connections.size() how many elements does it say the list contains?

returning string.c_str() could be a potential memory issue, as it's returning a pointer to memory that has been freed.

in c++, custom printing is often done by overloading the stream operator in your class:

friend std::ostream &operator<<(std::ostream &os, const myClass &c)
{
  return os << c.some << c.val;
}

...

cout << myObj << endl;

...

strstream ss;
ss << "cool: " << myObj << " " << 55;
string s = ss.str();

you could also just pass a reference to a string object in your toStr function:

makeString(string &s)
{
  ...
  s = qstr.toStdString();
}

string s;
makeString(s);


Yes the code has many issues

  • Normally people don't write "this->x"; they just write "x"; the "this->" is implicit inside methods
  • As the other answer pointed out, c_str() points to invalid memory here

However, the Connections list should contain at least one element because the constructor appends it there. I think it would help if the poster would also post the code to allocate the structure. It looks like the constructor doesn't get called OR there is other code that clears the Connections list before toStr() is actually called.

0

精彩评论

暂无评论...
验证码 换一张
取 消