[英]Using a global vector of structs
我正在嘗試全局聲明 - 並初始化 - 結構向量。 我的代碼如下:
Struct creation (in header file vsystem.h)
struct var {
string name;
float value;
};
Variable (in source file vsystem.cpp)
#include <string>
#include <vector>
vector <var> varList;
這兩個# include <string>
和<vector>
。 我也試過
vector <var> varList ();
但這也行不通。 我的錯誤是
expected constructor, destructor, or type conversion before '<' token
另一方面,我的setVar
function 正在觸發錯誤:
Multiple markers at this line
- 'string' was not declared in this scope
- expected primary-expression before 'float'
- initializer expression list treated as compound
expression
- expected ',' or ';' before '{' token
代碼:
int setVar(string varName, float value){
// Check to see if varName already exists
varExists = false;
for (int i=0; i<varList.size(); i++){
if (varList[i].name == varName){
varExists = true;
return ERR_VAR_EXISTS;
}
}
// Good! The variable doesn't exist yet.
var tempVar ();
var.name = varName;
var.value = value;
varList.push_back(tempVar);
return 0;
}
請幫忙!
我在 Mac 10.6.7 上使用 G++ 編譯器運行 Eclipse Helios Service Release 2。
vector
在std
命名空間中。 您需要在聲明中將其限定為std::vector
:
std::vector <var> varList;
(或者,如果您真的討厭std::
,您可以使用 using 聲明, using std::vector;
。)
string
也是如此; 它需要被限定為std::string
。 C++ 標准庫中的所有名稱都在std
命名空間中,除了 (a) 宏和 (b) 舊 C 標頭中的名稱(以.h 結尾的名稱)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.