OP here. These are the steps I took to resolve my issue, based on the last edit of my question:
- With the separate files, as given in my question, the error was
Expected identifier
inmscmix_dat.c
. - 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 ofExpected identifier
, but a new one arose. - Compiling the two files gave multiple errors in
msclib.cpp
, all wrt variable type conversions. - 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];
}
solved C and C++ : data file with error “Expected unqualified-id” [closed]