2017-02-17 58 views
0

具有以下代碼的令牌:''''或'...'在''之前。''在構造

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)) ===| 

看起來像'uns長'部分刺激了編譯器。在網上找不到任何東西。這對我來說真的很奇怪..我錯過了什麼?

+0

添加''#包括在文件的頂部。你需要它用於'uint16_t'。 –

+0

@RSahu看起來並不像它有幫助..我有很多很多很多的類,它們都是uintx_t類型的鍵入,沒有包含cstdint,沒有產生錯誤。 –

+0

不過,試試吧。它不會傷害。 –

回答

2

s_addr被定義爲在winsock2.h宏(或由winsock2.h包含的頭inaddr.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); 
+0

不錯,謝謝:) –

相關問題