Jump to content

  • Log In with Google      Sign In   
  • Create Account


wtf???


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
26 replies to this topic

#21 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 12 January 2002 - 10:11 AM

Hold it!

Erroneous info is being passed around here. <iostream.h> is the old-style C++ stream I/O header; the new one is <iostream>. You should aim to always use the new header unless you''re just prototyping some code you won''t have to live with in the future.

Having included the new-style header, cin, cout and all other iostream-defined objects and classes are declared under the std namespace. You can import the entire namespace into your working namespace (the top-level unnamed namespace), but that''s a bad idea in the long run.
using namespace std; 

Alternatively, you can import particular entities from a namespace:
using std::cout; 

After this line above, you can refer to cout anywhere in the same file (if it''s a header file, you can refer to cout without prefix in any other files that include it).

The third option is to explicitly qualify the object name every time you use it:
std::cout << "H1!" << std::endl; 

As you can see, endl is also under the std namespace.

Finally, there''s no substitute for a good book and tons of trial and error. If you have more problems, the great folks here at GameDev are only too willing to help.

Happy Hacking!

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!


Sponsor:

#22 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 12 January 2002 - 10:17 AM

You whole project needs to only have one main() function, as others have pointed out. The other .c or .cpp files are used to declare other functions to use with your program.

-------In file1.cpp--------------

#include <iostream>
using namespace std;

void MyOtherFunction(char *str);

int main(int argc, char *argv[])
{
MyOtherFunction("hello";
return 0;
}


-----In file2.cpp---------------

#include <iostream>
using namespace std;

void MyOtherFunction(char *str)
{
printf("%s", str);
return
}


Multiple source files are used only to organize source code and they only build to one program. In the above example you could use the file2.cpp in another project that needs the MyOtherFunction() function.

I hope that clears the mystery behind it.


Oh for the other guys that are confused about std::,

when you include <iostream> function/classes are in "namespaces" and you need to specify "using namespace std" to use classes in that namespace. When you include <iostream.h>, this doesn''t have any namespace and is similar to the older C style headers.

By saying "using namespace std" you tell the compiler that you want to use ALL the classes/functions in that namespace, but if you want to use only a few, you could do "using std::cout;" using std::cin;" and this will let you call cin and cout without typinh "std::cout << "hello"" all the time.

Ok, enough typing for now.




#23 StoNeD_TrOLL   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 January 2002 - 11:52 AM

Heres my little file2.cpp:
#include <iostream>
using namespace std;
void
{
std::cout <<"This dosnt work";
}
i get these errors:
Error : declaration syntax error
ngw.cpp line 4 {

Error : declaration syntax error
ngw.cpp line 5 std::cout <<"This dosnt work";

Error : declaration syntax error
ngw.cpp line 6 }

what is wrong now?!!!

Edited by - StoNeD_TrOLL on January 12, 2002 6:54:05 PM

#24 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 12 January 2002 - 07:19 PM

quote:
Original post by StoNeD_TrOLL
void
{

void is a return type declarator or an indicator that a function takes no parameters. You can not declare a variable of type void, and you can not simply place the void keyword in the middle of nowhere.

Try this:
#include <iostream>
using namespace std;
int main(void)
{
std::cout << "This works." << endl;
return 0;
}

(The C++ standard requires main to have a return type of int).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!


#25 StoNeD_TrOLL   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 January 2002 - 08:37 AM

That didnt work ether.Same errors as before!!

#26 StoNeD_TrOLL   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 12:42 PM

Ha HA! I got it!I put int main_2() in my second file and it worked!!!!


#27 wrenhal   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 January 2002 - 08:15 AM

quote:
Original post by StoNeD_TrOLL
Ha HA! I got it!I put int main_2() in my second file and it worked!!!!



But all that did is avoid the problem. What does main_2 do??? If it''s the same as main then you don''t even need the second file.

BeS
It''s Da BOMB Baby!!!
. o O ~
A little nonsense now and then,
is relished by the wisest men
~ O o .
-- Willy Wonka




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS