Jump to content
357mag

Is there a way to make this work?

Recommended Posts

Cool program which I like using a global variable. But the compiler says reference to count is ambiguous. It is one of Herb's programs, and I know his style of writing program is suspect, but his programs themselves as far as his ideas I have always liked.

 

Here it is:

 

#include<iostream>
using namespace std;

void functionOne();
void functionTwo();

int count;

int main()
{
    int i;

    for(i = 0; i < 10; i++)
    {
        count = i * 2; // reference to count is ambiguous
        functionOne();
    }

    system("pause");
    return 0;
}

void functionOne()
{
    cout << "count: " << count;
    cout << endl;
    functionTwo();
}

void functionTwo()
{
    int count;

    for(count = 0; count < 3; count++)
        cout << '.';
}

 

Share this post


Link to post

Stay away from 'using namespace std;' !!!

https://stackoverflow.com/questions/1452721/whats-the-problem-with-using-namespace-std

 

Your count identifier is likely conflicting with std::count().  The compiler doesn't know which one you want.

 

Consider using this instead:

//using namespace std;
using std::cout;
using std::endl;
using std::system;

Or, put your count global variable in its own namespace.

 

(Also, you are missing #include <cstdlib> for std::system())

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×