c++ - “vector ”没有命名类型

标签 c++ vector namespaces

我是 C++ 的新手,在将 vector 声明为类变量时遇到了问题。我已经使用类似的策略让它们在我的代码的其他地方工作,但它不喜欢我的头文件。

error: ‘vector’ does not name a type
error: ‘vector’ has not been declared
error: expected ‘,’ or ‘...’ before ‘<’ token
error: ‘vector’ does not name a type

我已经评论了 GCC 指出的问题。

#ifndef HEADER_H
#define HEADER_H

#include <cstdlib>
#include <vector>
#include <string>

using std::string;

//  Class declarations

class Node {
    int id;
    string type;
public:
    Node(int, string);
    int get_id();
    string get_type();
    string print();
};

class Event {
    string name, date, time;
public:
    Event(string, string, string);
    string get_name();
    string get_date();
    string get_time();
    string print();
};

class Course {
    char id;
    std::vector<Node*> nodes[40];     // This one
public:
    Course(char, std::vector<Node*>); // This one
    char get_id();
    std::vector<Node*> get_nodes();   // & this one.
    string print();
};


class Entrant {
        int id;
        Course* course;
        string name;
    public:
        Entrant(int, char, string);
        int get_id();
        Course* get_course();
        string get_name();
        string print();
    };

    //  Function declarations

void menu_main();

void nodes_load();
void event_create();
void entrant_create();
void course_create();


#endif  /* HEADER_H */

Here's a screenshot我的 IDE 中的错误,如果这提供了更多线索。

最佳答案

从实际编译您的代码中我可以看到的唯一问题是您在 Entrant 类中使用了 Course 但您没有为 Course 定义那时。

如果您像这样在 Entrant 上方转发声明 Course:

class Course;

class Entrant { }; //class definition

然后你的代码编译,根据这个live example

关于c++ - “vector ”没有命名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336428/

相关文章:

C++:不同命名空间中的多个运算符定义

c++ - 函数模板中的类型参数

c++ - 在引用上跳过重载函数

python - 在 python 中,如何创建一个由 1 到 100 之间的数字组成的向量,每个数字重复 100 次

C++ vector 排序字符串问题

c++ - 如何将 vector 变量传递给函数?

r - 命名空间无法在 R 中卸载

PHPUnit 找不到我的 PHP 文件,命名空间问题?

c++ - Firebase Auth C++ - 持久登录

c++ - 递增引用变量不起作用