這是我的情況:C++使用輸入參數作爲輸出
void Database::execute(string query, Table *outTable) {
//some code
if (isSelect) {
outTable = new Table(rowCount, columnCount);
}
// some code
}
我的想法是,該函數執行執行對數據庫的查詢。如果查詢產生一個表(例如select),我分配outTable。 無論如何,在執行此函數後,當我嘗試在調用函數中使用outTable執行某些操作時,會出現段錯誤。我已經檢查過該函數用new命令執行該行。 這裏有什麼問題?
以下是我在調用函數做:
Database d = new Database("mydb");
Table *t;
d.execute("SELECT * FROM TABLE1", t);
int n = t->rowCount(); //here I get the segfault
通過參考?或'* outTable =表(...);'? – 2013-12-08 19:39:36
@ H2CO3我在關於函數調用的問題中添加了一些內容。請檢查一下。 –
@the_cadyman然後第二個版本不適用,並且您需要通過引用傳遞。哦,不要等,首先你應該閱讀一個初學C++教程。 – 2013-12-08 19:42:46