You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复C++编译时出现的expected expression错误?

C++编译错误排查:Lambda表达式相关的expected expression错误

编译错误信息

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.

相关代码文件

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>
#include <cstring>

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)));
            }
            file.clear();
            file.seekg(0);
            filecontents = "";
            while (std::getline(file,str)){
                filecontents += str;
                filecontents.push_back('\n');
            }
            delete[] CurrentContents;
            CurrentContents = StringToChar(filecontents);
            if (strcmp(LastContents, CurrentContents) != 0){
                delete[] LastContents;
                LastContents = StringToChar(filecontents);
                fn(StringToChar(filecontents));
            }
        }
    });
    first.detach();
}

修改后的Filechange.hpp

#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

错误原因与修复方案

  1. Lambda表达式需要C++11及以上标准支持
    g默认可能使用C98标准,而Lambda是C++11引入的特性,编译时必须指定标准版本:

    g++ -std=c++11 Main.cpp Filechange/Filechange.cpp -o filewatch
    

    也可以使用更高版本如-std=c++17。

  2. 头文件错误

    • 原Filechange.hpp中包含了Filechange.cpp,会导致编译时重复定义函数,必须移除该行。
    • 函数声明void (*fn)(char txt)与定义中的void (*fn)(char* txt)参数类型不匹配,修正后保证声明和定义一致。
  3. 线程对象未处理
    创建std::thread后必须调用join()或detach(),否则程序退出时会触发std::terminate()终止程序。这里线程是无限循环监控文件,用detach()让线程后台运行更合适。

  4. 额外问题修复

    • 原代码中文件读取一次后,std::ifstream的指针停在文件末尾,后续读取不到更新内容,需要用file.clear()和file.seekg(0)重置状态和指针。
    • StringToChar返回的动态分配内存未释放,存在内存泄漏,代码中补充了delete[]操作;更优方案是直接使用std::string传递内容,避免手动内存管理。

内容的提问来源于stack exchange,提问作者Moonlight Shinesparkle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 15:24:18