C++中的语法错误

syntax errors in C++

本文关键字:错误 语法 C++      更新时间:2023-10-16

我是c++的新手。我的朋友刚刚给了我这个代码,但它不起作用,并发送了许多语法错误,如:错误C2146,错误C2734,。。。我不熟悉,所以我认为最好询问堆栈溢出。pixel.cpp:

#include<iostream>
#include"a.h"
using namespace std;
extern const unsigned uint_8 microsoftSansSerif_8ptBitmaps[];
extern const unsigned FONT_INFO microsoftSansSerif_8ptFontInfo;
extern const FONT_CHAR_INFO microsoftSansSerif_8ptDescriptors[];
int main() 
{
    getchar();
}

a.h:

// Font data for Microsoft Sans Serif 8pt
const unsigned uint_8 microsoftSansSerif_8ptBitmaps[] = {
    0b11110000, 
    0b00010000, 
    0b00101000, 
    0b00101000, 
    0b01000100, 
    0b01000100, 
    0b01111100, 
    0b10000010, 
    0b10000010, 
};
const FONT_CHAR_INFO microsoftSansSerif_8ptDescriptors[] = 
{
    {7, 0},         // A 
};
const FONT_INFO microsoftSansSerif_8ptFontInfo =
{
    2, //  Character height
    'A', //  Start character
    'A', //  End character
    2, //  Width, in pixels, of space character
    microsoftSansSerif_8ptDescriptors, //  Character descriptor array
    microsoftSansSerif_8ptBitmaps, //  Character bitmap array
};

错误:

a.h(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
a.h(2) : error C2146: syntax error : missing ';' before identifier 'microsoftSansSerif_8ptBitmaps'
a.h(3) : error C2059: syntax error : 'bad suffix on number'
a.h(3) : error C2146: syntax error : missing '}' before identifier 'b11110000'
a.h(4) : error C2059: syntax error : 'constant'

一些问题:

  1. uint_8FONT_INFOFONT_CHAR_INFO不在任何地方声明。

  2. 在自定义类型之前不必要地添加unsigned是错误的。

  3. 二进制文字需要在c++11模式下编译,以及支持该模式的编译器。

  4. 要使用getchar,需要包含<cstdio>

  5. 通常,应该将extern声明放在.h文件中,将定义放在.cpp中,而不是相反。