头文件类成员函数声明错误:"incomplete type name in nested name specifier"

Header file class member function declaration error: "incomplete type ' ' name in nested name specifier"

本文关键字:name incomplete type in specifier nested 文件 成员 函数 错误 声明      更新时间:2023-10-16

我刚刚将正在编写的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);

好。遵循这些规则,你应该会得到一些非常接近的东西:

  1. 将类、typedefs、#defines、模板和内联函数放入头文件中
  2. 用#ifndef/#define/#endif包装头文件,这样意外的多个包含就不会导致多个定义的符号
  3. 把你的实现放在你的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 ... }