Hej.
Jeg har en konstruktor i en klasse som initialisere nogle variabler. Men under compilingsporcessen (med MinGW), kommer der en mystisk fejl om at jeg kalder en anden klasses konstruktor forkert, på trods af at jeg ikke selv laver et kald.
Headerfilen til klassen:
#ifndef FTP_H
#define FTP_H
#include <vector>
#include "winsock2.h"
#include "log.h"
#include "sock.h"
struct connInfo
{
std::string IP;
unsigned int port;
};
class FTP
{
public:
FTP(Log* log, std::string host, unsigned int port = 21, int timeout = 30);
~FTP();
void connectToHost(std::string& wlcMsg);
bool login(std::string user = "anonymous", std::string passwd = "password");
void test();
private:
Sock m_piSock;
void sendCmd(const std::string&);
int recvCmd(std::string&);
connInfo getConnectionInfo(const std::string& pasvCom);
std::string m_host;
unsigned int m_port;
int m_timeout;
Log* m_logger;
};
#endif
Implementeringen af funktioner:
#include <string>
#include "sock.h"
#include "ftp.h"
#include "ftperror.h"
#include "tcperror.h"
#include "log.h"
using namespace std;
FTP::FTP(Log* log, string host, unsigned int port, int timeout)
: m_host(host), m_port(port), m_timeout(timeout), m_logger(log)
{}
FTP::~FTP()
{
// descructer
// maby some content goes here
}
void FTP::connectToHost(string& wlcMsg)
{
m_logger->setStatus("Connecting to server "+ m_host);
struct svr_info serverInfo;
try {
m_piSock = Sock(m_host, m_port, m_timeout);
m_piSock.conn();
m_logger->setStatus("Connected to server, waiting for welcome message");
m_piSock.read(wlcMsg);
} catch (tcp_error &e) {
throw ftp_error(e);
}
}
bool FTP::login(string user, string passwd)
{
string buffer;
sendCmd("USER "+ user);
int num = recvCmd(buffer);
if (num == 331) {
sendCmd("PASS "+ passwd);
num = recvCmd(buffer);
if (num == 230)
return true;
else
return false;
} else
return false;
}
// returns the command number, and put the whole command in the string buffer
int FTP::recvCmd(string& buffer)
{
int cmdNum = 0;
try {
m_piSock.read(buffer);
buffer.erase(buffer.end() - 2, buffer.end());
m_logger->setResponse(buffer);
cmdNum = (int)buffer.substr(0, 3).c_str();
} catch (tcp_error &e) {
throw ftp_error(e);
}
return cmdNum;
}
void FTP::sendCmd(const string& buffer)
{
try {
string buf = buffer+ "\\r\\n";
m_piSock.write(buf);
m_logger->setCommand(buffer);
} catch (tcp_error &e) {
throw ftp_error(e);
}
}
bool markStart(char c)
{
if (c == '(')
return true;
else
return false;
}
bool markEnd(char c)
{
if (c == ')')
return true;
else
return false;
}
bool notComma(char c)
{
if (c == ',')
return false;
else
return true;
}
// return a struct with connection data to server DTP
connInfo FTP::getConnectionInfo(const string& pasvCom)
{
typedef string::const_iterator constIter;
// find the (...) part of the string
constIter i = find_if(pasvCom.begin(), pasvCom.end(), markStart);
constIter j = find_if(pasvCom.begin(), pasvCom.end(), markEnd);
string IPSep = string(++i, j);
// put all values into vector -> 1,2,3,4,5,6 = ['1', '2'...]
vector<string> output;
string tempStr = "";
for (constIter h = IPSep.begin(); h != IPSep.end(); ++h) {
if (*h == ',' && tempStr != "") {
output.push_back(tempStr);
tempStr = "";
} else
tempStr += *h;
}
// put it all togheter to an IP and a port
connInfo info;
info.IP = output[0] +"."+ output[1] +"."+ output[2] +"."+ output[3];
info.port = (unsigned int)output[4].c_str() * 256 + (unsigned int)output[5].c_str();
return info;
}
void FTP::test()
{
// set the server to ASCII mode
string buffer = "";
sendCmd("TYPE A");
recvCmd(buffer);
buffer = "";
sendCmd("PASV");
recvCmd(buffer);
connInfo info = getConnectionInfo(buffer);
throw ftp_error(info.IP +":port");
}
Mystisk output fra MinGw:
ftp.cpp: In constructor `FTP::FTP(Log*, std::string, unsigned int, int)':
ftp.cpp:13: error: no matching function for call to `Sock::Sock()'
sock.h:16: note: candidates are: Sock::Sock(const Sock&)
sock.h:18: note: Sock::Sock(std::string, unsigned int, int)
mingw32-make[1]: *** [release\\ftp.o] Error 1