0

我对 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 中错误的屏幕截图,如果提供更多线索的话。

4

3 回答 3

3

从实际编译代码中我可以看到的唯一问题是您CourseEntrant课堂上使用但此时您没有定义Course

如果您像这样转发Course上面的声明Entrant

class Course;

class Entrant { }; //class definition

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

于 2013-03-11T10:41:08.603 回答
3

你是作弊 ;-) 。您提供给我们的代码有std::vector,它可以工作,而您屏幕截图中的代码有vector哪个不起作用(编译器不知道从哪里得到它)。

解决方案:更改您的代码以使用std::vector.

于 2013-03-11T11:23:21.347 回答
0

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

于 2013-03-11T10:42:30.073 回答