未在此范围内声明错误,c ++

时间:2015-08-21 18:38:13

标签: c++

我不确定如何解决我的错误。我如何制作是为了让我的void run()函数可以看到这个变量' intf'? intf本身的值在.cnf文件中单独声明。谢谢

我的错误如下

monreqserver.cc: In member function 'void Pds::MyXtcMonitorServer::run()':
monreqserver.cc:57: error: 'intf' was not declared in this scope

我的代码如下:

#include "pdsdata/app/XtcMonitorServer.hh"

#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

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

namespace Pds {
  class MyXtcMonitorServer : public XtcMonitorServer {
  public:
    MyXtcMonitorServer(const char* tag,
           unsigned sizeofBuffers, 
           unsigned numberofEvBuffers, 
           unsigned numberofEvQueues, const char * intf) : 
      XtcMonitorServer(tag,
           sizeofBuffers,
           numberofEvBuffers,
           numberofEvQueues) 
  {
  _init();
   }
   ~MyXtcMonitorServer() {}
  public:

  void run() {

    //////////////
   //udp socket//
  //////////////

    int udp_socket_info;
    struct sockaddr_in udp_server;

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

    udp_server.sin_addr.s_addr = inet_addr(mult_address); 
    udp_server.sin_port = htons(1100);
    udp_server.sin_family = AF_INET;

    ifreq ifr;
    ifr.ifr_addr.sa_family = AF_INET;
    strcpy(ifr.ifr_name, intf);

    if (ioctl(udp_socket_info, SIOCGIFADDR, &ifr)<0) {
    perror("SIOCGIFADDR failed");
    }

char* port = "1100";
char* ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
char* zero = "/0";

char ip_port[100];
sprintf(ip_port, "%s %s %s", ip, port, zero);

sendto(udp_socket_info , ip_port  , strlen(ip_port), 0, (struct sockaddr *)&udp_server, sizeof(udp_server));     
puts("Message Sent");

  ///////////////////////////////
 ///SETTING UP TCP CONNECTION///
///////////////////////////////

int tcp_socket_info, tcp_new_socket;
    struct sockaddr_in tcp_server, tcp_client;

    tcp_socket_info = socket(AF_INET, SOCK_STREAM, 0);
    if (tcp_socket_info == -1) {
    printf("Could not create socket");
    }

    tcp_server.sin_addr.s_addr = INADDR_ANY;
    tcp_server.sin_family = AF_INET;
    tcp_server.sin_port = htons(1100);

  int y=1;
    if(setsockopt(tcp_socket_info, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) {
  perror("set reuseaddr");
    }

    //binds socket
    if (bind(tcp_socket_info, (struct sockaddr *)&tcp_server, sizeof(tcp_server)) < 0) {
    perror("Bind error");
    }

    //listen
    listen(tcp_socket_info , 5);

    //waiting for connection
    puts("Waiting for incoming connections...");
    int c = sizeof(struct sockaddr_in);

    //accept connection loop
    tcp_new_socket = accept(tcp_socket_info, (struct sockaddr *)&tcp_client, (socklen_t*)&c);
    puts("Connection accepted");

     while(1)
  sleep(1);
    }
  private:
    void _copyDatagram(Dgram* dg, char*) {}
    void _deleteDatagram(Dgram* dg) {}
    void _requestDatagram() {}
    private:

  };
};

using namespace Pds;

  void usage(char* progname) {
      printf("Usage: %s -p <platform> -P <partition> -i <node mask> -n <numb shm buffers> -s <shm buffer size> [-q <# event queues>] [-t <tag name>] [-d] [-c] [-g <max groups>] [-h]\n", progname);
    }

