I am trying to compile a class of mail sending using sockets
However, I have that error during the compilation :
expected `)’ before ‘=’ token
At the line where I declare the constructor :
Mail(ipSMTP="serveurmail.com", port=25)
I’m suspecting that the issue is coming from the two string attributes declared before the Mail() constructor :
Here is the code of the mail.h file, containing the Mail class declaration :
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif
#include <string>
#include <iostream>
using namespace std;
class Mail
{
private :
public :
SOCKET sock;
SOCKADDR_IN sin;
char buffer[255];
int erreur;
int port;
string message;
string ipSMTP;
Mail(ipSMTP="serveurmail.com", port=25)
{
#if defined (WIN32)
WSADATA WSAData;
erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
erreur = 0;
#endif
message = "";
/* Création de la socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
/* Configuration de la connexion */
sin.sin_addr.s_addr = inet_addr(ipSMTP.c_str());
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
}
~Mail()
{
/* On ferme la socket précédemment ouverte */
closesocket(sock);
#if defined (WIN32)
WSACleanup();
#endif
}
void envoi()
{
//Instructions d'envoi d'email par la socket via le SMTP
}
};
You need to specify the types of your constructor arguments, as well as assigning your members with the passed values.
should be written as
In the above we are assigning the member variables using a constructor initializer list, you can read more about it here:
[10.6] Should my constructors use “initialization lists” or “assignment”?