c++ - why is adding try block causing compiler error -


i have working code, error handling crude @ moment - sends error string standard error , exits. throw exception , allow user deal choose typical handler.

however, having added try catch block code giving me compiler error don't understand @ all.

my code looks this:

// ------------------------------------------------------------------------- sockets::tcpsocket::tcpsocket(const char* address, const int port) {     this->address    = address;     this->port       = port;     this->buffersize = sockets::tcpsocket::def_buffer;     sockets::tcpsocket::init(); }  // ------------------------------------------------------------------------- sockets::tcpsocket::~tcpsocket() {     freeaddrinfo(this->addressinfolist);     close(this->filedes); }  // ------------------------------------------------------------------------- void sockets::tcpsocket::init() {     memset(&(this->addressinfo), 0, sizeof this->addressinfo);      addressinfo.ai_family   = af_unspec;      // either ipv4 or ipv6     addressinfo.ai_socktype = sock_stream;    // uses tcp     addressinfo.ai_flags    = ai_passive;     // accept ip      // address info     int status = getaddrinfo(         this->address,         std::to_string(this->port).c_str(),         &(this->addressinfo),         &(this->addressinfolist)     );      if (status != 0)  // error- segfault if addressinfolist not allocated     {         throw getaddrinfoexception();     }      // create socket     this->filedes = socket(         this->addressinfolist->ai_family,         this->addressinfolist->ai_socktype,         this->addressinfolist->ai_protocol     );      if (this->filedes == -1)     {         std::cerr << "create socket error: " << strerror(errno) << std::endl;         exit(exit_failure);     } }  // ------------------------------------------------------------------------- 

as see calling (private) init() function constructor. , throw getaddrinfoexception in init() function. if wrap call init() in try catch wihtin constructor works fine. when call main - expecting stack unwind - compiler error.. this:

int main()     {         using namespace std;          // test client         const char* test_addr = "www.google.com";         try         {             sockets::tcpsocket socket = sockets::tcpsocket(test_addr, 80);         }         catch (sockets::getaddrinfoexception e) { /* */ }      return 0; } 

the compiler error lists each of member functions of tcpsocket , gives message:

tcpsocket/test.cpp: in function ‘int main()’: tcpsocket/test.cpp:22:12: error: request member ‘doconnect’ in ‘socket’, of non-class type ‘int(int, int, int) throw ()’      socket.doconnect();             ^ tcpsocket/test.cpp:23:12: error: request member ‘dosend’ in ‘socket’, of non-class type ‘int(int, int, int) throw ()’      socket.dosend("get / http/1.1\r\nhost: www.redway-soft.com\r\n\r\n");             ^ tcpsocket/test.cpp:24:20: error: request member ‘dorecv’ in ‘socket’, of non-class type ‘int(int, int, int) throw ()’      cout << socket.dorecv() << endl;                     ^ tcpsocket/test.cpp:34:12: error: request member ‘restart’ in ‘socket’, of non-class type ‘int(int, int, int) throw ()’      socket.restart();             ^ tcpsocket/test.cpp:35:30: error: request member ‘dolisten’ in ‘socket’, of non-class type ‘int(int, int, int) throw ()’      string received = socket.dolisten();                               ^ 

despite searching through multiple tutorials , guides cannot find reason problem. appreciated.

edit:

the class definition looks this:

#ifndef __tcpsocket_hpp__ #define __tcpsocket_hpp__  #include <cstring> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #include <string> #include <vector> #include <iostream> #include <cerrno> #include <exception>   namespace sockets {      class getaddrinfoexception: std::exception     {         public:             virtual const char* what() const throw()             {                 return "getaddressinfo() failure";             }     };        /**     * @class tcpsocket, creates http/tcp socket. tcpsocket designed idea     * should instantiated in own thread use cases.     * @param const char* address, e.g. www.google.com     * @param int port, port defaults 80     */     class tcpsocket     {         private:              /**             * instance vars             */             struct addrinfo  addressinfo;             struct addrinfo* addressinfolist;             int              filedes;             const char*      address;             int              port;             int              buffersize;              /**             * @function init, allows socket re-assigned new address , port             */             void init();          public:              /**             * pulbic class vars             */             static const int def_buffer   = 2000;             static const int def_port     = 5556;             static const int def_timeout  = 200000;             static const int def_numtries = 5;              /**             * @constructor             * @param address, connect default null siffnifying server socket             * @param port, port connect/listen on             */             tcpsocket(const char* address = null, const int port = def_port);             /**             * @destructor             */             ~tcpsocket();              /**             * public getters             */             inline const char*            getaddress()         {return this->address;}             inline const int&             getport()            {return this->port;}             inline const struct addrinfo& getaddressinfo()     {return this->addressinfo;}             inline const int&             getfiledes()         {return this->filedes;}             inline const struct addrinfo* getaddressinfolist() {return this->addressinfolist;}              /**             * @function setbuffer, set buffersize custom size             * @param buffer, size set read in buffer             */             inline void setbuffer(int buffer) {this->buffersize = buffer;}              /**             * @function restart, cleans , re-initialises socket new params             * @param address, connect             * @param port, port connect             */             void restart(const char* address = null, const int port = def_port);              /**             * @function doconnect, connect target address             */             void doconnect();              /**             * @function dosend - sends specific message,             * @param message e.g. "get / http/1.1\r\nhost: www.google.com\r\n\r\n"             * send simple request, or html string sent             * in case of send server socket responding             * request.             */             void dosend(const char* message);              /**             * @function dorecv, gets return data server             * @param timeout, length of time wait (micros) data def = 100 000             * @param numtries, number of attempts @ reading data if none recv             * @return message has been received             */             std::string dorecv(int timeout = def_timeout, int numtries = def_numtries);              /**             * @function dolisten, listens @ given port , accepts connection             * note function blocking , implementation should             * on own thread/process, server class should             * in charge of managing individual threads , sockets.             */             std::string dolisten();               /**             * @function doclose, closes filedescripter.             */             void doclose();     }; }  #endif 

my crystal ball telling me you're using socket beyond catch block, scope until end of try block. need socket in try block.

the reason doesn't socket not declared because function, such this one linux or this one windows. inside try block, local variable shadows function.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -