1

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"

6
  • What exactly is going on inside the body of OnFilechange here? Is the entire body of the function supposed to be a lambda that's the argument to first? Commented Jul 22, 2022 at 1:33
  • in the OnFileChange file I create a void function the which takes a function as a argument. The OnFileChange creates a new thread and executes a loop the which constantly checks a file for a change, when it changes it should execute the provided function. Since the thread requires a function inside of it, most of the OnFileChange function is a lambda Commented Jul 22, 2022 at 1:38
  • 2
    why your .hpp file include your .cpp file? Commented Jul 22, 2022 at 1:39
  • I'm new to c++, so any feedback on what I should do to improve the hpp file could help Commented Jul 22, 2022 at 1:40
  • 3
    My guess is that you're compiling using a pre-C++11 standard. Commented Jul 22, 2022 at 2:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

use -std=c++17 in g++ if possible as g++ defaulted to c++98

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.