我有一个名为 main.cpp 的文件
#include <iostream>
#include <fstream>
#include <sstream>
#include "headers/lexer.hpp"
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Please provide a file";
exit(1);
}
std::cout << "Reading from: " << argv[1] << std::endl;
std::ifstream mainFile(argv[1]);
std::stringstream buf;
char t;
while (mainFile.get(t))
buf << t;
std::string orgCode = buf.str();
std::cout << "The code is:" << std::endl
<< orgCode;
Lexer lexer(orgCode);
std::cout << std::endl
<< "Reached the end of the program";
return 0;
}
将此头文件 lexer.hpp 放在“headers”目录中
#ifdef __LEXER_H
#define __LEXER_H
#include <string>
class lexer
{
public:
Lexer(std::string sourceCode)
{
source = sourceCode;
cursor = 0;
size = sourceCode.length();
curr = sourceCode.at(cursor);
}
private:
std::string source;
int cursor;
int size;
char curr;
};
#endif
这是tasks.json 文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}",
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
这是 c_cpp_proporties.json 文件:
`{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}`
问题是
Lexer lexer(orgCode);
行给出了错误identifier "Lexer" is undefined
我已经搜索了很多但没有找到解决方案。
我尝试过弄乱tasks.json文件和c_cpp_proporties.json文件,但无论我做什么都不起作用,就像我改变了几次路径一样,并且在
includePath
中我也尝试包含几个指向头文件的直接路径,添加到 args
更多参数中,但仍然不起作用
我发现我只是从 .hpp 文件中删除了以下几行:
#ifdef __LEXER_H
#define __LEXER_H
#endif
由于某种原因,当我将代码包裹在这些行周围时,它变灰并且不起作用,我不知道原因必须研究它,我敢打赌这将是我错过的愚蠢的东西。