microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

"Cout is ambigous" error when using namespace std #9471

Open tpkowastaken opened 2 years ago

tpkowastaken commented 2 years ago

Environment

Bug Summary and Steps to Reproduce

"Cout is ambigous" error when using namespace std

when programming in c++ while there is a code including this:

#include <iostream>
using namespace std;
int main(){
   cout << "hello world!";
}

then there is a chance that there appears to be "cout is ambigous error" in problems tab even though there is no error to be found whatsoever. It appears for every "cout" in the file. if you write std:: before the cout the error gets removed.

Steps to reproduce:

  1. create a new file called main.cpp and use the c++ language mode
  2. use the msys64 debugger (might not be required but i do make sure to rather include)
  3. write some code with cout in that code with at least 100 lines i will include my code on which it does bug out but it wasn't an issue only on this code:
    #include <iostream>
    #include <fstream>
    using namespace std; 
    void swap(int *element1, int *element2){
    int tmp = *element1;
    *element1 = *element2;
    *element2 = tmp;
    }
    void quicksort(int *inputfield, int n){
    cout << "{\ninputfield: ";
    for(int i = 0;i<n;i++){
        cout << inputfield[i] << " ";
    }
    cout << "\n";
    if(n>2){
        //getting the pivot:
        int pivot;
        {
            double pivotaverage = (inputfield[0] + inputfield[n/2] + inputfield[n-1])/3; // average of those pivots
            double a,b,c;
            a = abs(inputfield[0] - pivotaverage);
            b = abs(inputfield[n/2] - pivotaverage);
            c = abs(inputfield[n-1] - pivotaverage);
            if(a<=b && a<=c)pivot = 0; // getting the closest to average
            else if(b<=a && b<=c)pivot = n/2; //getting the closest to average
            else pivot = n-1; // getting the closest to average
            cout << "pivot index: " << pivot << "\n}\n";
            cout << "pivot value: " << inputfield[pivot];
        }
        int i = 0;
        for(int j = 0;j<n;j++){
            if(j == pivot)continue;
            if(i == pivot)i++;
            else if(inputfield[j] > inputfield[pivot])continue;
            else if(inputfield[j] <= inputfield[pivot]){
                //swap inputfield[i] with inputfield[j]
                int tmp = inputfield[i];
                inputfield[i] = inputfield[j];
                inputfield[j] = tmp;
            }
        }
        if(pivot < i){
            //swap pivot with with inputfield[i]
            int tmp = inputfield[i];
            inputfield[i] = inputfield[pivot];
            inputfield[pivot] = tmp;
            quicksort(inputfield, i+1);
            quicksort(inputfield + (i + 1) + 1, (n-((i+1)+1)));//give a pointer to element after the one that is scanned = (i+1) and skip the pointer = +1; then the length is n-that thing
        }
        else if(pivot > i){
            //swap pivot with inputfield[i+1]
            int tmp = inputfield[i+1];
            inputfield[i+1] = inputfield[pivot];
            inputfield[pivot] = tmp;
            quicksort(inputfield, i+1);
            quicksort(inputfield + (i + 1) + 1, (n-((i+1)+1)));//give a pointer to element after the one that is scanned = (i+1) and skip the pointer = +1; then the length is n-that thing
        }
    }
    else if(n == 2 && inputfield[0] > inputfield[1]){ // this is just to faster things up when it gets to the end
        int tmp = inputfield[1]; //Swap
        inputfield[1] = inputfield[0];
        inputfield[0] = tmp;
    }
    //if n = 1 or n = 0 there are no changes needed
    }
    int main(){
    int *field;
    int n;
    ifstream theFile("input.txt");
    string line;
    if(theFile){
        getline(theFile, line);
        for(int i = 0;line[i] != '\0';i++){
            if(line[i]<58 && line[i]>47){
                n = n*10+(line[i]-48);
            }
        }
        bool negative = false;
        field = new int[n];
        for(int i = 0;i<n;i++){field[i] = 0;}
        int g = 0;
        getline(theFile, line);
        for(int i = 0;line[i] != '\0';i++){
            if(line[i] == '-'){
                negative = true;
                cout << "hey";
            }
            else if(line[i] < 58 && line[i] > 47){
                field[g] = field[g]*10+(line[i]-48);
            }
            else{
                if(negative)field[g] = 0 - field[g];
                negative = false;
                g++;
            }
        }
        if(negative)field[g] = 0 - field[g];
    }
    else return 1;
    cout << n << '\n';
    for(int i = 0;i<n;i++){
        cout << field[i] << " ";
    }
    cout << "\n\n";
    quicksort(field, n);
    cout << "\n";
    for(int i = 0;i<n;i++){
        cout << field[i] << " ";
    }
    delete [] field;
    } 
  4. delete the '}' on line 29
  5. wait 5 seconds
  6. write the '}' back on the same line
  7. The problems tab now shows "cout is ambigous error 11 times"
  8. you can compile the code as well to see that it has no errors
  9. It can be run as well just make sure to put input.txt file in the same folder with content such as this:
    5
    2 4 7 8 9

