Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
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!
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 in mscmix_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 of Expected 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];
}
Related
I am making something in c++, it doesn't have any errors visible in Visual Studio code, but when I use g++ to be able to execute it, I get this error:
In file included from Main.cpp:6: In file included from ./Filechange/Filechange.hpp:1: ./Filechange/Filechange.cpp:14:24: error: expected expression
std::thread first ([&wtime,&f,&fn]() mutable {
^ Main.cpp:16:33: error: expected expression
OnFilechange("FileEvent", 0.5, [](char* txt){
^ 2 errors generated.
These are the files:
Main.cpp:
#include <lua.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include "Filechange/Filechange.hpp"
void wait(int seconds)
{
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
int main(int argc, char const *argv[])
{
lua_State *State = luaL_newstate();
OnFilechange("FileEvent", 0.5, [](char* txt){
std::cout << txt << std::endl;
});
lua_close(State);
return 0;
}
Filechange.cpp:
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
char* StringToChar(std::string str){
char* Array = new char[str.length() + 1];
strcpy(Array,str.c_str());
return Array;
}
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt)){
std::thread first ([&wtime,&f,&fn]() mutable {
std::ifstream file(f);
std::string str;
std::string filecontents;
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
char* LastContents = StringToChar(filecontents);
char* CurrentContents = StringToChar(filecontents);
while (true){
if (wtime != 0){
std::this_thread::sleep_for(std::chrono::milliseconds(int(wtime*1000)));
}
filecontents = "";
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
CurrentContents = StringToChar(filecontents);
if (strcmp(LastContents, CurrentContents) != 0){
LastContents = StringToChar(filecontents);
fn(StringToChar(filecontents));
}
}
});
}
Filechange.hpp:
#include "Filechange.cpp"
#ifndef FILECHANGE_HPP
#define FILECHANGE_HPP
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
void OnFilechange(const char *f,float wtime,void (*fn)(char txt));
#endif
There's also a extension less file named FileEvent which will change in the runtime using other code files.
The Filechange.cpp and Filechange.hpp are in a folder named "Filechange"
This function:
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt))
expects a function pointer, and a lambda in g++ is not implemented as a function pointer. Instead, you should declare the function to take a std::function, as in:
void OnFilechange(const char *f, float wtime, std::function<void(char *)> fn)
You may also need #include <functional> to get the declaration of std::function.
use -std=c++17 in g++ if possible as g++ defaulted to c++98
We have defined some methods to allow for ad-hoc console input when required. The methods are contained within a namespace and define and use a class to perform the actual work.
The definions, stripped down for brevity:
ConsoleInput.h
#include <cstddef>
#include <map>
#include <memory>
#include <vector>
namespace MyNameSpc
{
typedef std::vector<char> buffer_t;
class ConsoleInput
{
// Methods for windows and linux console input
}
int GetString(buffer_t &buffer, ...);
}
RequestInput.h
#include "ConsoleInput.h"
#include <cstddef>
#include <string>
namespace MyNameSpc
{
const std::string empty = std::string();
class RequestInputParam
{
// Methods
}
int RequestInput(buffer_t &buffer);
int RequestInput(buffer_t &buffer, const RequestInputParam ¶m);
// and other overloads
}
ConsoleInput.cpp
#include "ConsoleInput.h"
#include "RequestInput.h"
#include <cstddef>
#include <iostream>
#include <memory>
#include <string>
#include <fcntl.h>
#include <stdio.h>
// and other headers.
namespace MyNameSpc
{
// implements class ConsoleInput methods.
// Implements GetString()
}
RequestInput.cpp
#include "ConsoleInput.h"
#include "RequestInput.h"
#include <cstddef>
#include <iostream>
#include <memory>
#include <string>
namespace MyNameSpc
{
// implements the overloaded methods.
}
And we call them by #include RequestInput.h and then calling int retVal = MyNameSpc::RequestInput(...). This all works fine when all of the code is C++. I now have to reference this code from C code which was recently dropped into our repository. I don't think I can (well, I -can-, but I think it's going to break other things) simply compile the C code as C++, so I really do need to somehow pull this into C.
When I tried to build, I was getting initial errors about not finding the iostream header.. makes sense, but, after reading about extern "C", I am unclear how to proceed, when I have these C++ headers in the code, classes (the only one I might need to reference is the parameter class) and namespaces.
UPDATE
I have reviewed the links which were provided and attempted to make use of the information from here and here. I am having no success with this.
RequestInputWrapper.h
#ifndef REQUEST_WRAPPER_H
#define REQUEST_WRAPPER_H
#include <stddef.h>
#include "RequestInput.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct RequestInputParam RequestInputParam; // line 29
RequestInputParam* CreateRequestInputParam();
void DisposeRequestInputParam( RequestInputParam* pObject );
void C_AddMainPrompt(RequestInputParam *param, char *msg);
int C_RequestInputAllocPtr(char * * const ppInput,
unsigned int * const pInputLen);
int C_RequestInput(char * const pInput,
unsigned int * const pInputLen);
#ifdef __cplusplus
}
#endif
#endif /* REQUEST_WRAPPER_H */
RequestInputWrapper.cpp
#include "RequestInput.h"
#include "RequestInputWrapper.h" // line 13
#ifdef __cplusplus
extern "C" {
#endif
RequestInputParam* CreateRequestInputParam()
{
return new RequestInputParam(); // line 25
}
void DisposeRequestInputParam( RequestInputParam* pObject ) // line 28
{
if ( pObject != NULL )
{
delete pObject; // line 32
pObject = NULL;
}
}
void C_AddMainPrompt(RequestInputParam *param, char *msg) { param->AddMainPrompt( msg ); }
int C_RequestInputAllocPtr(char * * const ppInput,
unsigned int * const pInputLen)
{
return RequestInput(ppInput, pInputLen);
}
int C_RequestInput(char * const pInput,
unsigned int * const pInputLen)
{
return RequestInput(pInput, pInputLen);
}
#ifdef __cplusplus
}
#endif
It is currently throwing the following errors (I C&P only the first portion, it started getting repetitive; the line numbers won't match the code above, which has been stripped of all comments and such so I added comments with the noted line #'s):
cli/RequestInputWrapper.cpp: In function ‘RequestInputParam* CreateRequestInputParam()’:
cli/RequestInputWrapper.cpp:25:35: error: invalid use of incomplete type ‘RequestInputParam {aka struct RequestInputParam}’
return new RequestInputParam();
^
In file included from cli/RequestInputWrapper.cpp:13:0:
./Include/RequestInputWrapper.h:29:16: error: forward declaration of ‘RequestInputParam {aka struct RequestInputParam}’
typedef struct RequestInputParam RequestInputParam;
^
cli/RequestInputWrapper.cpp: In function ‘void DisposeRequestInputParam(RequestInputParam*)’:
cli/RequestInputWrapper.cpp:32:16: error: possible problem detected in invocation of delete operator: [-Werror]
delete pObject;
^
cli/RequestInputWrapper.cpp:28:6: error: ‘pObject’ has incomplete type [-Werror]
void DisposeRequestInputParam( RequestInputParam* pObject )
^
In file included from cli/RequestInputWrapper.cpp:13:0:
./Include/RequestInputWrapper.h:29:16: error: forward declaration of ‘struct RequestInputParam’ [-Werror]
typedef struct RequestInputParam RequestInputParam;
^
cli/RequestInputWrapper.cpp:32:16: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
delete pObject;
^
I suspect the answers in the articles suggested as "duplicates" have simply been written and not tested. After more searching, I eventually stumbled across this answer which offered the guidance needed to get this to build.
In short, I was missing the namespace in my C++ wrapper code (facepalm) and I had to add the reinterpret_cast calls. I also changed the name of the type in the typedef... I thought it was referencing the C++ type (as that was what at least one example seemed to show) but, nope... it needs to be it's own struct type.
RequestInputWrapper.h
#ifndef REQUEST_WRAPPER_H
#define REQUEST_WRAPPER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
typedef struct C_RequestInputParam C_RequestInputParam;
C_RequestInputParam * CreateRequestInputParam();
void DisposeRequestInputParam( C_RequestInputParam *pObject );
void C_AddMainPrompt( C_RequestInputParam *param, char *msg);
int C_RequestInputAllocPtr( char * * const ppInput,
unsigned int * const pInputLen );
int C_RequestInput( char * const pInput,
unsigned int * const pInputLen );
#ifdef __cplusplus
}
#endif
#endif /* REQUEST_WRAPPER_H */
RequestInputWrapper.cpp
#include "RequestInput.h"
#include "RequestInputWrapper.h"
extern "C" {
C_RequestInputParam * CreateRequestInputParam()
{
return reinterpret_cast< C_RequestInputParam * >( new RequestInputParam() );
}
void DisposeRequestInputParam( C_RequestInputParam *pObject )
{
if ( pObject != NULL )
{
delete reinterpret_cast< MyNameSpc::RequestInputParam * >( pObject );
pObject = NULL;
}
}
void C_AddMainPrompt( C_RequestInputParam *param, char *msg )
{
reinterpret_cast< MyNameSpc::RequestInputParam * >( param )->AddMainPrompt( msg );
}
int C_RequestInputAllocPtr( char * * const ppInput,
unsigned int * const pInputLen )
{
return MyNameSpc::RequestInput( ppInput, pInputLen );
}
int C_RequestInput( char * const pInput,
unsigned int * const pInputLen )
{
return MyNameSpc::RequestInput( pInput, pInputLen );
}
}
SIMPLY PUT Why does my text data file myData.cpp get the error Expected unqualified-id before '{' token? The file alone gives rise to this error and has been reproduced here http://coliru.stacked-crooked.com/a/7f32b5e643fb4d52
// ***** myData.cpp ******
{ // <---- error occurs here
{ "*****", "Error" },
{ "00-01", "Instructional exposition (textbooks, tutorial papers, etc.)" },
{ "00-02", "Research exposition (monographs, survey articles)" },
{ "00A05", "General mathematics" }
}
MORE DETAIL. Potentially helpful, but not necessary to reproduce error.
Right now, with 2 files main.cpp and myFunctions.cpp, everything works. But when I split it into 3 files main.cpp, myFunctions.cpp, and myData.cpp, I get the error Expected unqualified-id before '{' token.
I want to make it 3 files, because the text data for myData.cpp is pretty long and I don't want it to clutter myFunctions.cpp.
This is what I have as 2 files that compiles.
// ***** 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]);
}
// ****** myFunctions.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);
return strcmp(msc_code, p->code);
}
size_t msc_get_no(const char* msc_code)
{
MSCDat* p;
p = (MSCDat*) bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);
return p - &mscdat[0];
}
This is what I have as 3 files that does not compile, because of the Expected unqualified-id error in myData.cpp. The only difference is with myFunctions.cpp, so I have excluded main.cpp and myData.cpp.
// ***** myFunctions.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[] =
#include "myData.cpp" //<------ only here is different
;
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);
return strcmp(msc_code, p->code);
}
size_t msc_get_no(const char* msc_code)
{
MSCDat* p;
p = (MSCDat*) bsearch(msc_code, &mscdat[0], msccnt, sizeof(mscdat[0]), msc_cmp);
return p - &mscdat[0];
}
THANK YOU
Your IDE tries to compile myData.cpp by itself. But this file is only an include file. If you rename it to myData.h (or even
myData.dat) everthing should be fine.
What's wrong I'm doing? Strange, because signature of constructor is the same. The compiler says:
'ShaderProgram::ShaderProgram(std::vector< int*, std::allocator< _Ty >>)': overloaded member function not found in 'ShaderProgram'.
This error occures, when I use 3 files with code(below), but when I put this code in 1 file(main cpp) - it works
//main.cpp
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "ShaderHelpers.h"
int main(int argc, const char* argv[]) {
int* a = new int(5);
int* b = new int(7);
ShaderProgram *sp = new ShaderProgram(std::vector<int*>{ a, b});
return 0;
}
================================================================
//shader.cpp
#include "ShaderHelpers.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>
ShaderProgram::ShaderProgram() { }
ShaderProgram::ShaderProgram(std::vector<int*> shaders)
{
Shaders = shaders;
for each (int* i in shaders)
{
std::cout << i;
}
}
ShaderProgram::~ShaderProgram()
{
std::cout << "delete";
}
===============================================================
//ShaderHelper.h
#pragma once
#include <string>
class ShaderProgram
{
public:
std::vector<int*> Shaders;
ShaderProgram(std::vector<int*> shaders);
~ShaderProgram();
private:
ShaderProgram();
};
I just needed to add
# include <vector>
into the ShaderHelper.h
//This is the header file (header.h)
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
~
~
//This is the cpp file (program.cpp)
#include "header.h"
#include <cstring>
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
I'm getting program.cpp:13: error: 'w' was not declared in this scope
I'm trying to just do the strcpy from data which contain some info to w which is from the private section of the class and using the member function to access them.
I'm not sure if I forgot anything and why I can't access them.
Thanks to the last answer from Sergey Vakulenko
The sequence of the header file is very important.
It should be
#include <cstring>
#include "header.h"
not
#include "header.h"
#include <cstring>
add these headers to your cpp file:
#include <stdio.h>
#include <string.h>
#include "nameofheader.h"
Edit (more full explication ):
for me, that exemple not give any error:
1.h:
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
1.cpp:
#include <stdio.h>
#include <string.h>
#include "1.h"
//This is the cpp file (program.cpp)
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
int main (int argc, char** argv) {
return 0;
}
compled with g++:
g++ 1.cpp -o 1
Your program, the way you are showing it to us here, should compile without problems:
ideone.com/Bj6VU
If you want more help, you should make the all of the two files you are compiling (program.cpp and header.h) available.