'vector'没有命名类型

时间:2013-03-11 10:36:34

标签: c++ vector namespaces

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

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

3 个答案:

答案 0 :(得分:3)

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

如果您在Course之上转发声明Entrant,请执行以下操作:

class Course;

class Entrant { }; //class definition

然后根据此live example

编译您的代码

答案 1 :(得分:3)

你在作弊;-)。您提供给我们的代码有std::vector,但是截图中的代码vector不起作用(编译器不知道从哪里获取)。

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

答案 2 :(得分:0)

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