2015-08-21 4 views
-1

Я не уверен, как решить мою ошибку. Как сделать так, чтобы моя функция void run() могла видеть эту переменную 'intf'? значение самого intf было объявлено отдельно в файле .cnf. Спасибоне объявлен в этой области ошибки, C++

Мои ошибки следующим образом

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 
+0

Что такое cnf-файл? Я не вижу объявления переменной с запуском функции. –

+1

Путь слишком много кода. Сделайте простой пример, показывающий проблему. – David

+0

Я прошу просто, если я объявляю переменную intf в XtcMonitorServer, как ее вызвать в функции запуска без ошибки «не объявлено в области видимости» – user2616248

ответ

0

Переменная intf является локальной переменной main(). Поэтому неизвестно за пределами main(). Вот почему вы не можете получить к нему доступ в функциях-членах вашего класса.

три возможных решения:

  • вы делаете переменную глобальной (как это, кажется, глобальный параметр, который применяется ко всем классам

  • вы делаете переменная публичной статической переменной в класс. Затем вы можете инициализировать его из основного, используя префикс своего класса.

  • или вы определяете его как параметр функции-члена run(), и соответственно вызываете прогон из основного объекта).

Последняя является, по которой я выбрал бы, но я не знаю достаточно о контексте, чтобы дать более объективные советы по выбору:

// 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 
0

Название intf упоминается в запуск функции. Но компилятор не видит никаких заявлений до его использования в функции

void run() { 
    //... 
    strcpy(ifr.ifr_name, intf); 
         ^^^^^ 
+0

Так как я объявляю это, прежде чем использовать strcpy? – user2616248

+0

@ user2616248 Неясно, какая переменная является членом данных класса или глобальной переменной. Если это глобальная переменная, вы должны поместить ее объявление перед определением функции в глобальное пространство имен. или в пространстве имен, где объявлена ​​функция. –

+0

@ user2616248 Или вы можете передать его в качестве аргумента в функции. В этом случае вы должны объявить соответствующий параметр функции. –

Смежные вопросы