2014-06-18 47 views
1

提前:我是C++的新手,所以請客氣一點。 ;-)C++ push_back僅將向量中的對象添加到向量中的位置0

我想幾個對象(結果)添加到載體(結果),但不知何故,它不工作的方式我想^^

更新:我改變了代碼小而表現出一些更多的代碼以獲取更多信息

//file1 
class Result{ 
public: 
    Result(string rtype, string rname, double rcosts){ 
     type = rtype; name = rname; costs = rcosts; 
    } 
private: 
    string type, name; double costs; 
}; 

//file2 
void getCosts(vector<Parts> parts){ 
    vector<Part *> p; 
    for(unsigned i = 0; i < parts.size(); i++){ 
     p.push_back(&parts[i]); 
    } 
    cout << p.at(0)->getName() << p.at(0)->getPrice << endl; //this output is correct 
    cout << p.at(0)->getName() << p.at(0)->getPrice << endl; //this output is correct 
    cout << p.at(0)->getName() << p.at(0)->getPrice << endl; //this output is correct 

    vector<Result *> r; 
    for(std::vector<Part *>::iterator ip = p.begin; ip != p.end(); ip++){ 
     addResult((*ip)->getType(), (*ip)->getName(), r, (*ip)->getPrice()); 
    } 
    sortAndPrintResults(r); 
    //after this method printed the results into a file the programm ends. so the scope shouldn't matter. (getCosts is only called once) 
} 

void addResult(string type, string name, vector<Result *> results, double costs){ 
    Result * res = new Result(type, name, costs);   
    results.push_back(res); 
    cout << res->getName() << endl; //this prints the name of every object 
} 

輸出應該如下:

abc //results.at(0) 
def //results.at(1) 
ghi //results.at(2) 

但是,相反它是:

abc //results.at(0) 
def //results.at(0) 
ghi //results.at(0) 
error: out of range. //results.at(1) 
error: out of range. //results.at(2) 

我在做什麼錯?

+0

什麼是'r'在'getCosts'? – dyp

+1

請嘗試創建一個[最小可編譯示例](http://stackoverflow.com/help/mcve)。 – dyp

+1

'vector '通過'new'加插入元素被認爲是不好的做法(因爲它容易出錯並且效率低下)。如果可能,只需使用'vector ';否則'vector >' – dyp

回答

2

問題出在您的調試中。

此代碼添加一個單一的,然後期待三個。

void addResult(string type, string name, vector<Result *> results, double costs){ 
    results.push_back(new Result(type, name, costs)); 
    cout << results.at(0)->getName() << endl; 
    cout << results.at(1)->getName() << endl; 
    cout << results.at(2)->getName() << endl; 
} 

您想在輸出前撥打addResult 3次。

在這種情況下,你希望把你的for循環後getCosts

void getCosts(vector<Parts *> p){ 
    for(std::vector<Part *>::iterator ip = p.begin; ip != p.end(); ip++){ 
     addResult((*ip)->getType(), (*ip)->getName(), r, (*ip)->getPrice()); 
    } 
    //Check results here. 
} 

編輯: 作爲CaptainObvlious提到的,你還按值傳遞的載體導入addResult功能。

添加按值意味着vector<Result *>是在函數內本地創建的,並且不回連接到您傳遞的r變量(因此當您嘗試r.at(0),沒有什麼內部)

定影這是相當簡單的,在功能參數結果鏈接到您的r向量,則需要通過引用,這是與「&」前面加上類型簡單,它傳遞

void addResult(string type, string name, vector<Result *>& results, double costs) 

已閱讀by-value vs by-reference

+0

是的,第一件事情是有道理的^^但是當我把'cout'放在循環後面時,即使results.at(0)也會導致超出範圍的錯誤。 –

+1

@max這是因爲你正在通過值而不是通過引用或指針傳遞向量。當你從'getCosts'返回時,向量'p'被銷燬,修改不會反映到調用它的函數中。試試'void getCosts(vector &p)'。如果實際上是OP的問題,@Jono可以自由地將其納入您的答案。 –

+1

@jono:它的工作!非常感謝你!和其他人一樣,當然! –

0

我注意到你的代碼的一些錯誤,請嘗試以下方法:

//file1 
class Result{ 
public: 
    Result(string rtype, string rname, double rcosts){ 
     type = rtype; name = rname; costs = rcosts; //changed to actually assign to member 
    } 
private: 
    string type, name; double costs; 
}; 

//file2 - you did not show where you got the 'r' parameter from 
void getCosts(vector<Parts *> p){ 
    for(std::vector<Part *>::iterator ip = p.begin; ip != p.end(); ip++){ 
     addResult((*ip)->getType(), (*ip)->getName(), r, (*ip)->getPrice()); 
    } 
} 

void addResult(string type, string name, vector<Result *> & results, double costs){ 
    Result * res = new Result(type, name, costs); 
    results.push_back(res); 
    cout << res->getName() << endl; 
} 
+0

看到我的更新。 'cout'打印每個對象的名稱。 –