How do I fix this c++ expected expression error? - c++

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

Related

C++ : data file has error: Expected unqualified-id

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.

c++ overloaded member function not found in 'ShaderProgram

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

C++ : Transfer the Program into Header

Im new in C++, and i got a program called sendSMS:
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Socket.h"
#include <pthread.h>
#include <stdio.h>
#include "Config.h"
#include <mysql/mysql.h>
#include <cstdlib>
#include <SerialStream.h>
void *func_servidor(void *ptr_timer);
ServerSocket server(30001);
pthread_t thread_servidor;
pthread_t thread_transfdados;
pthread_t thread_BD;
pthread_cond_t cv;
pthread_mutex_t mp;
int ret;
#define CTRL_C "\x1A"
const int PORT_MON = 30000;
const string serialPort = "/dev/ttyS0";
using namespace LibSerial;
using namespace std;
int setSerial(SerialStream& ssStream, const string& port) {
...
....
}
int sendsms(int argc, char **argv) {
bool send = true;
...
....
return(0);
}
void *func_servidor(void *ptr)
{
...
....
return(0);
}
I want to transfer all those functions to a header .h file and call it in a main program(main.cpp), so the main.cpp only calls "everything" like:
#include sendSMS.h
class modem{
{
public:
void SendSms();
}
int main{
SendSms();
return(0);
}
And the header will be like?
#ifndef __SENDSMS__
#define __SENDSMS__
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Socket.h"
#include <pthread.h>
#include <stdio.h>
#include "Config.h"
#include <mysql/mysql.h>
#include <cstdlib>
#include <SerialStream.h>
class sendsms
{
private:
int ret;
const int PORT_MON;
int argc;
const string serialPort;
char **argv;
bool send;
public:
sendsms();
void *func_servidor(void *ptr);
int setSerial(SerialStream& ssStream, const string& port);
int sendsms();
};
#endif
In your main.cpp you will need an object of type modem in order to call sendsms() from main():
#include sendSMS.h
class modem{
{
public:
void SendSms();
}
int main(int argc, char **argv) {
modem m;
m.SendSms();
return(0);
}

variable was not declare in the scope class in private can not be access by member function

//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.

Error: more than one instance of overloaded function "Datafrompeer" matches the argument list

I get the following error when compiling my code below:
error:more than one instance of overloaded function "Datafrompeer"
matches the argument list
What can I do to resolve this?
#include <stdio.h>
#include <string.h>
#include <WinSock2.h>
#include <conio.h>
int Datafrompeer(char* SERVER_ADDRESS_STR,int SERVER_PORT,FILE * fileptr)
{
}
void main()
{
char* IPstr=NULL;
int Portnum;
FILE* SharedFilesList=NULL;
Datafrompeer(IPstr,Portnum,SharedFilesList);
}