Little C++ question

This is a discussion on Little C++ question within the C++ Programming forums, part of the General Programming Boards category; I've just migrated to c++ from java. Just a little question here... I am using Borland c++ 5.02 to compile ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    31

    Talking Little C++ question

    I've just migrated to c++ from java. Just a little question here...
    I am using Borland c++ 5.02 to compile my stuff, and when I type
    std::cout << std::endl;
    as stated in my book it does not compile.
    but if I type
    cout << endl;
    it does... what is the std thing and how come I don't need to type it in Borland? I know this is pretty basic but am just curious.

    Pier.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    std is the standard namespace, where the cout and cin and the other functions that are found in the iostream header file are found. The '::' is the scope resolution operator which lets the compiler know that the function to the right is found within the namespace,class,struct, to the right( it brings the object into scope). The reason you do not need to type this is most likely because the header file you are using already brings the namespace into scope. For further explainations just search for namespace and scope on this board. You could even try the tutorials.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Are there namespaces in Java? STD is a standard namespace. A namespace is simply a structure(if even considered one) used for organization. It stores variables in a common area.

    Code:
    #include <iostream>
    
    namespace foo
    { int endl=5;}
    
    int main()
    { std::cout<<std::endl;//will output a new line
       std::cout<<foo::endl;//will output 5
       return 0;
    }
    P.S. Your problem is probably that you included .h after iostream. This is the older header file convention and is still widely used. You have to either drop the .h and use std:: (could also put using namespace std; right below your header files. if you did, you wouldn't need the std: or keep the .h after the header files.

    P.S.S. As you get more advanced, you will learn that some with the .h and without the .h differ. An example is the string header file. So be careful on which one u use. (iostream doesn't have this problem)

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    The problem is the code below is all I typed!
    I didn't use any header file, or did borland c++ automatically use it? If so, how do I disable it? Even when I remove std:: it says that I should use it and that it compiles so that older code can still be used(backward compatibility).
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
    	{
       const std::string s = "a string";
       std::cout << s << std::endl;
       {const std::string s = "another string";
       std::cout << s << std::endl; }
       }
       std::string name;
       std::cin >> name;
       return 0;
    }
    but it gives me the following errors
    Info :Compiling C:\DOCUMENTS AND SETTINGS\PIER\MY DOCUMENTS\ACCC++\noname00.cpp
    Warn : string.h(549,3):Functions containing for are not expanded inline
    Warn : string.h(557,3):Functions containing while are not expanded inline
    Warn : string.h(563,3):Functions containing for are not expanded inline
    Warn : string.h(575,3):Functions containing for are not expanded inline
    Warn : string.cc(686,32):Comparing signed and unsigned values
    Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
    Error: noname00.cpp(7,13):'cout' is not a member of 'std'
    Error: noname00.cpp(7,32):'endl' is not a member of 'std'
    Error: noname00.cpp(9,13):'cout' is not a member of 'std'
    Error: noname00.cpp(9,32):'endl' is not a member of 'std'
    Error: noname00.cpp(12,12):'cin' is not a member of 'std'

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    When I take off all the std:: , the following error messages appear but the program compiles and runs...

    Info :Compiling C:\DOCUMENTS AND SETTINGS\PIER\MY DOCUMENTS\ACCC++\noname00.cpp
    Warn : string.h(549,3):Functions containing for are not expanded inline
    Warn : string.h(557,3):Functions containing while are not expanded inline
    Warn : string.h(563,3):Functions containing for are not expanded inline
    Warn : string.h(575,3):Functions containing for are not expanded inline
    Warn : string.cc(686,32):Comparing signed and unsigned values
    Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
    Warn : noname00.cpp(6,16):Use qualified name to access member type 'std::string'
    Warn : noname00.cpp(8,17):Use qualified name to access member type 'std::string'
    Warn : noname00.cpp(11,16):Use qualified name to access member type 'std::string'
    Info :Linking C:\DOCUMENTS AND SETTINGS\PIER\MY DOCUMENTS\ACCC++\NONAME00.exe

  6. #6
    D3T
    D3T is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    dunno..

    maybe you could try using
    Code:
    #include <iostream.h>
    instead of just <iostream>

    Havent tried this but still.

    cin, cout, cerr etc are already in iostream.h , and so you prolly dont need to use
    Code:
    std:cout << "text";
    
    /* Instead */
    cout << "text";
    which is what i use and works OK

    Yours.
    [D3T]
    Borland Turbo C++ 3.0 For Dos

    *
    do { war(); } while (nations == hostile);

    ... The best way to prevent wars is to have them.

  7. #7
    Unregistered
    Guest
    It appears that the version of your compiler is not namespace compliant. To check, try this:
    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
       cout << "hello world,";
       return 0;
    }
    If your compiler balks or otherwise gives errors, try this:
    Code:
    #include <iostream.h>
    int main()
    {
      cout << "hello world";
      return 0;
    }
    If the second version works and the first version doesn't, then the compiler your are using is not namespace compliant. That would mean you need to use the .h extension behind iostream and you can't use syntax: std::whatever or using namespace whatever. Not all versions of VC++ nor all versions of Borland or any other compilers available out there are namespace compliant. The newest versions from the major vendors should be, but anything else is questionable.

    Note: one explanation I have seen is that compilers that aren't namespace compliant do everything in namespace std without knowing they are doing so. Sort of like our home addresses usually don't have Earth or Milky Way or Galaxy XY0X122 attached to them. depending on your point of view, at some time in the future the additional address information may be important, but for now who cares. Same with namespaces, for most applications they are irrelevant. For bigger, usually commercial type applications with multiple modules, etc. they can be relevant. If your compiler is compliant so be it. Learn the syntax. If not, so be it. It won't be that difficult to learn the additional lingo once you do have a compliant compiler.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    I tried it, and I think Borland c++ 5.2 is not namespace compliant... have ditched it and moved on to dev c++ 4 ... so far no problems

    Pier.

  9. #9
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    Dev-C++ is a good compiler

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Code:
    int main() {
    	{
       const std::string s = "a string";
       std::cout << s << std::endl;
       {const std::string s = "another string";
       std::cout << s << std::endl; }
       }
       std::string name;
       std::cin >> name;
       return 0;
    }
    Why do you have all these wandering curly brackets?
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  11. #11
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    first of all dont use dev c++ 5 beta use 4. cause 5 has bugs cause of incompletion and does not work 1--% good so until the complete defv c++ 5 co,mes out use 4. and also use
    Code:
    cin.get();
    before
    Code:
    return 0;
    so the program doesnt close before u can read wuts goign on. thropugh time you may not need it though
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21