   int main(int argc, char** argv) {

const unsigned NO_PLATFORM = unsigned(-1UL);
unsigned platform=NO_PLATFORM;
const char* partition = 0;
const char* tag = 0;
const char* intf = 0;
int numberOfBuffers = 0;
unsigned sizeOfBuffers = 0;
unsigned nevqueues = 1;
unsigned node =  0xffff;
unsigned nodes = 6;
bool ldist = false;

 int c;
 while ((c = getopt(argc, argv, "I:p:i:g:n:P:s:q:t:dch")) != -1) {
errno = 0;
char* endPtr;
switch (c) {
case 'p':
  platform = strtoul(optarg, &endPtr, 0);
  if (errno != 0 || endPtr == optarg) platform = NO_PLATFORM;
  break;
 case 'I':
 intf = optarg;
 break;
case 'i':
  node = strtoul(optarg, &endPtr, 0);
  break;
case 'g':
  nodes = strtoul(optarg, &endPtr, 0);
  break;
case 'n':
  sscanf(optarg, "%d", &numberOfBuffers);
  break;
case 'P':
  partition = optarg;
  break;
case 't':
  tag = optarg;
  break;
case 'q':
  nevqueues = strtoul(optarg, NULL, 0);
  break;
case 's':
  sizeOfBuffers = (unsigned) strtoul(optarg, NULL, 0);
  break;
case 'd':
  ldist = true;
  break;
case 'h':
  // help
  usage(argv[0]);
  return 0;
  break;
default:
  printf("Unrecogized parameter\n");
  usage(argv[0]);
  break;
  }
 }

  if (!numberOfBuffers || !sizeOfBuffers || platform == NO_PLATFORM || !partition || node == 0xffff) {
fprintf(stderr, "Missing parameters!\n");
usage(argv[0]);
return 1;
}

 if (numberOfBuffers<8) numberOfBuffers=8;

 if (!tag) tag=partition;

 printf("\nPartition Tag:%s\n", tag);

 MyXtcMonitorServer* apps = new MyXtcMonitorServer(tag, 
                        sizeOfBuffers, 
                        numberOfBuffers, 
                        nevqueues, intf);
 apps->distribute(ldist);

 apps->run();

 return 0;
}

和头文件包括如下:

#ifndef Pds_XtcMonitorServer_hh
#define Pds_XtcMonitorServer_hh

#include "pdsdata/app/XtcMonitorMsg.hh"

#include "pdsdata/xtc/TransitionId.hh"
#include <pthread.h>
#include <mqueue.h>
#include <queue>
#include <stack>
#include <vector>
#include <poll.h>
#include <time.h>

  namespace Pds {

 class Dgram;
 class TransitionCache;

class XtcMonitorServer {
public:
  XtcMonitorServer(const char* tag,
         unsigned sizeofBuffers, 
         unsigned numberofEvBuffers, 
         unsigned numberofEvQueues, const char * intf); 
  virtual ~XtcMonitorServer();
public:
  enum Result { Handled, Deferred };
  Result events   (Dgram* dg);
  void discover   ();
  void routine    ();
  void unlink     ();
public:
  void distribute (bool);
protected:
  int  _init             ();
private:
  void _initialize_client();
  mqd_t _openQueue       (const char* name, mq_attr&);
  void _flushQueue       (mqd_t q);
  void _flushQueue       (mqd_t q, char* m, unsigned sz);
  void _moveQueue        (mqd_t iq, mqd_t oq);
  bool _send             (Dgram*);
  void _update           (int,TransitionId::Value);
  void _clearDest        (mqd_t);
 private:
  virtual void _copyDatagram  (Dgram* dg, char*);
  virtual void _deleteDatagram(Dgram* dg);
  virtual void _requestDatagram();
 private:
  const char*     _tag;               // name of the complete shared memory segment
  unsigned        _sizeOfBuffers;     // size of each shared memory datagram buffer
  unsigned        _numberOfEvBuffers; // number of shared memory buffers for events
  unsigned        _numberOfEvQueues;  // number of message queues for events
  char*           _myShm;             // the pointer to start of shared memory
  XtcMonitorMsg   _myMsg;             // template for messages
  mqd_t           _discoveryQueue;    // message queue for clients to get 
                                    // the TCP port for initiating connections
mqd_t           _myInputEvQueue;    // message queue for returned events
mqd_t*          _myOutputEvQueue;   // message queues[nclients] for distributing events
std::vector<int> _myTrFd;           // TCP sockets to clients for distributing
                                    // transitions and detecting disconnects.
std::vector<int> _msgDest;          // last client to which the buffer was sent
TransitionCache* _transitionCache;
int             _initFd;
pollfd*         _pfd;               /* poll descriptors for:
                **   0  new client connections
                **   1  buffer returned from client
                                    **   2  events to be distributed
                **   3+ transition send/receive  */
int             _nfd;
mqd_t           _shuffleQueue;      // message queue for pre-distribution event processing
mqd_t           _requestQueue;      // message queue for buffers awaiting request completion
timespec        _tmo;
pthread_t       _discThread;        // thread for receiving new client connections
pthread_t       _taskThread;        // thread for datagram distribution
unsigned        _ievt;              // event vector
  };
};

#endif

2 个答案:

答案 0 :(得分:0)

变量intfmain()的局部变量。因此,在main()的范围之外,它是未知的。这就是您无法在班级的成员函数中访问它的原因。

三种可能的解决方案:

  • 您将变量设为全局变量(因为它似乎是适用于所有类的全局参数

  • 您将变量设置为类中的公共静态变量。然后,您可以使用类的前缀从main初始化它。

  • 或者将其定义为run()成员函数的参数(并相应地调用从main运行)。

最后一个是我选择的,但我不太了解上下文,为选择提供更客观的建议:

// in the class:
void run(const char*intf) {  // for convenience you can use the same name
...
}

// in main: 
... 
apps->run(intf);  // pass the local variable as parameter

答案 1 :(得分:0)

在函数运行中引用名称intf。但是编译器在函数

中使用之前没有看到任何声明
  void run() {
    //...
    strcpy(ifr.ifr_name, intf);
                         ^^^^^