c++编程中,类的声明文件和实现文件为什么都要包含标准库?
答案:2 悬赏:0 手机版
解决时间 2021-04-08 03:20
- 提问者网友:皆是孤独
- 2021-04-07 16:32
在类的定义文件中写了头文件,如#include .在类的实现中为什么还要包含#include 这个头文件呢?
最佳答案
- 五星知识达人网友:封刀令
- 2021-04-07 16:44
你可以试一下,实现文件不含 #include 。
iostream -- 输入输出流。
通常 因为是分开的2个文件,分开编译时,如果 里面 遇到 与输入输出 有关 函数 时, 需要 函数原型, 这时需要 头文件。 一般 标准头文件 有 宏保护 指令, 重复包含了, 它会 只包含一次,忽略 多余的 #include。
所以 , 写多了 不要紧, 写少了 不行。
iostream -- 输入输出流。
通常 因为是分开的2个文件,分开编译时,如果 里面 遇到 与输入输出 有关 函数 时, 需要 函数原型, 这时需要 头文件。 一般 标准头文件 有 宏保护 指令, 重复包含了, 它会 只包含一次,忽略 多余的 #include。
所以 , 写多了 不要紧, 写少了 不行。
全部回答
- 1楼网友:雪起风沙痕
- 2021-04-07 17:51
安装psdk就可以用了,之前有写过一个类在mfc里面调用,并能成功与蓝牙手机通信,只不过是非标准的蓝牙协议。代码如下:
=====================h头文件========================
#ifndef ns_bth
#include "ws2bth.h" //如果没有定义ns_bth宏,则将psdk头文件包含进来
#endif
#ifndef bth_addr
typedef ulonglong bth_addr;
#endif
#ifndef safe_delete
#define safe_delete(pobj) {if(pobj) delete pobj; pobj = null;}
#endif
#ifndef safe_deleterg
#define safe_deleterg(pobj) {if(pobj) delete [] pobj; pobj = null;}
#endif
#define receive_over_command 0x00001000
typedef struct _tag_bthdev_info
{
bth_addr btaddr;
tchar szaddr[32];
tchar szname[128];
_tag_bthdev_info()
{
memset(this, 0, sizeof(this));
}
}
bthdev_info;
class cggbluetooth
{
public:
cggbluetooth();
~cggbluetooth();
bool startsearchbthdev(in dword dwcontrolflags);
bool getnextbthdev(
in out bthdev_info *pcbthdevinfo,
in dword dwcontrolflags = lup_return_name | lup_return_addr | lup_flushcache
);
bool endsearchbthdev();
//socket api
bool create(int af = af_bth, int type = sock_stream, int protocol = bthproto_rfcomm);
bool close();
//if no error occurs, this function returns zero. if an error occurs, it returns socket_error
int bind(bth_addr sktaddr = 0, ulong port = bt_port_any);
int listen(in int backlog);
int getsockname(bth_addr *psktaddr, ulong *port);
int setbthservice(tchar *lpservicename);
int accept(cggbluetooth *pcbth);
int connect(bth_addr sktaddr, ulong port, int nmsecond = -1);
int send(lpvoid lpdata, int len, int flags = 0);
int recv(lpvoid lpdata, int len, int flags = 0);
int sendall(lpvoid lpdata, int len, int flags = 0);
int recvall(lpvoid lpdata, int len, int flags = 0);
private:
void hexmac2string(bth_addr dw64addr, tchar *pstrmac);
private:
handle m_hlookup;
socket m_hsocket;
sockaddr_bth m_sktaddr;
};
==============cpp文件================
#include "stdafx.h"
#include "ggbluetooth.h"
//#include "tchar.h"
cggbluetooth::cggbluetooth()
: m_hlookup(null)
, m_hsocket(null)
{
memset(&m_sktaddr, 0, sizeof(m_sktaddr));
}
cggbluetooth::~cggbluetooth()
{
endsearchbthdev();
close();
}
void cggbluetooth::hexmac2string(bth_addr dw64addr, tchar *pstrmac)
{
byte *pbaddr = (byte*)&dw64addr;
_stprintf(
pstrmac, _t("%02x:%02x:%02x:%02x:%02x:%02x"),
pbaddr[5], pbaddr[4], pbaddr[3],
pbaddr[2], pbaddr[1], pbaddr[0]
);
}
bool cggbluetooth::startsearchbthdev(in dword dwcontrolflags)
{
wsaqueryset wsaq;
zeromemory(&wsaq, sizeof(wsaq));
wsaq.dwsize = sizeof(wsaq);
wsaq.dwnamespace = ns_bth;
wsaq.lpcsabuffer = null;
return wsalookupservicebegin(&wsaq, dwcontrolflags, &m_hlookup) == error_success ? true : false;
}
bool cggbluetooth::getnextbthdev(
in out bthdev_info *pcbthdevinfo,
in dword dwcontrolflags
)
{
if (!m_hlookup || !pcbthdevinfo)
{
return false;
}
memset(pcbthdevinfo->szaddr, 0, sizeof(pcbthdevinfo->szaddr));
memset(pcbthdevinfo->szname, 0, sizeof(pcbthdevinfo->szname));
union
{
char buf[5000];
double __unused; // ensure proper alignment
};
lpwsaqueryset pwsaresults = (lpwsaqueryset)buf;
dword dwsize = sizeof(buf);
int nresult;
zeromemory(pwsaresults, sizeof(wsaqueryset));
pwsaresults->dwsize = sizeof(wsaqueryset);
pwsaresults->dwnamespace = ns_bth;
pwsaresults->lpblob = null;
nresult = wsalookupservicenext (m_hlookup, dwcontrolflags, &dwsize, pwsaresults);
if (nresult == error_success)
{
pcbthdevinfo->btaddr = ((sockaddr_bth *)pwsaresults->lpcsabuffer->remoteaddr.lpsockaddr)->btaddr;
bool bhavename = pwsaresults->lpszserviceinstancename && *(pwsaresults->lpszserviceinstancename);
if (bhavename)
{
hexmac2string(pcbthdevinfo->btaddr, pcbthdevinfo->szaddr);
_tcscpy(pcbthdevinfo->szname, pwsaresults->lpszserviceinstancename);
}
return true;
}
return false;
}
bool cggbluetooth::endsearchbthdev()
{
if (m_hlookup)
{
wsalookupserviceend(m_hlookup);
m_hlookup = null;
return true;
}
return false;
}
///===============================
bool cggbluetooth::create(int af, int type, int protocol)
{
if (m_hsocket)
{
return false;
}
m_hsocket = socket(af, type, protocol);
m_sktaddr.addressfamily = af;
m_sktaddr.serviceclassid = guid_null;
return m_hsocket == invalid_socket ? false : true;
}
bool cggbluetooth::close()
{
if (m_hsocket)
{
closesocket(m_hsocket);
return true;
}
return false;
}
int cggbluetooth::bind(bth_addr sktaddr, ulong port)
{
m_sktaddr.btaddr = sktaddr;
m_sktaddr.port = port;
return bind(m_hsocket, (sockaddr *)&m_sktaddr, sizeof(m_sktaddr));
}
int cggbluetooth::listen(int backlog)
{
return listen(m_hsocket, backlog);
}
int cggbluetooth::getsockname(bth_addr *psktaddr, ulong *port)
{
int nlen = sizeof(m_sktaddr);
int nresult = getsockname(m_hsocket, (sockaddr *)&m_sktaddr, &nlen);
if (nresult == 0)
{
*psktaddr = m_sktaddr.btaddr;
*port = m_sktaddr.port;
}
return nresult;
}
guid obexfiletransferserviceclass_uuid_ex = {0x00001106, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
int cggbluetooth::setbthservice(tchar * lpservicename)
{
csaddr_info sockinfo;
sockinfo.iprotocol = bthproto_rfcomm;
sockinfo.isockettype = sock_stream;
sockinfo.localaddr.lpsockaddr = (sockaddr *)&m_sktaddr;
sockinfo.localaddr.isockaddrlength = sizeof(m_sktaddr);
sockinfo.remoteaddr.lpsockaddr = (sockaddr *)&m_sktaddr;
sockinfo.remoteaddr.isockaddrlength = sizeof(m_sktaddr);
wsaqueryset svcinfo = {0};
svcinfo.dwsize = sizeof(svcinfo);
svcinfo.dwnamespace = ns_bth;
svcinfo.lpszserviceinstancename = lpservicename;
svcinfo.lpserviceclassid = &obexfiletransferserviceclass_uuid_ex;
svcinfo.dwnumberofcsaddrs = 1;
svcinfo.lpcsabuffer = &sockinfo;
return wsasetservice(&svcinfo, rnrservice_register, 0);
}
int cggbluetooth::accept(cggbluetooth *pcbth)
{
sockaddr_bth ca;
int size = sizeof(ca);
pcbth->m_hsocket = accept (m_hsocket, (sockaddr *)&ca, &size);
pcbth->m_sktaddr = ca;
return pcbth->m_hsocket == invalid_socket ? socket_error : 0;
}
int cggbluetooth::connect(bth_addr sktaddr, ulong port, int nmsecond)
{
sockaddr_bth sa = {0};
sa.addressfamily = af_bth;
sa.btaddr = sktaddr;
sa.port = port;
if (nmsecond == -1)
{
return connect(m_hsocket, (lpsockaddr)&sa, sizeof(sockaddr_bth));
}
ulong non_blocking = 1;
ulong blocking = 0;
int nresult = ioctlsocket(m_hsocket, fionbio, &non_blocking);
if (nresult == socket_error)
{
return nresult;
}
nresult = socket_error;
if (connect(m_hsocket, (lpsockaddr)&sa, sizeof(sockaddr_bth)) == socket_error)
{
struct timeval tv;
fd_set writefds;
// 设置连接超时时间
tv.tv_sec = nmsecond / 1000; // 秒数
tv.tv_usec = nmsecond % 1000; // 毫秒
fd_zero(&writefds);
fd_set(m_hsocket, &writefds);
nresult = select((int)m_hsocket + 1, null, &writefds, null, &tv);
if(nresult > 0)
{
if(fd_isset(m_hsocket, &writefds))
{
int error = 0;
int len = sizeof(error);
//下面的一句一定要,主要针对防火墙
if(!(getsockopt(m_hsocket, sol_socket, so_error, (char *)&error, &len) != 0 || error != 0))
{
nresult = 0;
}
}
}
else if (nresult == 0)
{
nresult = -2;
}
}
if (ioctlsocket(m_hsocket, fionbio, &blocking) == socket_error)
{
nresult = socket_error;
}
return nresult;
}
int cggbluetooth::send(lpvoid lpdata, int len, int flags)
{
return send(m_hsocket, (char *)lpdata, len, flags);
}
int cggbluetooth::recv(lpvoid lpdata, int len, int flags)
{
return recv(m_hsocket, (char *)lpdata, len, flags);
}
int cggbluetooth::sendall(lpvoid lpdata, int len, int flags)
{
int ncount = send(m_hsocket, (char *)lpdata, len, flags);
if (ncount == socket_error && wsagetlasterror() != wsaewouldblock)
{
return socket_error;
}
int ncommand = 0;
ncount = recv(m_hsocket, (char *)&ncommand, 4, flags);
if (ncount != 4 || ncommand != receive_over_command)
{
return socket_error;
}
return error_success;
}
int cggbluetooth::recvall(lpvoid lpdata, int len, int flags)
{
int ncount = -1, ncurrecv = 0, nmaxread = 32 * 1024;
while (ncurrecv < len)
{
if (len - ncurrecv < nmaxread)
{
nmaxread = len - ncurrecv;
}
ncount = recv(m_hsocket, (char *)lpdata + ncurrecv, nmaxread, flags);
if (ncount == socket_error)
{
if (wsagetlasterror() == wsaewouldblock)
{
sleep(1);
continue;
}
else
{
return socket_error;
}
}
ncurrecv += ncount;
}
int ncommand = receive_over_command;
ncount = send(m_hsocket, (char *)&ncommand, 4, flags);
if (ncount == socket_error && wsagetlasterror() != wsaewouldblock)
{
return socket_error;
}
return error_success;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