c++ - Visual Studio Code C++ 上的不准确错误
问题描述
我遇到了一个问题,尽管编译器编译了我的代码,但在我的部分代码下有很多错误和红线。例如:
连续的直角括号之间需要一个空格(使用'> >')
范围枚举是 C++11 扩展 [-Wc++11-extensions]
在嵌套名称说明符中使用枚举是 C++11 扩展 [-Wc++11-extensions]
我在网上浏览了一些东西,并将 VS 代码扩展中的 c++ 标准更改为 C++ 20,但错误仍然存在。请看下面的代码。
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
using std::cout;
using std::ifstream;
using std::istringstream;
using std::string;
using std::vector;
using std::sort;
using std::abs;
enum class State {kEmpty, kObsacle, kClosed, kPath, kStart, kFinish};
vector<State> ParseLine(string line)
{
istringstream my_stream(line);
int n;
char c;
vector<State> vector;
while (my_stream >> n >> c && c == ','){
if (n ==0)
{
vector.push_back(State::kEmpty);
}
else{
vector.push_back(State::kObsacle);
}
}
return vector;
}
vector<vector<State>> ReadBoardFile (string path){
ifstream my_file (path);
string line;
vector<vector<State> > board;
while(getline(my_file,line))
{
vector<State> row= ParseLine(line);
board.push_back(row);
}
return board;
}
bool compare(vector<int> node1, vector<int> node2){
int f1 = node1[2]+node1[3];
int f2 = node2[2]+node2[3];
return f1>f2;
}
void CellSort(vector<vector<int>> *v) {
sort(v->begin(), v->end(), compare);
}
int Heurstic (int x1, int y1, int x2, int y2)
{
return abs(x2-x1)+abs(y2-y1);
}
bool checkValidCell(int x, int y , vector<vector<State>> &table){
bool on_grid_x = (x >= 0 && x < table.size());
bool on_grid_y = (y >= 0 && y < table.size());
if (on_grid_x && on_grid_y)
return table[x][y] == State::kEmpty;
return false;
}
void addToOpen (int x, int y , int g, int h, vector<vector<int> > &openlist, vector<vector<State> > &grid)
{
openlist.push_back(vector<int>{x, y, g, h});
grid[x][y] = State::kClosed;
}
void ExpandNeighbors (const vector<int> ¤t, vector<vector<int>> &open, vector<vector<State>> &grid, int goal[2] ) // current node, x and y
{
int Xcurrent = current[0];
int Ycurrent = current[1];
int g = current[2];
int h;
int Xneighbor;
int Yneighbor;
if (checkValidCell(Xcurrent+1,Ycurrent,grid))
{
Xneighbor = Xcurrent+1;
Yneighbor = Ycurrent;
h = Heurstic(Xcurrent, Ycurrent, goal[0], goal[2]);
addToOpen (Xneighbor, Yneighbor , g, h, open, grid);
}
if (checkValidCell(Xcurrent-1,Ycurrent,grid))
{
Xneighbor = Xcurrent+1;
Yneighbor = Ycurrent;
h = Heurstic(Xcurrent, Ycurrent, goal[0], goal[2]);
addToOpen (Xneighbor, Yneighbor , g, h, open, grid);
}
if (checkValidCell(Xcurrent,Ycurrent+1,grid))
{
Xneighbor = Xcurrent+1;
Yneighbor = Ycurrent;
h = Heurstic(Xcurrent, Ycurrent, goal[0], goal[2]);
addToOpen (Xneighbor, Yneighbor , g, h, open, grid);
}
if (checkValidCell(Xcurrent,Ycurrent-1,grid))
{
Xneighbor = Xcurrent+1;
Yneighbor = Ycurrent;
h = Heurstic(Xcurrent, Ycurrent, goal[0], goal[2]);
addToOpen (Xneighbor, Yneighbor , g, h, open, grid);
}
}
vector<vector<State>> search (vector<vector<State>> grid, int start[2], int goal[2])
{
cout << "hello";
vector<vector<int>> openlist;
int x = start[0];
int y = start[1];
int g = 0 ;
int h = Heurstic(x, y, goal[0], goal[1]);
addToOpen(x,y,g,h,openlist,grid);
while(openlist.size()>0)
{
CellSort(&openlist);
auto current = openlist.back();
x = current[0];
y = current[1];
grid[0][1] = State::kPath;
if (x == goal[0] && y == goal[1])
{
grid[start[0]][start[1]] = State::kStart;
grid[goal[0]][goal[1]] = State::kFinish;
return grid;
}
ExpandNeighbors(current, openlist, grid, goal);
}
cout << "no path found";
return std::vector<vector<State>>{};
}
string CellString(State cell)
{
switch (cell)
{
case State::kObsacle: return"⛰️ " ;
case State::kPath: return " ";
case State::kStart: return " ";
case State::kFinish: return " ";
default: return "0 ";
}
}
void printBoard (vector<vector<State>> table)
{
for (int i =0; i <table.size(); i++)
{
for (int j =0; j<table[i].size(); j++)
{
cout << CellString(table[i][j]);
}
cout <<"\n";
}
}
int main(){
string path = "files/board";
int start[2] = {0,0};
int goal[2] = {4,5};
auto grid = ReadBoardFile(path);
auto solution = search(grid, start, goal);
printBoard(solution);
}
解决方案
由于您可以编译代码,因此您的编译器没有问题。看起来 IntelliSense 配置不正确。检查您是否在(例如)cppStandard
中设置了正确的 C++ 版本。c_cpp_configuration.json
"cppStandard": "c++17"
另一个可能的原因可能是c_cpp_configuration.json
包含无效 JSON 并被忽略。
在此处查看有关如何在 VSCode 上配置 C++ IntelliSense 的更多信息
推荐阅读
- php - Laravel 5.7:当我转储 $query (Builder obj.) 时,它仅显示全局范围,而应用了本地范围但现在显示。这是预期的行为吗?
- ionic-framework - 在后台收到的通知不会触发 pushObject.on('notification')。当应用程序也被关闭时
- .net - 我正在尝试使用图像、图像名称、图像编号和图像价格填充数据库,但仅填充图像列
- openssl - 强制 OpenSSL 不使用 /etc/pki/CA/newcerts
- ruby-on-rails - 如何防止从方法中输出 ActiveRecord::Base.logger?
- c++ - 极简可可应用程序未出现在停靠栏中
- android - Gradle 3.4.1 无法使用 Proguard 将响应解析为 Object
- python - 如何将sklearn决策树规则提取为pandas布尔条件?
- android - 不能使用约束布局!错误:AAPT2 错误:查看日志了解详细信息
- jenkins - 我可以告诉詹金斯代理在配置的用户下执行作业吗?