Reputation: 10124
As my first C++ program I decided to make a simple TCP server based on previous knowledge from C and utilizing POSIX socket API:
Therefore, I made the following header file network.h
:
#ifndef HTTP_NETWORK
#define HTTP_NETWORK
#include<string>
#include <arpa/inet.h>
//Dummy Value to be changed
#define MAXPENDING 5
class Exception {
public:
Exception(std::string message):message(message){}
std::string getMessage();
private:
std::string message;
};
class NetworkException:public Exception {};
class TCPServer{
public:
TCPServer(int port,std::string address);
~TCPServer();
void listen();
private:
int port;
//Socket file Descriptor
int servSock;
struct sockaddr_in ServAddr;
};
#endif
And afterwards I made and the network.cpp
:
#include"network.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<iostream>
#include<cstring>
#include<string>
std::string Exception::getMessage(){
return this->message;
}
TCPServer::TCPServer(int port,std::string address)
:port(port){
if ((this->servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
throw NetworkException(std::string("SOCKET Error: could not create basic socket"));
}
memset(&this->ServAddr,0,sizeof(this->ServAddr));
ServAddr.sin_family = AF_INET;
ServAddr.sin_addr.s_addr = inet_addr(address.c_str());
ServAddr.sin_port = htons(port);
if (bind(this->servSock, (struct sockaddr *) &this->ServAddr, sizeof(this->ServAddr)) < 0) {
throw NetworkException(std::string("SOCKET Error: Failed to bind a socket"));
}
if (::listen(this->servSock, MAXPENDING) < 0) {
throw NetworkException(std::string("SOCKET Error: Failed to Listen"));
}
}
void TCPServer::listen(){
struct sockaddr_in ClntAddr; /* Client address */
socklen_t clntLen= (socklen_t)sizeof(ClntAddr);
int clntSock; /* Socket descriptor for client */
//@todo Dummy Logic Depedency Inject Socket Handler
for (;;) {
if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr, &clntLen)) < 0) {
std::cout<<"Failed to fetch"<<std::endl;
}
send(clntSock, "12345\n", 6, 0);
std::cout << "Handling client %s\n" << inet_ntoa(ClntAddr.sin_addr) << std::endl;
close(clntSock);
}
}
TCPServer::~TCPServer(){
close(this->servSock);
}
But somehow I have hard time to throw an object as an exception:
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(const NetworkException&)
class NetworkException:public Exception {};
^~~~~~~~~~~~~~~~
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const NetworkException&’
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(NetworkException&&)
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘NetworkException&&’
./src/socket/network.cpp:31:84: error: no matching function for call to ‘NetworkException::NetworkException(std::__cxx11::string)’
throw NetworkException(std::string("SOCKET Error: Failed to bind a socket"));
^
In file included from ./src/socket/network.cpp:1:0:
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(const NetworkException&)
class NetworkException:public Exception {};
^~~~~~~~~~~~~~~~
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const NetworkException&’
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(NetworkException&&)
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘NetworkException&&’
./src/socket/network.cpp:35:77: error: no matching function for call to ‘NetworkException::NetworkException(std::__cxx11::string)’
throw NetworkException(std::string("SOCKET Error: Failed to Listen"));
^
In file included from ./src/socket/network.cpp:1:0:
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(const NetworkException&)
class NetworkException:public Exception {};
^~~~~~~~~~~~~~~~
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const NetworkException&’
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(NetworkException&&)
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘NetworkException&&’
By first issue is that I have hard time to extend the Exception
Class into something more Specific. Usually in other languages a generic class has been provided and I could be able to throw it as an exception. In C++ AFAIK any type can be thrownas an error hence I decided to have a class to be thrown.
But for now the plan backfires and I find myself hard to thow an anonymous Object of NetworkException
I also tried to mitigate the issue by changing the NetworkException
via:
class NetworkException:public Exception {
public:
NetworkException(std::string message){}
};
And still get errors:
In file included from ./src/main.cpp:4:0:
./src/socket/network.h: In constructor ‘NetworkException::NetworkException(std::__cxx11::string)’:
./src/socket/network.h:20:42: error: no matching function for call to ‘Exception::Exception()’
NetworkException(std::string message){}
^
./src/socket/network.h:12:5: note: candidate: Exception::Exception(std::__cxx11::string)
Exception(std::string message):message(message){}
^~~~~~~~~
./src/socket/network.h:12:5: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(const Exception&)
class Exception {
^~~~~~~~~
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(Exception&&)
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
In file included from ./src/socket/network.cpp:1:0:
./src/socket/network.h: In constructor ‘NetworkException::NetworkException(std::__cxx11::string)’:
./src/socket/network.h:20:42: error: no matching function for call to ‘Exception::Exception()’
NetworkException(std::string message){}
^
./src/socket/network.h:12:5: note: candidate: Exception::Exception(std::__cxx11::string)
Exception(std::string message):message(message){}
^~~~~~~~~
./src/socket/network.h:12:5: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(const Exception&)
class Exception {
^~~~~~~~~
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(Exception&&)
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
Upvotes: 0
Views: 46
Reputation: 20959
You need to call the constructor of base class:
class NetworkException:public Exception {
public:
NetworkException(std::string message)
: Exception(message)
{}
};
before the body of ctor of derived class is performed, all data members must be constructed. Exception
has only one ctor which takes string, and you have to call it explicitly.
Upvotes: 2