Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple newbie question..

Reply
Thread Tools

Simple newbie question..

 
 
[ ThrashDK ]
Guest
Posts: n/a
 
      02-18-2005
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 @


 
Reply With Quote
 
 
 
 
SirMike
Guest
Posts: n/a
 
      02-18-2005
[ 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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-18-2005
[ 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.

 
Reply With Quote
 
[ ThrashDK ]
Guest
Posts: n/a
 
      02-18-2005
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 @


 
Reply With Quote
 
[ ThrashDK ]
Guest
Posts: n/a
 
      02-18-2005
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 @


 
Reply With Quote
 
SirMike
Guest
Posts: n/a
 
      02-18-2005
[ ThrashDK ] wrote:
> Is it something the compiler "asumes" or just does
> automatically?

Which compiler do you use ?

--
the code is my strength
SirMike
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      02-18-2005
[ 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.

 
Reply With Quote
 
[ ThrashDK ]
Guest
Posts: n/a
 
      02-18-2005
>> 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 @


 
Reply With Quote
 
SirMike
Guest
Posts: n/a
 
      02-18-2005
[ 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
 
Reply With Quote
 
[ ThrashDK ]
Guest
Posts: n/a
 
      02-18-2005
> 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 @


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple VB.Net / webservice requirement (but not simple for me....) Dave E ASP .Net 7 01-11-2006 02:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
A few simple problems in a simple program. jmac@berkeley.edu C Programming 7 07-23-2003 09:51 PM



Advertisments