C++ 错误 C2146:语法错误:缺少“;” 在标识符“顶点”之前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2706277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:25:45  来源:igfitidea点击:

error C2146: syntax error : missing ';' before identifier 'vertices'

c++directx

提问by numerical25

I would usually search for this error. But in VS C++ Express, this error comes up for just about every mistake you do. Any how I recieve this error below

我通常会搜索此错误。但是在 VS C++ Express 中,这个错误几乎出现在你犯的每一个错误上。我如何在下面收到此错误

error C2146: syntax error : missing ';' before identifier 'vertices'

everytime I add the following code at the top of my document

每次我在文档顶部添加以下代码时

// Create vertex buffer
SimpleVertex vertices[] =
{
    D3DXVECTOR3( 0.0f, 0.5f, 0.5f ),
    D3DXVECTOR3( 0.5f, -0.5f, 0.5f ),
    D3DXVECTOR3( -0.5f, -0.5f, 0.5f ),
};

below is the code in it's entirety. Cant figure out whats wrong. thanks

下面是完整的代码。无法弄清楚什么是错的。谢谢

[EDIT]

[编辑]

// include the basic windows header file
#include "D3Dapp.h"


class MyGame: public D3Dapp
{
    public:
        bool Init3d();
};
MyGame game;

struct SimpleVertex
{
    D3DXVECTOR3 Pos;  // Position
};


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
     game.InitWindow(hInstance , nCmdShow);
     return game.Run();
}


bool MyGame::Init3d()
{
    D3Dapp::Init3d();
    // Create vertex buffer
    SimpleVertex vertices[] =
    {
        D3DXVECTOR3( 0.0f, 0.5f, 0.5f ),
        D3DXVECTOR3( 0.5f, -0.5f, 0.5f ),
        D3DXVECTOR3( -0.5f, -0.5f, 0.5f ),
    }

    return true;
}

new error

新错误

1>c:\users\numerical25\desktop\intro todirectx\msdntutorials\tutorial0\tutorial\tutorial\main.cpp(14) : error C2146: syntax error : missing ';' before identifier 'Pos'

回答by sbi

error C2146: syntax error : missing ';' before identifier 'vertices'
error C2146: syntax error : missing ';' before identifier 'vertices'

Usually this error occurs when what's before the identifier isn't known to the compiler. In your case that means the compiler hasn't seen SimpleVertexyet.

通常当编译器不知道标识符之前的内容时会发生此错误。在您的情况下,这意味着编译器还没有看到SimpleVertex

回答by Ben Voigt

I definitely see a missing semicolon ;toward the end of mainright before return true;.

我确实看到一个失踪分号;朝着结束main权利之前return true;

回答by user1826194

Extra comma is added at the end of last member of the structure. I think this was the mistake.

在结构的最后一个成员的末尾添加额外的逗号。我认为这是错误。

SimpleVertex vertices[] =
{
    D3DXVECTOR3( 0.0f, 0.5f, 0.5f ),
    D3DXVECTOR3( 0.5f, -0.5f, 0.5f ),
    D3DXVECTOR3( -0.5f, -0.5f, 0.5f )**,**
}