开发者

Using a global vector of structs

I am trying to globally declare – and initialize – a vector of structs. My code is the following:

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;

Both of those # include <string> and <vector>. I have also tried

vector <var> varList ();

But that doesn't work either. My error is

expected constructor, destructor, or type conversion before '<' token

On a side note, my setVar function is triggering an error:

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

Code:

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;
}

Help please!

I am running Eclipse Helios Service Release 2 with G++ compiler on Mac 10.6.7.


vector is in the std namespace. You need to qualify it as std::vector in your declaration:

std::vector <var> varList;

(Alternatively you could use a using declaration, using std::vector;, if you really hate the std::.)

Similarly for string; it needs to be qualified as std::string. All of the names in the C++ Standard Library are in the std namespace except (a) those that are macros and (b) those that are in the legacy C headers (the ones that end in .h)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