【问题标题】:Errors C2059 and C2061 in C language [duplicate]C 语言中的错误 C2059 和 C2061 [重复]
【发布时间】:2012-02-22 20:50:06
【问题描述】:

可能重复:
Why does VS2010 give syntax errors when syntax is correct?

我正在尝试使用 Visual Studio 2010 用 C 语言开发一种 Windows 32 服务。

我创建了一个新项目,并插入了 .c 文件:

  • main.c
  • service.c
  • misc.c

我还有两个头文件:

  • myerrors.h
  • 我的.h

这是我的代码(请注意,这只是一个草稿)。

main.c:

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "my.h"
#include "myerrors.h"

static int parse_args(int ac, char **av)
{
    int i = 0;

    while (++i < ac)
        if (strcmp(av[i], "-i") && !InstallMyService())
            return false;
        else if (strcmp(av[i], "-d") && !UninstallMyService())
            return false;
        else if (strcmp(av[i], "-p"))
            if (!av[i + 1])
                return false;
            else
            {
                if (!InsertPathInRegistry(av[i + 1]))
                    return false;
                i++;
            }
        else
            return false;
    return true;
}

int main(int ac, char **av)
{
    HANDLE hLogFile;

    if ((hLogFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
        aff_error(CANT_CREATE_FILE);    
    if (ac > 1)
    {
        if (!parse_args(ac, av))
        {
            aff_error(BAD_ARGUMENTS);
            return EXIT_FAILURE;
        }
    }
    else
    {
        SERVICE_TABLE_ENTRY DispatchTable[] = {{DC_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
        StartServiceCtrlDispatcher(DispatchTable);
    }
    getchar();
    if (!CloseHandle(hLogFile))
        aff_error(CLOSE_FILE_FAILED);
    return EXIT_SUCCESS;
}

misc.c:

#include <Windows.h>
#include <stdio.h>
#include "my.h"
#include "myerrors.h"

void aff_error(char *error_str)
{
    fprintf(stderr, "ERROR: %s\n", error_str);
}

bool InsertPathInRegistry(char *path)
{
    printf("LOG: Inserting %s as ", path);
}

void WriteInLogFile(HANDLE hLogFile, char *log_string)
{
    printf("WriteInLogFile function");
}

service.c:

#include <Windows.h>
#include "my.h"

bool InstallMyService()
{
    return true;
}

bool UninstallMyService()
{
    return true;
}

void WINAPI ServiceCtrlHandler(DWORD Opcode)
{

}

void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{

}

我的标题只是一些函数声明和宏,例如:

# define DC_SERVICE_NAME    "MyService"

/* MISC functions */

void aff_error(char *error_str);

我的.h

#ifndef _MY_H_
# define _MY_H_

#include <Windows.h>
#include <strsafe.h>

/* Macros */

# define LOG_FILE_PATH      "c:\\my_log_file.txt"
# define DC_SERVICE_NAME    "MyService"

/* MISC functions */

void aff_error(char *error_str);

/* SERVICE functions */

void WINAPI ServiceMain(DWORD ac, LPTSTR *av);
bool InstallMyService();
bool UninstallMyService();
bool InsertPathInRegistry(char *path);
void WINAPI ServiceCtrlHandler(DWORD Opcode);

#endif /*!MY_H_ */

在尝试编译项目时,我遇到了一些奇怪的错误:

my.h(19): error C2061: syntax error : identifier 'InstallMyService'
my.h(19): error C2059: syntax error : ';'
my.h(19): error C2059: syntax error : ')'

或者:

my.h(21): error C2061: syntax error : identifier 'InsertPathInRegistry'
my.h(21): error C2059: syntax error : ';'
my.h(21): error C2059: syntax error : 'type'

我在一些论坛上查看说这些错误通常是包含错误放置的错误,但我真的不知道在这种情况下,我不认为我在包含错误时犯了错误......

谁能照亮我?

谢谢。

【问题讨论】:

  • 想告诉我们my.h ?
  • @JamesMcNellis:啊,错过了。就此而言,它是 _Bool 而不是 bool iirc。
  • 我编辑了我的帖子以显示 my.h 但似乎我会在你的 cmets 中得到我的答案:)
  • @BillyONEal:是的。 _Bool 是新的 C99 基本布尔类型的名称。 &lt;stdbool.h&gt;bool 定义为_Bool 的同义词。
  • 好像@JamesMcNellis 明白了:-)

标签: c windows visual-studio-2010 visual-c++ compiler-errors


【解决方案1】:

bool 不是 ANSI C 中的数据类型。它是 C99 版本语言中的数据类型,仅当包含 &lt;stdbool.h&gt;,但 Visual Studio 不支持 C99 , 仅 C89 (C99 还增加了_Bool 数据类型,可以不包含任何头文件使用)。

我建议您将bool 替换为另一种类型,例如int,或者使用typedef 将其别名为intunsigned char 之类的。

【讨论】:

  • 我用int替换了所有bool,效果很好!谢谢!
  • @downvoter:为什么要投反对票?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多