Note: if you restart vs code or wait long enough (4minutes) and then edit - it mostly disapears.

Other Extensions

I have tested this with only c++ extension v1.10.7 running and C++ extension pack v1.2.0 running.

Additional Information

I have faced this issue on a low-tier laptop which might be a bit slower than usual. Make sure to replicate on lower-end hardware

My specs: CPU: Intel(R) Core(TM) i7-4702MQ CPU @ 2.20GHz Memory: 16GB

A screenshot of this happening: Captusssre

sean-mcmanus commented 2 years ago

Thanks for reporting this. I've reproed the issue. It doesn't repro with VS. It could be some difference in how we're updating IntelliSense after an edit.

aadia1234 commented 1 year ago

Hi, I'm experiencing this exact issue for the first time (June 2023). Here is what I see: Screenshot 2023-06-23 at 12 11 05 PM

It seems that whenever I'm creating a new statement or modifiyng one of my previous print statements the error occurs. It goes away if I comment then uncomment #include or restart vscode (but reappears soon after)

Specs: Apple M1 Pro macOS: 13.3.1 (a) (22E772610a)

VSCode:

Version: 1.79.2 Commit: 695af097c7bd098fbf017ce3ac85e09bbc5dda06 Date: 2023-06-14T08:58:33.551Z Electron: 22.5.7 Chromium: 108.0.5359.215 Node.js: 16.17.1 V8: 10.8.168.25-electron.0 OS: Darwin arm64 22.4.0

NFGase commented 1 year ago

same bug, VSCode vision:1.79.2, GCC vision:10.3.0, the code can run well but the terminal remind me that "Cout is ambigous". image

ayush110404 commented 1 year ago

i am also experiencing same bug issue on the latest release (may 2023) version-1.79.2 and it was even afer the update of latest release.

Screenshot (43)

CosecSecCot commented 1 year ago

Yeah, Im also facing the same bug, as of 23 June 2023

image

Why does this appear ? how is cout/cin amigious ?? How to fix this ??

vasuthakral commented 1 year ago

Same bug appeared

sean-mcmanus commented 1 year ago

FYI, this issue doesn't seem related to the new issue with 1.16.2, because this issue requires very particular edits.

We're tracking the new/regression issue with https://github.com/microsoft/vscode-cpptools/issues/11122 .

cristianVega615 commented 1 year ago

I have the same error, apparently the solution is to install the extension pack. Good to me it worked. This one is without the extension pack Captura de pantalla (2123)

With extension pack Captura de pantalla (2122)

sean-mcmanus commented 1 year ago

@tpkowastaken Installing the extension pack is not expected to change the behavior (the bug might repro after making an edit in main). We plan to release a fix in 1.16.3 in the next hour or so.

CosecSecCot commented 1 year ago

I reinstalled the C/C++ Extension after v1.16.3 released, the bug is fixed now You can see, no problems are detected

image

sean-mcmanus commented 1 year ago

Yes, the new 1.16.2 issue is fixed with 1.16.3 (but FYI the original issue is a different bug that still exists, but is harder to repro).

Kenza1525 commented 1 year ago

I have the same error, apparently the solution is to install the extension pack. Good to me it worked. This one is without the extension pack Captura de pantalla (2123)

With extension pack Captura de pantalla (2122)

Thanks it worked when i reloaded the Extension Pack

jakublATopera commented 7 months ago

I have similar error, not with std::cout and in proprietary code, so for obvious reasons I can't show a screenshot with an error message that a variable defined few lines above in the same anonymous namespace is ambiguous. Problem appears, disappears and appears again. Despite reported ambiguity, Ctrl+click works, going to the definition, so wild guess is classifying reparsed type definition as new, while should match with already known to the engine. C/C++ extension 1.19.9

sean-mcmanus commented 7 months ago

@jakublATopera 1.19.9 has a bug in which missing include errors won't be shown if C_Cpp.errorSquiggles is set to "enabled" (https://github.com/microsoft/vscode-cpptools/issues/12134) -- if you change that setting to the default value you may see the cause of the cause is a header that isn't being found. Also, it might be worth seeing if the repro still occurs with 1.18.5.

DevinF064 commented 5 months ago

Hi! I seem to be having a similar issue for the first time as of 05/15/2024. I'm being told that cin is ambiguous, but cout is fine.

C/C++ extension version 1.20.5

image

Included headers are:

#include <iostream>
#include <string>
#include <fstream>
#include <regex>
#include <vector>
#include <map>
#include <cctype>
using namespace std;
JushBJJ commented 2 months ago

Having this problem as well image