It was mentioned in this lecture that a function or variable, could be in two different spaces in an include (a library or a .h file I am assuming).
That would be namespace, you can define your own namespaces and use them like
namespace Dan
{
std::string GetDan() { return "Dan"; }
}
int main()
{
std::cout << Dan::GetDan();
}
Why would one define the same function or variable two different ways inside the same document of even in separate documents? Why not simply call it something different?
You might not have been the one who wrote that code, e.g. it was defined in a library you are using.
How can there be two different ways for a function such as “cout”, maybe they are differently syntaxed but basically they should be doing the same thing with same outcome no?
So an example I came across recently was over here:
Since the code wasn’t formatted I figured I would just copy and paste the code in VS to make it readable and then since I’m already there, debug it. The problem code being the with for loop, so I just thought to log the count
as it’s quick and dirty.
for (int count = 1; count <= 5; count++);
{
GetGuessAndPrintback();
cout << count << endl;
}
Now the problem is the semi-colon before the braces, so the code in the braces isn’t within the scope of the loop so will only happen once. Though I don’t get the expected error of ““count” undeclared identifier” (meaning it doesn’t know what count is seeing as it’s not in the same scope of the loop) I get a completely different error saying “binary ‘<<’: no operator found which takes a right-hand operand of type ‘overloaded-function’ (or there is no acceptable conversion)”. And as you might have guessed that’s because there’s a function called count in the std namespace.