C++ typedef error

C++ typedef error

本文关键字:error typedef C++      更新时间:2023-10-16
bool CInetWrapper::OpenFtpConnection (LPCTSTR lpszServerName)
{
    // internetconnect(inet_open,'ftp.site.ru',port,'login','pass',INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,0);

    if (OpenInternet() && m_hConnection == NULL)
        // (HINTERNET,LPCSTR,INTERNET_PORT,LPCSTR,LPCSTR,DWORD,DWORD,DWORD);
        typedef HINTERNET (__stdcall* InternetConnect_)(HINTERNET,LPCSTR,INTERNET_PORT,LPCSTR,LPCSTR,DWORD,DWORD,DWORD);
        InternetConnect_ ic = (InternetConnect_)helper.GetProcAddressEx("wininet.dll", "InternetConnectA");
        m_hConnection = ic(
        m_hInternet,
        lpszServerName? lpszServerName:
            m_lpszServerName? m_lpszServerName: "localhost",
        INTERNET_DEFAULT_FTP_PORT,
        m_login,
        m_password,
        INTERNET_SERVICE_FTP,
        0,
        0);

    return CheckError(m_hConnection != NULL);
}

和编译器说:1> ------构建开始:项目:klstart,配置:调试Win32 -------1> httpreader.cpp1> c: u admin Visual Studio 2010 Projects klstart klstart klstart httpreader.cpp(100):错误c2065:'Internet Conconnect_'':undeclared标识符1> c: u admin Visual Studio 2010 Projects klstart klstart klstart httpreader.cpp(100):错误c2146:语法错误:缺失';';''在标识符"助手"之前我写错了什么?

您忘了将if的真实分支包装到复合语句中。

基本上,问题与此代码

中的问题相同
if (some_condition)
  typedef int MyType;
  MyType i; // ERROR: `MyType` is undeclared identifier
  ...

在上面的简单示例中,虚构的"作者"想这样做

if (something)
{
  typedef int MyType;
  MyType i; 
  ...
}

,但他忘了把这些{}放在那里,最终得到了完全不同的东西。您在代码中犯了同样的错误。

由于您没有在if之后创建复合语句(使用{}),该 if的真实分支中唯一包含的部分是您的typedef,而没有其他内容。该分支带有孤独的typedef是一个单独的本地范围,该范围在if之后立即结束。这意味着在您的if打字后InternetConnect_不再知道。

将您的typedef放在if之前,或将真分支包装到一对{}中。

尝试使用#include <cstdlib>

或尝试使用struct,更简单易于遵循,错误的机会更少。