Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Simple newbie question.. (http://www.velocityreviews.com/forums/t288957-simple-newbie-question.html)

[ ThrashDK ] 02-18-2005 10:05 AM

Simple newbie question..
 
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.. ;)

--
Med venlig hilsen / Best Regards - Brian J. Nielsen
To mail me in private, simply use "email.dk" after the @
For at maile mig privat, brug da "email.dk" efter @



SirMike 02-18-2005 10:14 AM

Re: Simple newbie question..
 
[ ThrashDK ] wrote:
> 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

--
the code is my strength
SirMike

Rolf Magnus 02-18-2005 10:40 AM

Re: Simple newbie question..
 
[ ThrashDK ] wrote:

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


[ ThrashDK ] 02-18-2005 10:52 AM

Re: Simple newbie question..
 
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;
}
----------------------------------

--
Med venlig hilsen / Best Regards - Brian J. Nielsen
To mail me in private, simply use "email.dk" after the @
For at maile mig privat, brug da "email.dk" efter @



[ ThrashDK ] 02-18-2005 11:09 AM

Re: Simple newbie question..
 
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. ;)

--
Med venlig hilsen / Best Regards - Brian J. Nielsen
To mail me in private, simply use "email.dk" after the @
For at maile mig privat, brug da "email.dk" efter @



SirMike 02-18-2005 11:13 AM

Re: Simple newbie question..
 
[ ThrashDK ] wrote:
> Is it something the compiler "asumes" or just does
> automatically?

Which compiler do you use ?

--
the code is my strength
SirMike

Rolf Magnus 02-18-2005 11:25 AM

Re: Simple newbie question..
 
[ ThrashDK ] wrote:

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


[ ThrashDK ] 02-18-2005 12:14 PM

Re: Simple newbie question..
 
>> Is it something the compiler "asumes"
>> or just does automatically?

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

--
Med venlig hilsen / Best Regards - Brian J. Nielsen
To mail me in private, simply use "email.dk" after the @
For at maile mig privat, brug da "email.dk" efter @



SirMike 02-18-2005 12:30 PM

Re: Simple newbie question..
 
[ ThrashDK ] wrote:
> Bloodshed Dev-C++ v4.01 (Build on: 22-11-2000)

I got also Dev-C++ 4 but the code you wrote doesn't work.
The compiler (not IDE) must be different. Try to download thee newest
version of Dev from http://ftp1.sourceforge.net/dev-cpp/devcpp4.zip and
try to compile it.

--
the code is my strength
SirMike

[ ThrashDK ] 02-18-2005 01:07 PM

Re: Simple newbie question..
 
> 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..

? ? :-//

--
Med venlig hilsen / Best Regards - Brian J. Nielsen
To mail me in private, simply use "email.dk" after the @
For at maile mig privat, brug da "email.dk" efter @




All times are GMT. The time now is 09:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.