gpt4 book ai didi

c++ - MFC visual c++ LNK2019链接错误

转载 作者:行者123 更新时间:2023-11-28 06:12:29 24 4
gpt4 key购买 nike

我只是不明白为什么我可以在类上使用公共(public)变量,但在尝试使用 getLicenceRefused 方法时出现链接错误。我不确定问题是不是因为我之前遇到的 CString 复制构造函数问题所以把参数去掉了,但我仍然得到同样的错误。

所以类正在编译和链接(因为我可以使用成员变量),当我尝试调用任何函数时我得到这个错误。

我已尽力做到尽可能全面,如果我遗漏了一些重要的东西或包含了太多内容,我深表歉意。

我只是偶尔使用 c++(很抱歉,如果我的术语是基于 java 的),因为它正在维护旧代码,这里没有人使用 c++ 来维持他们的技能水平。这两个类都是内部类(由现在离开的人写的:)),这两个类在同一个解决方案的不同项目中。 ConfigurationDialog 项目依赖于 CServerSettings 项目。这是我的调用代码

int CConfigurationDialog::testConnection(CString connectionName, CString origin)
{
CServerConnection* connection = getConnection();

// these 2 lines of code work and return the correct result, it is the body of the function with problems
auto result = connection->m_licencesRefused.find(origin);
bool connRefused = (result != connection->m_licencesRefused.end());

if(connection->getLicenceRefused(origin)) // this is the line with the link error

类定义

class CServerConnection
{
public:
CServerConnection();
CServerConnection( LPCTSTR connectionName );

void setLicenceRefused(CString origin, bool value);
bool getLicenceRefused(CString origin);
void setLicenceRequested(CString origin, bool value);
bool getLicenceRequested(CString origin);
void clearAllLicences();
CString getLoggedOnOrigin();
void setURLNotFound(bool notFound);
bool getURLNotFound();

public:
bool m_initialised;
CString m_connectionName;
CString m_lastError;
CString m_password;
CString m_company;
CString m_username;
CString m_sessionID;

public:
std::set<CString> m_licencesRefused;

bool m_urlNotFound;
};

类主体(我不能包含整个文件,因为它超过 1000 行并且包含几个类):

CServerConnection::CServerConnection(LPCTSTR connectionName)
{
m_initialised = false;
m_connectionName = connectionName;
m_lastError = _T( "" );
m_urlNotFound = false;
}

CServerConnection::CServerConnection()
{
m_initialised = false;
m_connectionName = _T( "" );
m_lastError = _T( "" );
m_urlNotFound = false;
}

void CServerConnection::setLicenceRefused(CString origin, bool value)
{
if(value)
{
m_licencesRefused.insert(origin);
}
else
{
m_licencesRefused.erase(origin);
}
}

bool CServerConnection::getLicenceRefused(CString origin)
{
auto result = m_licencesRefused.find(origin);
return (result != m_licencesRefused.end());
}

void CServerConnection::setLicenceRequested(CString origin, bool value)
{
if(value)
{
m_licencesRequested.insert(origin);
}
else
{
m_licencesRequested.erase(origin);
}
}

bool CServerConnection::getLicenceRequested(CString origin)
{
auto result = m_licencesRequested.find(origin);
return (result != m_licencesRequested.end());
}

void CServerConnection::clearAllLicences()
{
m_licencesRefused.clear();
setLicenceRefused(XL_FINANCE_ORIGIN, false);
setLicenceRefused(XL_POP_ORIGIN, false);

m_licencesRequested.clear();
setLicenceRequested(XL_FINANCE_ORIGIN, false);
setLicenceRequested(XL_POP_ORIGIN, false);
}

CString CServerConnection::getLoggedOnOrigin()
{
if(getLicenceRequested(XL_FINANCE_ORIGIN) && !getLicenceRefused(XL_FINANCE_ORIGIN))
{
return XL_FINANCE_ORIGIN;
}

if(getLicenceRequested(XL_POP_ORIGIN) && !getLicenceRefused(XL_POP_ORIGIN))
{
return XL_POP_ORIGIN;
}

return _T("");
}

void CServerConnection::setURLNotFound(bool notFound)
{
m_urlNotFound = notFound;
}

bool CServerConnection::getURLNotFound()
{
return m_urlNotFound;
}

和错误:

 error LNK2019: unresolved external symbol "public: bool __thiscall CServerConnection::getLicenceRefused(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >)" (?getLicenceRefused@CServerConnection@@QAE_NV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@@Z) referenced in function "private: int __thiscall CConfigurationDialog::testConnection(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >,class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >)" (?testConnection@CConfigurationDialog@@AAEHV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@0@Z)

最佳答案

头文件提供了足够的信息让您声明变量。就此而言,只需编译(但不链接)代码。当您链接时,链接器必须解决例如函数引用,例如对 ServerConnection::getLicenceRefused 的引用,通过引入相关的机器代码。

您必须告诉链接器要链接哪个库或目标文件以引入该机器代码。

getLicenseRefused 中的小写首字母表示它不是 Microsoft 代码,尽管它可能是从 C++ 访问的属性。除非熟悉的人碰巧看到并做出回应,否则您必须自己弄清楚库或目标文件。通常这涉及到查看文档,但根据具体情况,它可能就像向项目中添加一个 .cpp 文件一样简单;随便。


附录:在我写完上面的内容之后,OP 已经提供了该类的源代码。

所以这可能只涉及将该 .cpp 文件添加到项目中。

关于c++ - MFC visual c++ LNK2019链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30985569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com