首页 > 解决方案 > 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> &current, 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);



}

标签: c++visual-studio-code

解决方案


由于您可以编译代码,因此您的编译器没有问题。看起来 IntelliSense 配置不正确。检查您是否在(例如)cppStandard中设置了正确的 C++ 版本。c_cpp_configuration.json"cppStandard": "c++17"

另一个可能的原因可能是c_cpp_configuration.json包含无效 JSON 并被忽略。

在此处查看有关如何在 VSCode 上配置 C++ IntelliSense 的更多信息


推荐阅读