Cout is ambiguous c ошибка

This kind of thing doesn’t just magically happen on its own; you changed something! In industry we use version control to make regular savepoints, so when something goes wrong we can trace back the specific changes we made that resulted in that problem.

Since you haven’t done that here, we can only really guess. In Visual Studio, Intellisense (the technology that gives you auto-complete dropdowns and those squiggly red lines) works separately from the actual C++ compiler under the bonnet, and sometimes gets things a bit wrong.

In this case I’d ask why you’re including both cstdlib and stdlib.h; you should only use one of them, and I recommend the former. They are basically the same header, a C header, but cstdlib puts them in the namespace std in order to «C++-ise» them. In theory, including both wouldn’t conflict but, well, this is Microsoft we’re talking about. Their C++ toolchain sometimes leaves something to be desired. Any time the Intellisense disagrees with the compiler has to be considered a bug, whichever way you look at it!

Anyway, your use of using namespace std (which I would recommend against, in future) means that std::system from cstdlib now conflicts with system from stdlib.h. I can’t explain what’s going on with std::cout and std::cin.

Try removing #include <stdlib.h> and see what happens.

If your program is building successfully then you don’t need to worry too much about this, but I can imagine the false positives being annoying when you’re working in your IDE.

The cout is ambiguous vscode mistake usually affects scripts and documents when no good string, or the declared variable called cout is unresponsive. As a result, your computer confirms the system is ambiguous C++ and blocks your project, although nothing looks wrong with the syntax.cout is ambiguous

Therefore, readers can explore this guide because it explains why the reference to variable is ambiguous C++ and how to debug it without causing further complications. Moreover, this article includes several chapters that reproduce the error and pinpoint how the reference to class is ambiguous C++, so keep reading for more.

Contents

  • Why the Cout Is Ambiguous? Causes Of The Problem
    • – The Couts and Cins Are Ambiguous
    • – Porting Functions From Java to C++
    • – The Full Invalid Message
  • The Cout Is Ambiguous Will Disappear: The Ultimate Solution
  • Conclusion

Why the Cout Is Ambiguous? Causes Of The Problem

The reference to max is ambiguous when your C++ document or function does not have a correct string or the declared variable called cout needs to be more responsive. Consequently, this forces the system and your project to fail, so developers must fix it to continue coding.

In addition, your program may state the fprintf is ambiguous due to an exact cause, which is the primary command in most advanced applications. This mistake also confirms that working in C++ is sometimes challenging, especially for less experienced developers because they face unexpected obstacles that ruin their programming experience. Although the cout C++ error is less typical with modern programs, our experts confirmed it affects Visual Studio 2013 IDE. Consequently, developers should locate the mistake and apply the most advanced debugging methods.

However, you must prepare to learn how to fix the error because you must discover the two possible culprits that launch this mistake in your document. In addition, the examples include complex commands and functions to prove no script is safe from this bug, no matter how long or short it is. So, let us discover the first cause that launches this mistake in the following chapter.

– The Couts and Cins Are Ambiguous

Our experts provide a detailed example that is incorrect because the couts and cins are ambiguous, which is confirmed by the invalid message. However, we will not include the complete syntax because our experts will focus on the elements and values that provoke this bug.

You can learn more about the incorrect syntax in the following example:

//—–MAIN MENU(BEGIN)—–

system(“CLS”);

if (Player.l_a == “Damaged”)

{

cout << “Your Left Arm is damaged! Sleep for a while to fix it!”;

Sleep(1600);

}

if (Player.r_a == “Damaged”)

{

cout << “Your Right Arm is damaged! Sleep for a while to fix it!”;

Sleep(1600);

}

if (Player.l_l == “Damaged”)

{

cout << “Your Left Leg is damaged! Sleep for a while to fix it!”;

Sleep(1600);

}

if (Player.r_l == “Damaged”)

{

cout << “Your Right Leg is damaged! Sleep for a while to fix it!”;

Sleep(1600);

}

if (Player.money >= 1000 && Player.rank == “Private 2”)

{

system(“CLS”);

cout << “You have been promoted to Private First Class!”;

Player.rank = “Private First Class”;

Sleep(1600);

}

if (mmenuch1 == 2)

{

system(“CLS”);

cout << “You sleep, restoring your energy.”;

Player.energy = 100;

if (Player.l_a == “Damaged”)

{

cout << “nnYour Left Arm was healed.”;

Player.l_a = “Normal”;

}

if (Player.l_l == “Damaged”)

{

cout << “nnYour Left Leg was healed.”;

Player.l_l = “Normal”;

}

if (Player.r_l == “Damaged”)

{

cout << “Your Right Leg was healed.”;

}

Unfortunately, there are other causes for this error, as you will learn in the following example.

– Porting Functions From Java to C++

Another typical culprit for this mistake is porting functions from Java to C++, which is easy. However, developers face some string obstacles because their script has a few invalid public classes. In addition, the values inside the types appear correct, which confuses developers.Cout Is Ambiguous Causes

Let us learn about this code in the following example:

#include <iostream>

#include <string>

using namespace std;

class Bank {

public:

Bank();

~Bank() {}

int addCred(int addCred) {

currCred += addCred;

totalCred += addCred;

return currCred;

}

int giveChange(int deductCred){

currCred -= deductCred;

totalCred -= deductCred;

return currCred;

}

void updateCred(int cred) { currCred = cred; }

int getCred() { return currCred; }

private:

int currCred;

int totalCred;

};

Bank::Bank() {

currCred = 0;

totalCred = 2000;

}

class Display {

public:

Display();

~Display() {}

void showDisplay(string displayText) {

text = displayText;

cout<<“DISPLAY:: ” <<text;

}

void clear() {

text = “READY”;

showDisplay(text);

}

private:

string text;

};

Keypad::Keypad() {}

class Product{

public:

Product(const char* itsName = “”, int itsPrice = 0):

name(itsName), price(0)

{}

// normal constructor

Product(const string& itsName, int itsPrice):

name(itsName), price(itsPrice) {}

const string& getName() const {return name;}

int getPrice() const {return price;}

private:

string name;

int price;

};

void main() {

//Create objects needed

Tray* tList = tray_prod_init();

Bank* moneyBag = new Bank();

Display* display = new Display();

Keypad* keypad = new Keypad();

}

As you can tell, both examples have many values and launch the mistake. Therefore, before learning the solutions, let us learn about the exception message.

– The Full Invalid Message

Invalid messages and exceptions usually contain more code lines that pinpoint the exact cause and help developers debug the syntax. As a result, we included this section before the solution chapters to help you understand where and why the code fails. First, we will provide the exception from the previous example.

The following example includes the incorrect warning:

C2872: ‘cout’ : ambiguous symbol

C2679: binary ‘<<‘ : no operator defined which takes a right-hand operand of type ‘class std::basic_string<char,struct std::char_traits<char>,class std::allocator

<char> >’ (or there is no acceptable conversion)

This code confirms the affected code lines and why the binary values fail. Consequently, programmers and developers can use this message to pinpoint invalid processes and apply the solution. So, let us discover the best solution for this error in the following chapter. It is full-proof and will not mess up other functions and commands in your code.

The Cout Is Ambiguous Will Disappear: The Ultimate Solution

Removing the included string from your document is the best way to remove this error from your program. For instance, according to our experience, deleting the std library because some values might act as false positives, especially when working with IDE.



This procedure debugs the program because a fully functional document can only have a single std library, although a C header has the same purpose. Fortunately, developers can remove another command to debug the script.

Namely, your primary document must never have two variables with identical names maintained by the for a loop. So, deleting this variable from the second instance will make you forget about the mistake. But how does this look in practice?

The following example explains two incorrect variables:

int i; // incrementor in for loop

while ( … ) {

if ( … ) {

for (int i = 1; i <= 3; i++) {

}

if (i == 4) {

As you can see, we omitted the unnecessary values and properties so that we could focus on the variable on the first and sixth code lines. Now, delete the int command from the sixth line, as shown here:Removing the included string

int i; // incrementor in for loop

while ( … ) {

if ( … ) {

for (i = 1; i <= 3; i++) {

}

if (i == 4) {

This is the easiest way to debug your syntax because your code does not have complex changes or code alterations.

Conclusion

The reference to a variable is ambiguous when your C++ document or function does not have a correct string or the declared variable called cout is unresponsive. Henceforth, deleting some commands is vital to enable your program, as explained and summarized in the following bullet list:

  • Pinpointing the ambiguous error in your code is challenging because the script looks full-proof and functional
  • Although this bug does not affect modern programs and applications, our experts confirmed it with Visual Studio 2013 IDE
  • The bug affects your couts and cins, as explained in this article’s introductory example code
  • Although porting functions from Java to C++ is easy, the program may launch this mistake due to invalid values
  • The solutions are simple and require deleting some commands, such as the include std string or the second variable

The unexpected ambiguous cout error should not discourage beginners as the solutions are easy to implement. So, gear up and start scanning your code and delete the variables that confuse the processes.

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

  • Forum
  • General C++ Programming
  • Cout is ambiguous?

Cout is ambiguous?

What does that mean? How do I fix it?

Post the actual error message, as well as the line of code which is causing the error.

Post your code so that it is [code]between code tags[/code] as this will give it syntax highlighting and line numbers.

Please copy and paste the exact error message, and indicate which line the error is talking about.

Last edited on

Well, I am working on this bank database project and whenever I post this part:

void addaccount()
{
ifstream inData;
ofstream outData;
string fileName;
string str;
cout << «Insert the name of the data file: «;
cin >> fileName;
inData.open(fileName); // Access the file of the name you had inputted
string lastName, firstName, type;
double balance;
inData >> lastName >> firstName >> balance >> type;
cout << «What file would you like to save the data in?: «;
cin >> fileName;
outData.open(fileName);
outData << str;
cout << «nEnter the customer’s last name:»;
cin >> lastName;
cout << «nnEnter the customer’s first name : «;
cin >> firstName;
cin.ignore();
cin.getline(50, firstName);
cout << «nEnter Type of The account (C/S) : «;
cin >> type;
type = toupper(type);
cout << «nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : «;
cin >> deposit;
cout << «nnnAccount Created.»;
}

void deleteaccount();
{
account ac;
ifstream inFile;
ofstream outFile;
inFile.open(«account.dat», ios::binary);
if (!inFile)
{
cout << «File could not be open !! Press any Key…»;
return;
}
outFile.open(«Temp.dat», ios::binary);
inFile.seekg(0, ios::beg);
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
if (ac.retacno() != n)
{
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
}
}
inFile.close();
outFile.close();
remove(«account.txt»);
rename(«Temp.txt», «account.txt»);
cout << «nntRecord Deleted ..»;
}

void search();
{
int seqSearch(const int list[], int listLength, int searchItem)
{
int loc;
bool found = false;

for (loc = 0; loc < listLength; loc++)
if (list[loc] == searchItem)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
}

into it, all the couts get the error message IntelliSense: «cout» is ambiguous.

Speaking of which, I really need help with that. I have a topic called ‘Bank Database’ I could really use help with.

IntelliSense errors aren’t compilation errors. Sometimes IntelliSense can get confused or out of date.

Go to Build -> Rebuild

You may also consider trying to prefix your ‘cout’ object with its namespace, ‘std’. In other words, wherever you’ve put

cout << "Information";

You may consider putting

std::cout << "Information";

Perhaps there are multiple objects called ‘cout’ in your program (or you’ve included external libraries which also have a cout object) and providing a namespace might give some clarification as to which one you mean.

Topic archived. No new replies allowed.

Hi! I’m currently taking an introduction to C++ course and we just learned about for loops and how to use them. As practice, I took an example program I wrote when learning about counter-controlled loops and rewrote it to use a for loop statement instead of a while loop statement.

Unexpectedly, it started giving me the error ‘cout is ambiguous,’ which I haven’t seen for nor does looking it up tell me why this is happening in my case. The program still works when running it though, so I’m not entirely worried, but I would like to know why it’s popping up when all I did was change the kind of loop statement it’s in.

I’m using Visual Studio 2019 Community as my compiler

Code: https://ideone.com/kYVmAG

What’s more strange is that I did rewrite the program in a separate solution file and it didn’t give me ‘cout is ambiguous’ then, which makes me want to know why it’s giving me this error now even more.

#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.

#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] != '';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] != '';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 << "nn";
    quicksort(field, n);
    cout << "n";
    for(int i = 0;i<n;i++){
        cout << field[i] << " ";
    }
    delete [] field;
} 

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

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

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

  • Counter strike source ошибка создания временного файла
  • Counter strike source ошибка msvcr100 dll
  • Counter strike source ошибка 0xc000007b
  • Counter strike source выдает ошибку
  • Counter strike hl exe ошибка приложения