声明Windows API结构(DCB)的对象 - 错误C4430:缺少类型说明符 - 假定为int

时间:2017-12-29 17:30:51

标签: c++ visual-studio winapi serial-port visual-studio-2017

我正在尝试创建一个C ++程序,以便在Windows 7的Visual Studio社区2017中使用Windows API与串行端口设备进行通信。

尝试编译这段代码:

#include <iostream>
#include <Windows.h>
#include "stdafx.h"
#pragma hdrstop
using namespace std;

DCB dcb;

int main()
{
    return 0;
}

我将这些错误指向DCB dcb;

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C2146: syntax error: missing ';' before identifier 'dcb'

The DCB structure在Winbase.h中定义如下:

typedef struct _DCB {
    DWORD DCBlength;      /* sizeof(DCB)                     */
    DWORD BaudRate;       /* Baudrate at which running       */
    DWORD fBinary: 1;     /* Binary Mode (skip EOF check)    */
    DWORD fParity: 1;     /* Enable parity checking          */
    DWORD fOutxCtsFlow:1; /* CTS handshaking on output       */
    DWORD fOutxDsrFlow:1; /* DSR handshaking on output       */
    DWORD fDtrControl:2;  /* DTR Flow control                */
    DWORD fDsrSensitivity:1; /* DSR Sensitivity              */
    DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */
    DWORD fOutX: 1;       /* Enable output X-ON/X-OFF        */
    DWORD fInX: 1;        /* Enable input X-ON/X-OFF         */
    DWORD fErrorChar: 1;  /* Enable Err Replacement          */
    DWORD fNull: 1;       /* Enable Null stripping           */
    DWORD fRtsControl:2;  /* Rts Flow control                */
    DWORD fAbortOnError:1; /* Abort all reads and writes on Error */
    DWORD fDummy2:17;     /* Reserved                        */
    WORD wReserved;       /* Not currently used              */
    WORD XonLim;          /* Transmit X-ON threshold         */
    WORD XoffLim;         /* Transmit X-OFF threshold        */
    BYTE ByteSize;        /* Number of bits/byte, 4-8        */
    BYTE Parity;          /* 0-4=None,Odd,Even,Mark,Space    */
    BYTE StopBits;        /* 0,1,2 = 1, 1.5, 2               */
    char XonChar;         /* Tx and Rx X-ON character        */
    char XoffChar;        /* Tx and Rx X-OFF character       */
    char ErrorChar;       /* Error replacement char          */
    char EofChar;         /* End of Input character          */
    char EvtChar;         /* Received Event character        */
    WORD wReserved1;      /* Fill for now.                   */
} DCB, *LPDCB;`

1 个答案:

答案 0 :(得分:1)

当您使用预编译标题时,行上方的任何内容

#include "stdafx.h"

(或为预编译指定的标题的名称)

被忽略。

尝试重新排列您的包含以将其放在首位。

更多解释on MSDN,特别重要的是:

  

编译器将.h文件之前出现的所有代码视为预编译。它跳到与.h文件关联的#include指令之外,使用.pch文件中包含的代码,然后在文件名后编译所有代码。