C and C++ : data file with error “Expected unqualified-id” [closed]

孤街浪徒 提交于 2019-12-25 19:42:47

问题


I would like to run C code in C++, using Xcode 8.33 on MacOS Sierra 10.12. I am new to C/C++, compilers, etc so please bear with me. The C code, when compiled and ran with make via Terminal, works. But when I throw all the same files into a XCode C++ project, there is an error with the data file. Note: I did change main.c to main.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]);

}

The file msclib.c calls on the data file mscmix_dat.c. Here is also 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];
}

When running/compiling, the mscmix_dat.c file gets the error Expected identifier or ( - which is what I need help with. Even when I replace mscmix_dat.c with .cpp, I get the error 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" },
   .
   .
   .
}

I would appreciate explanations as to why this error is occurring, suggestions on how to fix it, and if necessary alternatives to processing this data file. Thank you!


回答1:


OP here. These are the steps I took to resolve my issue, based on the last edit of my question:

  1. With the separate files, as given in my question, the error was Expected identifier in mscmix_dat.c.
  2. Per @LightnessRacesinOrbit's suggestion, I consolidated the multiple main.cpp, msclib.h, msclib.c, and mscmix_dat.c files into two files: main.cpp and msclib.c, by replacing the #include thisfile.c with the actual file code content. I also changed msclib.c to .cpp via simple rename. This eliminated the original error of Expected identifier, but a new one arose.
  3. Compiling the two files gave multiple errors in msclib.cpp, all wrt variable type conversions.
  4. Because of C++ differences from C, I handled the type conversion issue via casting, but also respecting const.

Below is my final, successfully compiling code.

// **** 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];
}


来源:https://stackoverflow.com/questions/49619851/c-and-c-data-file-with-error-expected-unqualified-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!