【问题标题】:In member function I get the error " invalid use of undefined type 'struct (name)' - forward declaration of 'struct (name)' "在成员函数中,我收到错误“无效使用未定义类型'struct (name)' - forward declaration of 'struct (name)'”
【发布时间】:2015-01-03 04:56:34
【问题描述】:

我在同一个项目中有以下文件。 如果您认为没有必要,请不要阅读所有代码块, 错误消息仅出现在 ship.cpp 中

main.cpp

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "chart.cpp"
#define N 10

using namespace std;

int main()
{
    int i,j, flagi=-3, flagj=-3;
    int test, ports_count=0, treas_count=0;

    chart ***mapp;
    mapp=new chart **[N];
    for(i=0;i<N;i++){mapp[i]=new chart *[N];}

     /*missing code initilazing chart ***mapp */

    return 0;
}

图表.cpp

#include <iostream>
#include "ship.cpp"

using namespace std;

class chart{

  bool isPort;
  int weather;
  int treasure;
  ship* shipPtr;

  public:
    chart(){isPort=false; weather=0; treasure=0; shipPtr=NULL;}

    bool getPort(){return isPort;}
    int getWeather(){return weather;}
    int getTreasure(){return treasure;}
    ship* getShip(){return shipPtr;}

    void setPort(bool port){isPort=port;}
    void setWeather(int weath){weather=weath;}
    void setTreasure(int treas){treasure=treas;}
    void setShip(ship* shp){shipPtr=shp;}
};

和 船.cpp

#include <iostream>
#define N 10

using namespace std;

class ship{
protected:
      string name;
      int maxhp, curhp, speed, curtreas, x_pos, y_pos;

public:
   friend class chart; 
   //the line above gives error message " forward declaration of 'struct chart' "

   static int shipcount;

   ship(){shipcount++;}

   string getName(){return name;}
   int getMaxhp(){return maxhp;}
   int getCurhp(){return curhp;}
   int getSpeed(){return speed;}
   int getCurtreas(){return curtreas;}
   int getX_pos(){return x_pos;}
   int getY_pos(){return y_pos;}

   bool Move(chart ***mapp){

      int x, y, blocked=0;

      for(x=x_pos-1;x<=x_pos+1;x++){
          if((x>-1) && (x<N)){
              for(y=y_pos-1;y<=y_pos+1;y++){
                   if((y>-1) && (y<N)){

/* the line below gives error message "invalid use of undefined type 'struct chart'"*/

                         if((!mapp[x][y]->getPort) && (mapp[x][y]->getShip==NULL)){
                                                   blocked++;
                         }
                   }
               }
          }
      }
      if(blocked<2){
          return false;
      }

      /* missing the rest of the body of bool Move cause it is too big */

    }
}

编译器给出以下错误信息:

"invalid use of undefined type 'struct chart' " in ship.cpp -> line 39

"forward declaration of 'struct chart' " in ship.cpp -> line 12

为什么会出现这些错误?

我知道代码可能很复杂,但我们将不胜感激。

感谢您的宝贵时间。

【问题讨论】:

    标签: c++ struct forward-declaration member-functions


    【解决方案1】:

    此代码无法编译的原因是您的ship.cpp 文件需要chart 类的定义才能使用其成员。你没有提供这个定义,导致编译器抱怨。

    由于类chart 的所有成员都在类声明中定义,您可以将chart.cpp 重命名为chart.h,并在您的ship.cpp 文件中为其添加#include

    #include <iostream>
    #include "chart.h"
    #define N 10
    ... // The rest of ship.cpp code
    

    还将main 中的名称chart.cpp 替换为chart.h

    【讨论】:

    • 我将chart.cpp重命名为chart.h,将chart.h包含在ship.cpp中,并且在main中用chart.h替换了chart.cpp,但是在尝试使用这些更改进行编译时,编译继续不停,几秒钟后它崩溃了。
    • 编译器显示消息:'在包含的文件中,从 ship.cpp:2 从 chart.h 从 chart.h:2 从 ship.cpp 从 ship.cpp:2 从 chart.h 从chart.h:2 from ship.cpp' 这些消息继续出现太多次,并且是一条消息说:'#include nested deep'
    • @thomas_p 您需要将chart.h 中的#include "ship.cpp" 替换为前向声明class ship;
    • 好吧,我从chart.h 中删除了#include "ship.cpp",而是在chart.h 中添加了class ship;,正如你所说。然后将chart.h 包含到ship.cpp 文件中,最后仅将ship.cpp 包含到main.cpp 中。在这些更改之后,代码被编译了!非常感谢您的帮助。我应该在此处发布我稍后可能遇到的任何其他问题还是打开一个新问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2014-01-01
    • 2018-06-25
    • 2013-10-14
    相关资源
    最近更新 更多