【问题标题】:error: ‘Class’ was not declared in this scope错误:未在此范围内声明“类”
【发布时间】:2012-06-15 15:48:16
【问题描述】:

我想问一下,如何在另一个类中定义类。在下面的代码中。我尝试以 #define "CCompField.h" 的方式定义它,但它不起作用。 :(。 我认为这是一个很常见的编程问题,可能在网上解决了 100000 次,但我不知道如何找到它。感谢您的帮助。

#ifndef CNEWGAME_H
#define CNEWGAME_H

class CNewGame{
    public:
               CNewGame();
              ~CNewGame();

        void  BeginnerGame();
        void  IntermediateGame();
        void  AdviceGame();
        void  HowToPlay();
        void  NetGame( int mode );


        int MoveInMenu();

    protected:
        void  Intro();
        void  Animation ();
        void  Menu(int);
         int  MoveInNetMenu();
        void  NetMenu(int);

        void  HeadOfGame();
template <class T> void BodyOfGame(CCompField & b, T & a);
        void  FooterOfGame();
};

#endif

它会出现以下错误。

In file included from src/CNewGame.cpp:12:0:
src/CNewGame.h:37:36: error: ‘CCompField’ was not declared in this scope
src/CNewGame.h:37:45: error: ‘b’ was not declared in this scope
src/CNewGame.h:37:50: error: expected primary-expression before ‘&’ token
src/CNewGame.h:37:52: error: ‘a’ was not declared in this scope
src/CNewGame.h:37:53: error: variable or field ‘BodyOfGame’ declared void

【问题讨论】:

  • 有什么理由你有两次头部保护?
  • 不,我没有两次。我在复制过程中只犯了错误
  • 太棒了。如果答案与以下答案之一相同,请选择它作为正确答案。如果没有,请将正确答案发布为您的问题的答案,然后选择它作为正确答案。
  • 答案是我只包含了 CCompField 类
  • 作为一个新用户,我无法在 8 小时内回答我自己的问题。

标签: c++ class templates declaration


【解决方案1】:

您需要包含它而不是 #define "CCompField.h"

#include "CCompField.h"

你还有一双额外的

#ifndef CNEWGAME_H
#define CNEWGAME_H  

但只有一个关闭#endif。你不需要第二对#ifndef/#define

【讨论】:

  • 当然我试过 #include "CCompField.h" / 我在创建帖子时犯了错误:S/
  • 那么您必须将 CCompFiled.h 与其他文件放在同一目录中(或至少在编译器使用的包含路径上)
  • 我发现解决方案我试图只包含 CCompField 但它需要包含 #include "CField.h" #include "CCompField.h" #include "CCommonField.h" 因为它是他们。我很感激你的回答。抱歉有点混乱。我是新来的。
【解决方案2】:

我尝试以 #define "CCompField.h" 的方式定义它,但它不起作用。 :(

您需要找出定义CCompField 的头文件以及来自CNewGame.h#include(不是#define)。

【讨论】:

  • 我发现解决方案我试图只包含 CCompField 但它需要包含 #include "CField.h" #include "CCompField.h" #include "CCommonField.h" 因为它是他们。我很感激你的回答。抱歉有点混乱。我是新来的。
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2015-06-23
  • 2010-10-02
相关资源
最近更新 更多