简体   繁体   English

类,错误:ISO C ++禁止声明没有类型的“任务”

[英]Classes, error: ISO C++ forbids declaration of 'Task' with no type

Could someone please help me understand my errors. 有人可以帮我理解我的错误。 I am new to the concept of classes and not sure what I am missing. 我是班级概念的新手,不确定我缺少什么。 Did I not declare Task* _task correctly in my header file? 我没有在头文件中正确声明Task * _task吗? Thank you. 谢谢。

MonReqServer.hh:18: error: ISO C++ forbids declaration of 'Task' with no type
MonReqServer.hh:18: error: expected ';' before '*' token
MonReqServer.cc: In constructor 'Pds::MonReqServer::MonReqServer()':
MonReqServer.cc:11: error: class 'Pds::MonReqServer' does not have any field named '_task'
MonReqServer.cc:13: error: '_task' was not declared in this scope
MonReqServer.cc: At global scope:
MonReqServer.cc:16: error: definition of implicitly-declared 'virtual Pds::MonReqServer::~MonReqServer()'
MonReqServer.cc: In destructor 'virtual Pds::MonReqServer::~MonReqServer()':
MonReqServer.cc:18: error: '_task' was not declared in this scope

MonReqServer.hh is as follows: MonReqServer.hh如下:

#ifndef Pds_MonReqServer_hh
#define Pds_MonReqServer_hh

#include "pds/utility/Appliance.hh"
#include "pds/service/Routine.hh"

namespace Pds {

  class MonReqServer : public Appliance, public Routine {
  public:
    MonReqServer();
  public:
    Transition* transitions(Transition*);
  InDatagram* events     (InDatagram*);

  void routine();
 private:
  Task* _task;

 };
};

#endif

MonReqServer.cc is as follows: MonReqServer.cc如下:

 #include "pdsapp/tools/MonReqServer.hh"
 #include "pds/service/Task.hh"

#define mult_address "225.0.0.37"
#define mult_port "1100"

using namespace Pds;


 MonReqServer::MonReqServer() :
  _task(new Task(TaskObject("monlisten")))
{
  _task->call(this);
 }

 MonReqServer::~MonReqServer()
 {
  _task->destroy();
 }


Transition* MonReqServer::transitions(Transition* tr)
{
  printf("MonReqServer transition %s\n",TransitionId::name(tr->id()));
 return tr;
}

  void MonReqServer::routine()
  {

 int udp_socket_info;
 struct sockaddr_in udp_server;
 struct sockaddr addr;
 struct ip_mreq mreq;
 socklen_t fromlen;
 fromlen = sizeof addr;
 char incoming_message[100];

    udp_socket_info = socket(AF_INET, SOCK_DGRAM, 0);
    if (udp_socket_info == -1) {
    puts("Could not create socket");
    }

   memset((char*)&udp_server,0,sizeof(udp_server));
    udp_server.sin_family=AF_INET;
    udp_server.sin_port = htons( 1100 ); 
    udp_server.sin_addr.s_addr = INADDR_ANY; 

    if (bind(udp_socket_info,(struct sockaddr *)&udp_server, sizeof(udp_server)) < 0) {
  perror("bind error");
  exit (1);
      }
puts("bind");

 //join multicast group
 mreq.imr_multiaddr.s_addr=inet_addr(mult_address); 
 mreq.imr_interface.s_addr=htonl(INADDR_ANY); 

 if (setsockopt(udp_socket_info, IPPROTO_IP,IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
  perror("setsockopt");
  exit (1);
      }

    if (recvfrom(udp_socket_info, incoming_message , sizeof(incoming_message), 0, &addr, &fromlen) <0) {
    perror("recvfrom");
    }       
puts("Message received");

char tcp_ip[20];
char tcp_port[20];
sscanf( incoming_message, "%s %s", tcp_ip, tcp_port);

}

InDatagram* MonReqServer::events     (InDatagram* dg)
{
 (dg->seq.service()==TransitionId::L1Accept) {

 }
 return dg;
 }

You probably need to change the order of the #include s in MonReqServer.hh (I assume Task is defined in Task.hh ). 您可能需要在MonReqServer.hh中更改#include的顺序(我假设Task是在Task.hh定义的)。 But even then it's a good idea to forward-declare Task in MonReqServer.hh , so other source files including MonReqServer.hh know that Task is a class. 但是即使那样,在MonReqServer.hh转发声明Task是一个好主意,因此其他源文件(包括MonReqServer.hh知道Task是一个类。 Which is to say, put class Task; 也就是说,把class Task;放进去class Task; in MonReqServer.hh before Task is referenced. 在引用Task之前在MonReqServer.hh中。

About the error regarding the implicitly declare destructor: This is because you have defined the destructor without declaring it. 关于与隐式声明析构函数有关的错误:这是因为您定义了析构函数而未声明它。 Add ~MonReqServer(); 添加~MonReqServer(); to the header file. 到头文件。

It seems that header 似乎标题

#include "pds/service/Task.hh"

that contains the declaration of Task was included neither explicitly nor implicitly in header MonReqServer.hh where it is used in the definition of class MonReqServer 包含Task声明的表既不显式也不隐式包含在标头MonReqServer.hh ,该标头在类MonReqServer的定义中MonReqServer

  class MonReqServer : public Appliance, public Routine {
  public:
  //...
 private:
  Task* _task;
  ^^^^^
 };

As the compiler does not know what name Task denotes it also ignored declaration of variable _task . 由于编译器不知道Task表示什么名称,因此它也忽略了_task变量的_task

Also the class definition does not contain a destructor declaration. 同样,类定义不包含析构函数声明。 However you defined the destructor in file MonReqServer.cc 但是,您在文件MonReqServer.cc定义了析构函数

 MonReqServer::~MonReqServer()
 {
  _task->destroy();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM