Simple newbie question..

T

[ ThrashDK ]

Hello people,

What is the difference between "cout" and "std::cout"..? As far as I can
see, the do the same thing. But I'm just learning, so I might be wrong.. ;)
 
S

SirMike

What is the difference between "cout" and "std::cout"..? As far as I can
see, the do the same thing. But I'm just learning, so I might be wrong.. ;)
The first one is without namespace and the second one it with namespace.

when you use "cout" you must also write somwhere:
using namespace std;

std::cout you can use anytime, anywhere
 
R

Rolf Magnus

Hello people,

What is the difference between "cout" and "std::cout"..?

cout is the unqualified name, std::cout the qualified one.
As far as I can see, the do the same thing.

They don't:

#include <iostream>

int main()
{
cout << "Hello, ";
std::cout << "world\n";
}

The first cout line should produce an error message because the name cout is
not known in the global namespace. The second line should be ok, because
cout is defined in namespace std.

#include <iostream>

int main()
{
using std::cout;
cout << "Hello, ";
std::cout << "world\n";
}

Now, std::cout is imported from namespace std, so it can be used as simply
cout, too. In this case, cout and std::cout are just to ways to name the
same object.
But I'm just learning, so I might be wrong..
;)

You just have to read up on namespaces.
 
T

[ ThrashDK ]

Hey SirMike,
The first one is without namespace and the second one it with namespace.

when you use "cout" you must also write somwhere:
using namespace std;

std::cout you can use anytime, anywhere

Okay, thanks a lot. ;) Though I'm not sure I follow. I have this little
piece of simple code, quoted below. This works just as good, whether I use
"cout" or "std::cout" - here shown with just "cout". And nowhere is the line
"using namespace std;". Why is this?


-----------------------------------
#include <iostream>

void DemonstrationFunction()
{
cout<<"In function\n";
}

int main()
{
cout<<"In main\n";
DemonstrationFunction();
cout<<"Back in main\n";
return 0;
}
 
T

[ ThrashDK ]

Hey Rolf,

Okay, I understand that if you want to use just "cout" you have to provide
the line "using std::cout;". But I have a little piece of code (see answer
to SirMike), that I tried to play with. When saved, compiled and linked,
they do the same thing, and I have not provided a line that tells that I can
just use "cout".. Why? Is it something the compiler "asumes" or just does
automatically?

I try not to be too pesky, I'll probably see the light soon. ;)
 
R

Rolf Magnus

Okay, thanks a lot. ;) Though I'm not sure I follow. I have this little
piece of simple code, quoted below. This works just as good, whether I use
"cout" or "std::cout" - here shown with just "cout". And nowhere is the
line "using namespace std;". Why is this?


-----------------------------------
#include <iostream>

void DemonstrationFunction()
{
cout<<"In function\n";
}

int main()
{
cout<<"In main\n";
DemonstrationFunction();
cout<<"Back in main\n";
return 0;
}
----------------------------------

A C++ standard compliant compiler is not supposed to accept that, so if
yours does, it's broken.
Some rather old versions of g++ (before 3.x) had such a bug. If you're using
g++ and care for standard compliance, I strongly suggest upgrading to a
more recent version.
 
T

[ ThrashDK ]

Is it something the compiler "asumes"
Which compiler do you use ?

Um, it's rather old, I see.

Bloodshed Dev-C++ v4.01 (Build on: 22-11-2000)

As far as I know, it a development enviroment, with a compiler built-in. I
gues... Is it buggy? It's because I follow a book on C++, and it said to use
that compiler.
 
T

[ ThrashDK ]

The compiler (not IDE) must be different. Try
to download thee newest version of Dev

I downloaded the file you linked to, and installed it. I can see in the
About box that the compiler is different from the one in mine (v4.01 - but
older compiler???). However, the compiler still accepts the code that should
not be accepted. But the resulting .exe file is smaller in size with the new
one, 73728 bytes. When I compile with mine, the older one, the size is
bigger, 156417 bytes. It seems the compiler in the one you linked to, is
more efficient, or what? But I guess that's a whole other matter - there is
still no errors. It compiles, when it apparantly should not..

? ? :-//
 
T

[ ThrashDK ]

But the resulting .exe file is smaller
in size with the new one, 73728 bytes.

I figured out, that this happens when you tick "Generate Debugging
Information" in the linker, then the file is bigger.

And I have now tried it on two different PC's. It compiles with no errors on
both. I have had a look in the different settings, to see if something was
set incorrect, but I'm not sure. It doesn't appear to..
 
S

SirMike

I figured out, that this happens when you tick "Generate Debugging
Information" in the linker, then the file is bigger.
Ofcourse but debugging information are not usable by end-user so you
shouldn't compile it with debugging information when you release the
final program. But it doesn't matter in your problem.
 
R

Rolf Magnus

SirMike said:
Ofcourse but debugging information are not usable by end-user

However, it might be useful for the developer if it gets included in a bug
report from an end user.
 
I

Ingo Nolden

I figured out, that this happens when you tick "Generate Debugging
Information" in the linker, then the file is bigger.

And I have now tried it on two different PC's. It compiles with no errors on
both. I have had a look in the different settings, to see if something was
set incorrect, but I'm not sure. It doesn't appear to..
on my compiler VC++ from version 6.0 to 7.1 if you include

#include <iostream.h>

instead of

#inlcude <iostream>

you will get a version that is wrapped into std namespace.

I don't know if this is releavent, but it has not been mentioned, and
perhaps it may help somehow.

the .h version is actually a pre-standard version as far as I know, as
the iostream functions have been moved to the standard namespace either
by the standard or by the compiler manufacturer.

Ingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top