0
votes

I have written a C++ code to sort out a vector of objects. When i pass a lamda function as the third argument to the sort function for sorting the objects, it shows an error on vs code saying,"expression expected" pointing to the starting of the third argument. this exact code works fine on online compilers and Xcode.


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Process
{
    public:
    int AT,BT,CT,TAT,WT;
    Process(int AT,int BT)
    {
        this->AT=AT;
        this->BT=BT;
        CT=0;
        TAT=0;
        WT=0;
    }
};



// bool comaprator(Process p1, Process p2)
// {
//     return p1.AT<p2.AT;
// }


bool comparor(Process p1, Process p2)
{
    return p1.AT<p2.AT;
}

class Scheduler
{
    int clock;
    vector<Process> pr;
    public:
    Scheduler(vector<Process> v)
    {
        pr=v;
        clock=0;
    }
    void printFcfsStats()
    {
        clock=0;
        sort(pr.begin(),pr.end(),[](Process p1, Process p2) -> bool{return p1.AT<p2.AT;} );
        
        //sort(pr.begin(),pr.end(),[](Process p1, Process p2) -> bool{return p1.AT<p2.AT;} );
        for(int i=0;i<pr.size();i++)
        {
            cout<<pr[i].AT<<"\n";
        }

    }

};

vector<Process> processCreator()
{
    cout<<"Enter the number of processes\n";
    vector<Process> pr;
    int nop;
    cin>>nop;
    for(int a=0;a<nop;a++)
    {
        cout<<"Enter the arrival time of procees "<<(a+1)<<"\n";
        int AT;
        cin>>AT;
        cout<<"Enter the burst time of procees "<<(a+1)<<"\n";
        int BT;
        cin>>BT;
        Process p(AT,BT);
        pr.push_back(p);
    }
    return pr;
}

int main()
{
    vector<Process> v;
    Scheduler obj(processCreator());
    
    obj.printFcfsStats();
    
}

I have tried deleting VS code completely (including the hidden files) and reinstalling it. The compiler that I am using is clang++.

images: https://drive.google.com/file/d/18cXn86lnJjtwlAvKNSylKRgIywJBZKU1/view?usp=sharing https://drive.google.com/file/d/1wS-Nv2lGoDoF7jvMmdtGByx_cKTZi318/view?usp=sharing

1
Is this a compiler error, or is it an intellisense error? In other words can you compile and run your code despite the error message? - john

1 Answers

1
votes

VS Code on MacOS runs clang without any C++ standard specified by default. Find file .vscode/task.json in your project and ensure it has desired standards specified (the compiler arguments are listed under "args" key):

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++14"
            ],

            ...

}