使用结构的全局向量

发布于 2024-11-04 08:38:01 字数 1435 浏览 1 评论 0原文

我正在尝试全局声明并初始化一个结构向量。我的代码如下:

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;

这两个 # 都包括。我也尝试过

vector <var> varList ();

但这也不起作用。我的错误是

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

附带说明,我的 setVar 函数触发了错误:

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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-11-11 08:38:03

vector 位于 std 命名空间中。您需要在声明中将其限定为 std::vector :(

std::vector <var> varList;

或者,您可以使用 using 声明,using std::vector;,如果您真的讨厌 < code>std::。)

对于 string 类似;它需要被限定为 std::string。 C++ 标准库中的所有名称都位于 std 命名空间中,除了 (a) 宏和 (b) 旧版 C 头文件中的名称(以 结尾的名称) .h)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文