2015-12-06 95 views
-3

如何通过函数参数来修正返回创建的std :: list?现在,我尝试这样:C++在函数中创建std :: list并通过参数返回

bool DatabaseHandler::tags(std::list<Tag> *tags) 
{ 
    QString sql = "SELECT * FROM " + Tag::TABLE_NAME + ";"; 
    QSqlQueryModel model; 
    model.setQuery(sql); 

    if(model.lastError().type() != QSqlError::NoError) { 
     log(sql); 
     tags = NULL; 
     return false; 
    } 

    const int count = model.rowCount(); 

    if(count > 0) 
     tags = new std::list<Tag>(count); 
    else 
     tags = new std::list<Tag>(); 
//some code 

    return true; 
} 

后,我可以用它:

std::list<Tag> tags; 
mDB->tags(&tags); 

现在,我解决我的功能:

bool DatabaseHandler::tags(std::list<Tag> **tags) 
{ 
    QString sql = "SELECT * FROM " + Tag::TABLE_NAME + ";"; 
    QSqlQueryModel model; 
    model.setQuery(sql); 

    if(model.lastError().type() != QSqlError::NoError) { 
     log(sql); 
     *tags = NULL; 
     return false; 
    } 

    const int count = model.rowCount(); 

    if(count > 0) 
     *tags = new std::list<Tag>(count); 
    else 
     *tags = new std::list<Tag>(); 

    for(int i = 0; i < count; ++i) { 
     auto record = model.record(i); 
     Tag tag(record.value(Table::KEY_ID).toInt()); 
     (*tags)->push_back(tag); 
    } 

    return true; 
} 

它的工作原理,但列表返回大小4虽然循环的执行只有2次迭代和空的子对象(如果我只是调用它们的默认构造函数)。标签类没有复制构造函数。

+0

告诉我们什么是“标签”将有所帮助 –

回答

2

由于您将已经实例化的列表作为指向该函数的指针,因此不需要创建另一个列表。 从这个意义上说,你的问题很不明确。我建议你阅读一下关于指针,引用和函数调用的一般信息。

http://www.cplusplus.com/doc/tutorial/pointers/ http://www.cplusplus.com/doc/tutorial/functions/

更新:我还是强烈建议你提到的话题读了,因为你不知道这些基本点。 无论如何,这是什么,你可能想要做的(事件虽然我使用的引用表明,这里是指针的解决方案):

bool someFunc(std::list<Tag> **tags) { 
    // by default null the output argument 
    *tags = nullptr; 
    if (error) { 
     return false; 
    } 

    // dereference tags and assign it the address to a new instance of list<Tag> 
    *tags = new std::list<Tag>(); 
    return true 
} 


std::list<Tag> *yourList; 
if (someFunc(&yourList)) { 
    // then yourList is valid 
} else { 
    // then you had an error and yourList == nullptr 
} 

然而,这不是地道的C++。请阅读现代书籍或教程。

+0

我知道我可以将已初始化的对象的指针作为函数参数传递,但我想将列表初始化移动到函数体中。 –

+0

现在,我可以在函数参数中使用双指针,它可以工作,但列表返回大小为4,但我只传递了两个值。它压碎了我的大脑。 –

+0

谢谢!我做了初始化函数,但我非常需要了解如何编写这个变体(我只开始研究C++)。 –

1

使用参考。

bool DatabaseHandler::tags(std::list<Tag>& tags); 

std::list<Tag> tags; 
mDB->tags(tags); 

你必须改变所有的->.,当然。在函数中对参考进行的每个操作都将完成到它被调用的原始列表tags

编辑:如果你想创建函数内的列表并返回它,你有几个选项。我认为最接近的就是返回一个列表指针,如果函数失败,返回nullptr

//beware, pseudocode ahead 
std::list<Tag>* DatabaseHandler::tags() //return new list 
{ 
    if (success) 
     return new std::list<Tag>(...); //construct with whatever 
    else 
     return nullptr; //null pointer return, didn't work 
} 

std::list<Tag> tags* = mDB->tags(); 

你可以或者有它返回一个空列表,而不是,这取决于你怎么想它的工作。参考一个指针也会以同样的方式工作。

bool DatabaseHandler::tags(std::list<Tag>*&); //return true/false 

    std::list<Tag>* tags; 
    mDB->tags(tags); //tags will be set to point to a list if it worked 
+0

我需要将列表初始化为函数体。 –

+0

感谢您的回答,它的工作原理! –

相关问题