簡體   English   中英

Visual Studio Code C++ 上的錯誤不准確

[英]Inaccurate errors on 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);



}

由於您可以編譯代碼,因此您的編譯器沒有問題。 看起來 IntelliSense 配置不正確。 檢查您是否在cppStandard設置為正確的c_cpp_configuration.json版本(例如"cppStandard": "c++17" )。

另一個可能的原因是c_cpp_configuration.json包含無效的 JSON 並被忽略。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM