Why doesn't this work?

I was wondering why can the function PrintIntro(); be executed properly only within the int main() function. Why can’t PrintIntro(); be declared and executed successfully in any other function like the one I used for example?

Hello Alessandro,

The reason you’re unable to call the function named “function” from within main is because function is also defined in the std namespace. The name “function” is ambiguous from within main because the compiler has no explicit way of knowing if you’re trying to call the function named “function” within your file or if you mean to access the function wrapper in the std namespace.

So why is this happening and how do you resolve it? In your case, you’ve specified that you want to include everything defined in the std namespace with the line

using namespace std;

This is one of the pitfalls of using an entire namespace: it can cause name conflicts between what’s in the namespace and what’s in your file. The easiest solution is to rename “function” to anything else, ideally something a bit more specific, but any other valid name will work.

Another solution is to use a form of the using statement that will include only what you intend to use. Here’s an example.

using std::cout;
using std::cin;
using std::endl;
using std::string;

End result you should be able to call the function as is in main like so.

int main()
{
    // Call your function
    function();
    
    // ... other code 
}

I hope this helps. If I’ve misunderstood your question or you want more clarification, please let me know.

Happy Coding :slight_smile:

1 Like

Im still stuck at this point.

#include
#include
using namespace std;

void OpBillboard();

int main()
{
void OpBillboard();

// The cin
string Guess="";
cout << “Guest the Name\n”;
getline(cin, Guess);
cout << endl;

// REPEAT
cout << "answer = " << Guess << endl;

return 0;

}

void OpBillboard()
{
constexpr int WORD_LENGHT = 11;

cout << "Welcome to Cows game Bro\n";
cout << "Can you guess the word" << WORD_LENGHT << endl;
cout << "Letter isogram im thinking of\n";

};

I found the problem. I was putting void inside the funtction insted of just typing “OpBillboard”

Privacy & Terms