2013-04-11 11 views
1

得到以下編譯錯誤在Visual Studio '12pointIndex無法在C從向量檢索項++

error C3867: 'std::vector<_Ty>::at': function call missing argument list; use '&std::vector<_Ty>::at' to create a pointer to member line 39 

CODE

Vector2dVector mVertices; 

/// other code 

for (int pointIndex = 0; pointIndex < points.size(); pointIndex++) { 
    mVertices.push_back(Vector2d(pointIndex * 2.0f, pointIndex * 3.0f)); 
} 

int size = mVertices.size(); 
CCPoint *pointArr = new CCPoint[size]; 
for(int i = 0; i < size; i++) { 
    Vector2d vec2 = mVertices.at[i]; //Line 39 
    //pointArr[i].x = vec2->GetX(); 
    //pointArr[i].y = vec2->GetY(); 
} 
+0

好吧,這是一個愚蠢的問題,我同意。我預計Visual Studio 2012的智能會爲此發出警告。 – asloob 2013-04-11 09:33:18

+1

我認爲這不是一個「愚蠢的問題」:我們每個人都是初學者! :)享受學習。 – 2013-04-11 09:50:06

回答

2

的問題是,你在這裏有一個錯字:

Vector2d vec2 = mVertices.at[i]; //Line 39 
          ^^ 

你應該使用()std::vector::at方法調用,而不是[]

Vector2d vec2 = mVertices.at(i); //Line 39 

另一種可以爲使用std::vector::operator[]過載(而不是at()):

Vector2d vec2 = mVertices[i]; 

的區別在於std::vector::at()確實邊界上的向量索引檢查,並且如果索引超出範圍拋出異常std::out_of_range(防止緩衝區溢出)。

相反,如果您使用std::vector::operator[],則會禁用邊界檢查。

換句話說,使用std::vector::operator[]你有更快代碼,但你不必對矢量指數運行時檢查(所以你一定要注意你的索引,以避免危險的緩衝區溢出)。 (更確切地說,在Visual Studio中,如果_SECURE_SCL設置爲1,則還有邊界檢查std::vector::operator[])。

2
Vector2d vec2 = mVertices.at(i); 
         //^^ 

你需要括號,括號沒有。 at是一個成員函數。

1

Vector2dVector::at是最有可能是一個函數,而不是數組類型的字段:

Vector2d vec2 = mVertices.at(i); //Line 39