'vector'不命名类型

'vector' does not name a type

本文关键字:类型 vector      更新时间:2023-10-16

我对c++相当陌生,&我有一个问题,声明向量作为一个类变量。我使用类似的策略让它们在代码的其他地方工作,但它不喜欢我的头文件。

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 */

下面是我的IDE错误的截图,如果这给了更多的线索。

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

如果在Entrant之上转发声明Course,如下所示:

class Course;
class Entrant { }; //class definition

然后你的代码编译,根据这个活的例子

你在作弊;-)。你给我们的代码有std::vector,可以工作,而你的截图中的代码有vector,不能工作(编译器不知道从哪里得到它)。

解决方案:将代码更改为使用std::vector

安装了吗?也许这对你有帮助http://ubuntuforums.org/showthread.php?t=1261897

相关文章: