gpt4 book ai didi

c++ - C 和 C++ : data file with error "Expected unqualified-id"

转载 作者:行者123 更新时间:2023-11-30 21:06:17 24 4
gpt4 key购买 nike

我想在 MacOS Sierra 10.12 上使用 Xcode 8.33 在 C++ 中运行 C 代码。我是 C/C++、编译器等方面的新手,所以请耐心等待。 C 代码,用 make 编译并运行时通过终端,可以工作。但是当我将所有相同的文件放入 XCode C++ 项目时,数据文件出现错误。注意:我确实更改了main.cmain.cpp .

//**** main.cpp *****

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>

extern "C" {
#include "msclib.h"
}

int main(int argc, char** argv)
{
assert(argc >= 1);
return msc_get_no(argv[1]);

}

文件msclib.c调用数据文件mscmix_dat.c 。这也是msclib.h

// ***** msclib.h *****

extern size_t msc_get_no(const char*);

// ***** msclib.c *****

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>

#include "msclib.h"

struct msc_data
{
const char* code;
const char* desc;
};

typedef struct msc_data MSCDat;

static const MSCDat mscdat[] =
#include "mscmix_dat.c"
;

static const size_t msccnt = sizeof(mscdat) / sizeof(mscdat[0]);

static int msc_cmp(const void* a, const void* b)
{
const char* msc_code = a;
const MSCDat* p = b;
return strcmp(msc_code, p->code);
}

size_t msc_get_no(const char* msc_code)
{
assert(NULL != msc_code);
assert(strlen(msc_code) == 5);

MSCDat* p = bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);

if (NULL == p)
{
fprintf(stderr, "MSC \"%s\" not valid\n", msc_code);
return 0;
}

assert(NULL != p);
return p - &mscdat[0];
}

运行/编译时,mscmix_dat.c文件收到错误 Expected identifier or ( - 这就是我需要帮助的地方。即使我替换 mscmix_dat.c.cpp ,我收到错误 Expected unqualified-id

// ***** mscmix_dat.c *****
{ //<-- Xcode highlights this line and gives the error
{ "*****", "Error" },
{ "00-01", "Instructional exposition (textbooks, tutorial papers, etc.)" },
{ "00-02", "Research exposition (monographs, survey articles)" },
{ "00A05", "General mathematics" },
.
.
.
}

我希望能够解释为什么会发生此错误、如何修复该错误的建议以及处理此数据文件的必要替代方案。谢谢!

最佳答案

在这里。以下是我根据问题的最后一次编辑所采取的解决问题的步骤:

  1. 对于单独的文件,如我的问题中所示,错误是 mscmix_dat.c 中的预期标识符
  2. 根据 @LightnessRacesinOrbit 的建议,我通过替换 #include thisfile 将多个 main.cpp、msclib.h、msclib.c 和 mscmix_dat.c 文件合并为两个文件:main.cpp 和 msclib.c .c 为实际文件代码内容。我还通过简单的重命名将 msclib.c 更改为 .cpp。这消除了原来的预期标识符错误,但出现了一个新错误。
  3. 编译这两个文件时,msclib.cpp 中出现多个错误,所有错误均涉及变量类型转换。
  4. 由于 C++ 与 C 的差异,我通过强制转换处理类型转换问题,但也遵守 const

下面是我最终成功编译的代码。

// **** main.cpp ****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>

#include<string>
#include<iostream>
using namespace std;

extern size_t msc_get_no(const char*);

int main(int argc, char** argv)
{
assert(argc >= 0);
return (int)msc_get_no(argv[1]); // casting

}

// **** msclib.cpp ****
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>

extern size_t msc_get_no(const char*);

struct msc_data
{
const char* code;
const char* desc;
};

typedef struct msc_data MSCDat;

static const MSCDat mscdat[] =
{
{ "*****", "Error" },
{ "00-01", "Instructional exposition (textbooks, tutorial papers, etc.)" },
{ "00-02", "Research exposition (monographs, survey articles)" },
{ "00A05", "General mathematics" }
}
;

static const size_t msccnt = sizeof(mscdat) / sizeof(mscdat[0]);

static int msc_cmp(const void* a, const void* b)
{
const char* msc_code = static_cast<const char*>(a); //<----
const MSCDat* p = static_cast<const MSCDat*>(b); // (const MSCDat*)b also works
return strcmp(msc_code, p->code);
}


size_t msc_get_no(const char* msc_code)
{
assert(NULL != msc_code);
assert(strlen(msc_code) == 5);

MSCDat* p; // changed initialization of p
p = (MSCDat*) bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);

if (NULL == p)
{
fprintf(stderr, "MSC \"%s\" not valid\n", msc_code);
return 0;
}

assert(NULL != p);
return p - &mscdat[0];
}

关于c++ - C 和 C++ : data file with error "Expected unqualified-id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49619851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com