头文件类成员函数声明错误:"incomplete type name in nested name specifier"
Header file class member function declaration error: "incomplete type ' ' name in nested name specifier"
我刚刚将正在编写的C++程序拆分为多个文件。
现在,对于每个成员函数声明,我都会得到这个错误。
我做错了什么?
3 class Office{
4 private:
5 static const int IDLE = 0, BUSY = 1;
6 const int num_tellers;
7 int students_served;
8
9 vector<double> que; // a vector que which holds the arrival times of students entering an Office
10 vector<int> tellers; // a vector to hold the status (IDLE or BUSY) of the tellers *** INITIALIZED TO SIZE tellers[num_tellers] IN CONSTRUCTOR ***
11
12
13 variate_generator<mt19937, exponential_distribution<> > serve_time; // random variable, determines the time it takes a teller to serve a student
14
15 public:
16
17 Office(double const office_mean, int const num_tellers) : num_tellers(num_tellers), tellers(vector<int>(num_tellers, IDLE)),
18 serve_time(variate_generator< mt19937, exponential_distribution<> >( mt19937(time(0)), exponential_distribution<>( 1 / office_mean))){
19 } // initialize tellers vector to size num_tellers w/ tellers[i] = IDLE, accumulated times to , and initializes serve_time random variable
37 int Office::departure_destination(Event* departure) { // returns the next destination of a student departing from an Office
38
39 if (departure->depart_from == AID) {
40 return determine_destination(.15, .15, 0, 0, .70);
41 else if (departure->depart_from == PARKING)
42 return next_destination = determine_destination(.3, 0, 0, 0, .7);
43 else if (departure->depart_from == REGISTRAR)
44 return next_destination = determine_destination(.25, 0, .1, 0, .65);
45 else if (departure->depart_from == BURSAR)
46 return next_destination = determine_destination(0, .1, .2, .1, .60);
47 else
48 return -1;
49 }
50
然后在头文件中
57 int Office::departure_destination(Event* departure);
好。遵循这些规则,你应该会得到一些非常接近的东西:
- 将类、typedefs、#defines、模板和内联函数放入头文件中
- 用#ifndef/#define/#endif包装头文件,这样意外的多个包含就不会导致多个定义的符号
- 把你的实现放在你的c++文件中,并包括这个有(1)的头
理解这一点的最简单方法是认识到头文件中没有任何内容生成实际的机器指令或数据。标头中的所有内容都是声明性的。它描述了如果用于声明变量,它将如何生成代码或数据。当您试图调用另一个文件中的某个函数、调用对象上的方法等时,您的C++文件需要这些"大纲"来理解您想要什么。
所有#define类型的指令都是文本处理。
模板实际上是一种图灵完整的语言,但在你用它们做一些东西之前,它们不会生成代码。。。然后有点像他们在生成自己的C++文件。
类声明定义了一个对象中的内容,如果您选择生成一个对象的话。
因此,一个典型的头文件(比如my_header)看起来是这样的:
#ifndef MY_HEADER
#define MY_HEADER
extern int global;
class A {
... data declarations, inline functions ...
public:
void f();
};
#endif
和C++文件:
#include "my_header"
int global; // only in ONE C file...this generates real data
void A::f() { ... generates real code to be linked to ... }
相关文章:
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- C++中"dependent name"的定义是什么?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- C++初学者:为什么我的编译器根据我的循环返回"name not found"?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- reference_wrapper导致"incomplete type is not allowed"
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 结构中的错误"Incomplete type is not allowed"
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- 嵌套类"does not name a type"
- C++ "Using Uninitialized Memory.. (variable name) "
- 在反转字符串'my.name.is'时,我得到的输出为"is@.name.my"。我不明白'@'是从哪里来的
- 模板尾部结构抬高"incomplete type in nested name specifier"
- C++错误:" 'class' does not name a type"和"invalid use of incomplete type ‘struct ...' "
- 头文件类成员函数声明错误:"incomplete type name in nested name specifier"