簡體   English   中英

類,錯誤:ISO C ++禁止聲明沒有類型的“任務”

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

有人可以幫我理解我的錯誤。 我是班級概念的新手,不確定我缺少什么。 我沒有在頭文件中正確聲明Task * _task嗎? 謝謝。

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如下:

#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如下:

 #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;
 }

您可能需要在MonReqServer.hh中更改#include的順序(我假設Task是在Task.hh定義的)。 但是即使那樣,在MonReqServer.hh轉發聲明Task是一個好主意,因此其他源文件(包括MonReqServer.hh知道Task是一個類。 也就是說,把class Task;放進去class Task; 在引用Task之前在MonReqServer.hh中。

關於與隱式聲明析構函數有關的錯誤:這是因為您定義了析構函數而未聲明它。 添加~MonReqServer(); 到頭文件。

似乎標題

#include "pds/service/Task.hh"

包含Task聲明的表既不顯式也不隱式包含在標頭MonReqServer.hh ,該標頭在類MonReqServer的定義中MonReqServer

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

由於編譯器不知道Task表示什么名稱,因此它也忽略了_task變量的_task

同樣,類定義不包含析構函數聲明。 但是,您在文件MonReqServer.cc定義了析構函數

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM