我是 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/