microsoft / microsoft/vscode-cpptools

"Cout is ambigous" error when using namespace std

Open
#9,471 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Language Service verified Works in VS
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Environment
  • OS and version: OS: Windows_NT x64 10.0.19044
  • VS Code: 1.68.1
  • C/C++ extension: version: v1.10.7
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;
} 
  1. delete the '}' on line 29
  2. wait 5 seconds
  3. write the '}' back on the same line
  4. The problems tab now shows "cout is ambigous error 11 times"
  5. you can compile the code as well to see that it has no errors
  6. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report in a C++ main.cpp using the supplied code, editing the brace and watching the Problems tab; input.txt is needed to run the sample. Start by checking when the extension emits the ambiguous cout diagnostics during parsing, and consider the issue done when valid code compiles and runs without false cout errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, vscode
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.