【问题标题】:Random number gen- Undeclared identifier随机数生成 - 未声明的标识符
【发布时间】:2012-08-10 23:59:16
【问题描述】:

到目前为止,我将程序代码的一部分组合在一起很糟糕

#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define MAX_BUFF_SIZE 1024
#define IDM_FILE_RUN 40001
#define IDM_APP_EXIT 40002

//Window Function
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
               LPSTR lpszArgs, int nWinMode)
{

WNDCLASS wcls;
HWND hwnd;
MSG msg;

// Name of window and window class
LPCWSTR szWinName   = L"Threads Program";
LPCWSTR szClassName = L"ThreadsProgram";


wcls.hInstance = hThisInst;
wcls.lpszClassName = szClassName;
wcls.lpfnWndProc = WindowFunc;
wcls.style = 0;
wcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wcls.lpszMenuName = NULL;
wcls.cbClsExtra = 0;
wcls.cbWndExtra = 0;
wcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

// Register windows class
if(!RegisterClass(&wcls))
{
    return 0;
}

// Create main window
hwnd = CreateWindow(szClassName,
    szWinName,
    WS_OVERLAPPEDWINDOW,
    100,
    100,
    400,
    400,
    HWND_DESKTOP,
    NULL,
    hThisInst,
    NULL );

// Show main window
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);

// Message loop
while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return (int)msg.wParam;
}



LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message, 
                        WPARAM wParam, LPARAM lParam)
{
static char textBuffer[MAX_BUFF_SIZE];
static int nRead;


switch(message)    
{
case WM_CREATE:
    {
        HMENU hMenu;
        HMENU hMenuPopup;

        // create menus
        hMenu = CreateMenu();
        hMenuPopup = CreateMenu();

        // populate menus
        AppendMenu(hMenuPopup, MF_STRING,  IDM_FILE_RUN,   L"&Choose Balls");   
        AppendMenu(hMenuPopup, MF_STRING,  IDM_APP_EXIT,   L"&Exit");  
        AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup,   L"&File");

        // attach menus to main window
        SetMenu(hMainWindow, hMenu);
    }
    break;
case WM_COMMAND:
    {
        // Obey command
        switch(LOWORD(wParam))
        {
        case IDM_FILE_RUN:

            {
                srand (time(NULL));
                for (int i = 0; i < 6; i++)
                    printf ("%i\n", (rand ()% 49) + 1);  




    return 0;

            }
            break;
        case IDM_APP_EXIT:
            SendMessage(hMainWindow, WM_CLOSE, 0, 0);
            break;
        }
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hMainWindow, message, wParam, lParam);
}
return 0;
}// Window function

错误消息说我有一个未声明的标识符,它与我的随机数部分有关,

case IDM_FILE_RUN:              
            {
                srand (time(NULL));
                for (int i = 0; i < 6; i++)
                    printf ("%i\n", (rand ()% 49) + 1);  

                return 0;
            }

我不确定我需要写什么以及我需要在哪里写来修复它。

谢谢

Error   9   error C2059: syntax error : ')' 108 1
Error   6   error C2065: 'i' : undeclared identifier    108 1
Error   8   error C2065: 'i' : undeclared identifier    108 1
Error   4   error C2143: syntax error : missing ')' before 'type'   108 1
Error   2   error C2143: syntax error : missing ';' before 'type'   108 1
Error   3   error C2143: syntax error : missing ';' before 'type'   108 1
Error   5   error C2143: syntax error : missing ';' before 'type'   108 1

【问题讨论】:

  • 确切的错误信息是什么?
  • 另外,所有发布的代码都是该问题所必需的吗?我想更多的是Short, Self Contained, Correct (Compilable), Example
  • 7 错误信息 syntax error ')' undeclared identifier 'i' (两次) 语法错误 ')'before type 语法错误';'before type (三次)
  • 如果您在问题正文中发布完整错误消息,我相信很容易得到答案。
  • 抱歉,我不确定是否需要所有发布的代码。

标签: c random identifier


【解决方案1】:

这可能是一个长镜头,但将您当前的 for 循环更改为如下所示:

  int i;
  for (i = 0; i < 6; i++)
    ...

可能是你的编译器不喜欢在for 中声明变量(我的不喜欢,除非我给它一个特殊的命令行选项)

【讨论】:

  • 是的,这很有帮助,因为它现在可以使用 int i 编译;在 srand 线之上,当它在 for 线之上时,仍然出现错误。虽然它编译它不显示数字,但原始代码在控制台中工作,对于窗口程序的输出行是否相同?
  • @GennySaxo 抱歉,我不知道这个问题的答案.. 您可能需要四处寻找您从中获取代码的信息。
  • 在 for 循环中声明一个变量,如 for (int i = 0; i &lt; 6; i ++) 是 C99 标准中的一个新特性。 Microsoft 的编译器不支持 C99。 @GennySaxo:告诉我们“它仍然出现错误”并没有什么帮助;您需要准确地告诉我们什么错误是什么。可能你应该发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
相关资源
最近更新 更多