拥有以下代码:
SockAddrIn.h
#pragma once
#include <winsock2.h>
#include <string>
namespace networking
{
class SockAddrIn
{
public:
SockAddrIn() {}
SockAddrIn(const std::string& ip, uint16_t port);
SockAddrIn(uint16_t sin_family, unsigned long s_addr, uint16_t port);
SockAddrIn(struct sockaddr_in sin)
: m_sin(sin)
{}
virtual ~SockAddrIn() {}
operator struct sockaddr_in();
operator struct sockaddr_in*();
operator struct sockaddr*();
private:
struct sockaddr_in m_sin;
};
}
SockAddrIn.cpp
#include "networking/SockAddrIn.h"
#include <winsock2.h>
using namespace networking;
SockAddrIn::SockAddrIn(
const std::string& ip,
uint16_t port)
: SockAddrIn(AF_INET, inet_addr(ip.c_str()), htons(port))
{}
SockAddrIn::SockAddrIn(
uint16_t sin_family,
unsigned long s_addr,
uint16_t port)
{
m_sin.sin_family = sin_family;
m_sin.sin_addr.s_addr = s_addr;
m_sin.sin_port = port;
}
SockAddrIn::operator struct sockaddr_in()
{
return ( m_sin );
}
SockAddrIn::operator struct sockaddr_in*()
{
return ( &m_sin );
}
SockAddrIn::operator struct sockaddr*()
{
return ( (struct sockaddr*)&m_sin );
}
生成以下错误(第15行是第3个构造函数):
include\networking\SockAddrIn.h|15|error: expected ',' or '...' before '.' token|
src\networking\SockAddrIn.cpp||In constructor 'networking::SockAddrIn::SockAddrIn(const string&, uint16_t)':|
hi_client\src\networking\SockAddrIn.cpp|11|error: no matching function for call to 'networking::SockAddrIn::SockAddrIn(int, long unsigned int, u_short)'|
hi_client\src\networking\SockAddrIn.cpp|11|note: candidates are:|
include\networking\SockAddrIn.h|16|note: networking::SockAddrIn::SockAddrIn(sockaddr_in)|
include\networking\SockAddrIn.h|16|note: candidate expects 1 argument, 3 provided|
include\networking\SockAddrIn.h|15|note: networking::SockAddrIn::SockAddrIn(uint16_t, long unsigned int)|
include\networking\SockAddrIn.h|15|note: candidate expects 2 arguments, 3 provided|
include\networking\SockAddrIn.h|14|note: networking::SockAddrIn::SockAddrIn(const string&, uint16_t)|
include\networking\SockAddrIn.h|14|note: candidate expects 2 arguments, 3 provided|
include\networking\SockAddrIn.h|13|note: networking::SockAddrIn::SockAddrIn()|
include\networking\SockAddrIn.h|13|note: candidate expects 0 arguments, 3 provided|
include\networking\SockAddrIn.h|10|note: constexpr networking::SockAddrIn::SockAddrIn(const networking::SockAddrIn&)|
include\networking\SockAddrIn.h|10|note: candidate expects 1 argument, 3 provided|
src\networking\SockAddrIn.cpp|16|error: expected ',' or '...' before '.' token|
src\networking\SockAddrIn.cpp||In constructor 'networking::SockAddrIn::SockAddrIn(uint16_t, long unsigned int)':|
src\networking\SockAddrIn.cpp|20|error: request for member 'S_addr' in 'S_un', which is of non-class type 'long unsigned int'|
src\networking\SockAddrIn.cpp|21|error: 'port' was not declared in this scope|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
看起来像是关于&#39; unsigned long&#39;部分激怒了编译器..无法在网上找到任何东西。这对我来说真的很奇怪..我错过了什么?
答案 0 :(得分:2)
s_addr
被定义为winsock2.h
中的宏(或inaddr.h
包含的标题winsock2.h
,确切地说)。像这样:
#define s_addr S_un.S_addr /* can be used for most tcp & ip code */
这会将你的构造函数声明变成废话:
SockAddrIn(uint16_t sin_family, unsigned long s_addr, uint16_t port);
// becomes
SockAddrIn(uint16_t sin_family, unsigned long S_un.S_addr, uint16_t port);