[英]C++ forward declare inline function in namespace problem
我正在尝试在命名空间中向前声明我的函数。 但是我得到一个错误。 让我先展示我的.h 和.cpp 文件。
Header:
namespace DeviceList
{
int GetIDFromType(NNBSSString type);
NNBSSString GetTypeFromID(int id);
void CNNBSSDeviceListAddDevice(NNBSSString& DeviceName, NNBSSString& Address, int DeviceType,
__STRING__ DeviceNetName, __STRING__ DevicePath);
void CNNBSSDeviceListRemoveSelected();
void CNNBSSDeviceListRemoveCache(int index);
inline void CNNBSSDeviceListAddressConnectionRespond(NNBSSString& deviceAddress, bool OK, NNBSSThread* thread);
void CNNBSSDeviceListUpdate();
}
和源文件:
namespace DeviceList
{
int GetIDFromType(NNBSSString type)
{
int m_id;
if (type == _("USB Camera"))
{
m_id = NNBSS_EVT_DEVICETYPE_USBCAM;
}
else if (type == _("IP Camera"))
{
m_id = NNBSS_EVT_DEVICETYPE_IPCAM;
}
else if (type == _("Microphone"))
{
m_id = NNBSS_EVT_DEVICETYPE_MICROPHONE;
}
else if (type == _("DVR"))
{
m_id = NNBSS_EVT_DEVICETYPE_DVR;
}
else if (type == _("NVR"))
{
m_id = NNBSS_EVT_DEVICETYPE_NVR;
}
else
{
m_id = -100;
NNBSSErrorShow("Given DeviceType was wrong while getting int from NNBSSString!", 100);
}
return m_id;
}
NNBSSString GetTypeFromID(int id)
{
// Has to be given by using GetIDFromEnum
NNBSSString m_type;
switch (id)
{
case NNBSS_EVT_DEVICETYPE_USBCAM:
m_type = _("USB Camera");
break;
case NNBSS_EVT_DEVICETYPE_IPCAM:
m_type = _("IP Camera");
break;
case NNBSS_EVT_DEVICETYPE_MICROPHONE:
m_type = _("Microphone");
break;
case NNBSS_EVT_DEVICETYPE_DVR:
m_type = _("DVR");
break;
case NNBSS_EVT_DEVICETYPE_NVR:
m_type = _("NVR");
break;
default:
NNBSSErrorShow("Given DeviceType was wrong while getting NNBSSString from int!", 100);
break;
}
return m_type;
}
void CNNBSSDeviceListAddDevice(NNBSSString& DeviceName, NNBSSString& Address, int DeviceType,
__STRING__ DeviceNetName = __STRING__(), __STRING__ DevicePath = __STRING__())
{
SCNNBSSDeviceParameters params;
params.DeviceName = DeviceName;
params.Address = Address;
params.DeviceType = DeviceType;
params.DeviceNetName = DeviceNetName;
params.DevicePath = DevicePath;
SCNNBSSDeviceParametersList.emplace_back(params);
{
CNNBSSHardwareCheckCameraConnection* p_CNNBSSHardwareCheckCameraConnection =
new CNNBSSHardwareCheckCameraConnection(Address);
p_CNNBSSHardwareCheckCameraConnection->Run();
}
// Update all pages that have Device List active
for (int c = 0; c < (int)m_NNBSSContentPanelList.size(); c++)
{
int pageID = c - 1;
pageID < 0 ? pageID = 0 : pageID = c - 1;
if (CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_IsCreated)
{
CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_RecreateList();
}
}
}
void CNNBSSDeviceListRemoveSelected()
{
if (CNNBSSDeviceListAddressHandle(CNNBSSControlPanelAddressHandle()->_GetCurrentContentPanel())->_IsCreated)
{
CNNBSSDeviceListAddressHandle(CNNBSSControlPanelAddressHandle()->_GetCurrentContentPanel())
->_RemoveSelectedFromList();
}
// Update all pages that have Device List active
for (int c = 0; c < (int)m_NNBSSContentPanelList.size(); c++) {
int pageID = c - 1;
pageID < 0 ? pageID = 0 : pageID = c - 1;
if (CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_IsCreated)
{
CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_RecreateList();
}
}
}
void CNNBSSDeviceListRemoveCache(int index)
{
if (index < 0)
{
index = 0;
}
if (SCNNBSSDeviceParametersList.size() < index)
{
index = (int)SCNNBSSDeviceParametersList.size();
}
auto itr = SCNNBSSDeviceParametersList.begin();
std::advance(itr, index);
if (itr != SCNNBSSDeviceParametersList.end())
{
SCNNBSSDeviceParametersList.erase(itr);
}
}
void CNNBSSDeviceListAddressConnectionRespond(NNBSSString& deviceAddress, bool OK, wxThread* thread)
{
for (int c = 0; c < (int)SCNNBSSDeviceParametersList.size(); c++)
{
if (SCNNBSSDeviceParametersList[c].Address == deviceAddress)
{
if (OK)
{
SCNNBSSDeviceParametersList[c].DeviceConnectionStatus = NNBSS_DEVICE_CONNECTION_STATUS_STRING_ONLINE;
}
else
{
SCNNBSSDeviceParametersList[c].DeviceConnectionStatus = NNBSS_DEVICE_CONNECTION_STATUS_STRING_UNKNOWNERROR;
}
break;
}
}
// Update all pages that have Device List active
for (int c = 0; c < (int)m_NNBSSContentPanelList.size(); c++)
{
int pageID = c - 1;
pageID < 0 ? pageID = 0 : pageID = c - 1;
if (CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_IsCreated)
{
CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_RecreateList();
}
}
// Delete thread
thread->Delete();
delete thread;
}
void CNNBSSDeviceListUpdate()
{// Whenever a USB device is connected, this function will be called automatically
{
std::vector<__STRING__> currentDevices, currentDevicePaths;
CNNBSSHardwareAddressHandle()->GetConnectedUSBCameraList(currentDevices, currentDevicePaths);
// Update all pages that have Device List active
for (int c = 0; c < (int)m_NNBSSContentPanelList.size(); c++)
{
int pageID = c - 1;
pageID < 0 ? pageID = 0 : pageID = c - 1;
if (CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_IsCreated)
{
CNNBSSDeviceListAddressHandle(m_NNBSSContentPanelList[c])->_RecreateList();
}
}
}
}
问题是,当我在 CNNBSSDeviceListAddressConnectionRespond function 之前删除 inline 关键字时,编译器会抛出错误,但对于其他编译器,编译器不会这样做。 我想知道,为什么其他人没有内联就可以工作,但 CNNBSSDeviceListAddressConnectionRespond function 却不行?
此外,完整的错误消息:
Severity Code Description Project File Line Suppression State
Error LNK1169 one or more multiply defined symbols found NNBSS NNBSS\Build\Debug\NNBSS.exe 1
Error LNK2005 "void __cdecl DeviceList::CNNBSSDeviceListAddressConnectionRespond(class NNBSSString &,bool,class NNBSSThread *)" (?CNNBSSDeviceListAddressConnectionRespond@DeviceList@@YAXAEAVNNBSSString@@_NPEAVNNBSSThread@@@Z) already defined in ClassManager.obj NNBSS NNBSS\Build\Thread.obj 1
我使用包括警卫。 我的 header 文件包含在超过 128 个文件中。
如果这里缺少任何东西,请告诉我
看来您使用的是 VS2019。 我遇到了同样的问题,正如 inte.net 所说,我们并不孤单。 因此,这是一个将在您重建项目后修复的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.