-2

我正在尝试读取 XML 中的元素并将其存储在一个数组中, struct并且需要将此数组的指针传递给其他函数。但是我在 gnu 中编译时遇到问题,错误消息:

错误:无法转换myRecuint32_t{aka unsigned int}' 作为回报 return *recs;

尝试不设置myRec recs[count]malloc得到无效指针的错误。

struct myRec
{
std::string one;
std::string two;
std::string three;
std::string four;
std::string five;
std::string six;
};

uint32_t count = 0;

XMLDocument doc;
doc.LoadFile(pFilename);
XMLElement* parent = doc.FirstChildElement("a");
XMLElement* child = parent->FirstChildElement("b");
XMLElement* e = child->FirstChildElement("c");

for (e = child->FirstChildElement("c"); e; e = e->NextSiblingElement("c"))
{
    count++;
}

std::cout << "\n""Count = " << count << std::endl;

recs = (myRec *)malloc(6 *count * sizeof(myRec));

XMLElement *row = child->FirstChildElement();
if (count > 0)
{
    --count;
    count = (count < 0) ? 0 : count;
    for (uint32_t i = 0; i <= count; i++)
    {
        while (row != NULL)
        {
            std::string six;
            six = row->Attribute("ID");
            recs[i].six = six;

            XMLElement *col = row->FirstChildElement();
            while (col != NULL)
            {
                std::string sKey;
                std::string sVal;
                char *sTemp1 = (char *)col->Value();
                if (sTemp1 != NULL) {
                    sKey = static_cast<std::string>(sTemp1);
                }
                else {
                    sKey = "";
                }
                char *sTemp2 = (char *)col->GetText();
                if (sTemp2 != NULL) {
                    sVal = static_cast<std::string>(sTemp2);
                }
                else {
                    sVal = "";
                }
                if (sKey == "one") {
                    recs[i].one = sVal;
                }
                if (sKey == "two") {
                    recs[i].two = sVal;
                }
                if (sKey == "three") {
                    recs[i].three = sVal;
                }
                if (sKey == "four") {
                    recs[i].four = sVal;
                }
                if (sKey == "five") {
                    recs[i].five = sVal;
                }
                col = col->NextSiblingElement();
            }// end while col
            std::cout << "\n""one = " << recs[i].one << "\n"" two= " << recs[i].two << "\n""three = " << recs[i].three << "\n""four = " << recs[i].four << "\n""five = " << recs[i].five << "\n""six = " << recs[i].six << std::endl;
            row = row->NextSiblingElement();
        }// end while row
    }
}
else
{
    std::cout << "Failed to find value, please check XML! \n" << std::endl;
}
return *recs;

期望返回一个指向数组的指针

我将其声明为:

std::string getxmlcontent(const char* pFilename);
myRec*recs= NULL;

功能:

std::string readxml::getxmlcontent(const char* pFilename)
{
}

不确定这是否是正确的方法,因为我对 c++ 很陌生

4

1 回答 1

1

你犯了一些错误,你可能应该得到一本好的 C++ 书并做一些学习

在 C++ 中使用new

recs = new myRec[6*count];

代替

recs = (myRec *)malloc(6 *count * sizeof(myRec));

C++ 程序的问题malloc在于它不会调用构造函数,因此结构中的所有字符串都是无效的,并且(很可能)您的程序在运行时会崩溃。

我不清楚你为什么需要6*count,这似乎是因为你的结构中有六个字符串。如果是这样,那就是混乱的想法,真的你只需要

recs = new myRec[count];

你会得到6*count字符串,因为这就是你声明你的结构的方式。

sKey = sTemp1;

代替

sKey = static_cast<std::string>(sTemp1);

不需要演员,将 a 分配给 a 是完全合法char*std::string

最后,如果要返回指针,则只需返回指针

return recs;

不是

return *recs;

但是,您尚未在发布的代码中包含函数签名。我怀疑还有另一个错误,但除非您发布如何声明此函数,否则我无法判断。

于 2019-07-18T08:13:12.723 回答