CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2002
    Posts
    61

    namespace issues

    I'm having a problem resolving some namespace isses.

    in my project I have:
    main.cpp: #include <iostream.h>
    main.cpp: #include "timestamp.h"

    timestamp.h: #include "timelist.h"
    timestamp.h: #include <fstream.h>

    timelist.h: #include <list>

    the issue is I'm getting "ambiguous operators" in timestamp.h when I declare ofstreams and ifstreams. I tried changing list to list.h but I get several compile problems (likewise with changing iostream.h to iostream)

    also adding "using namespace std;" or declaring them as std:fstream is not working.

    Am I using the wrong namespace or is there a different method of fixing this?

    I can't help but feel like I'm running some sort of "hack job" and that there's a better way to all of this.

    thanks,
    Devan

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    You should not mix the deprecated header files (those ending with '.h') with the new-style header files (those without '.h').

    Almost all compilers, even those complying with ISO standard, allow the use of the traditional header files (like iostream.h, stdlib.h, etc). Nevertheless, the ISO standard has completely redesigned these libraries taking advantage of the template feature and following the rule to declare all the functions and variables under the namespace 'std'.

    The standard has specified new names for these "header" files, basically using the same name for C++ specific files, but without the ending '.h'. For example, 'iostream.h' becomes 'iostream'.

    If you use the ISO-C++ compliant include files you have to bear in mind that all the functions, classes and objects will be declared under the 'std' namespace.

    The following shows you the four different methods to map a namespace...
    Code:
    // Using the full member name, including the namespace it belongs to:
    std::cout << "Hello World";
    
    
    // By taking advantage of Using-Declarations:
    using std::cout;                             // This declares cout in the current
                                                 // scope as synonym for std::cout
    cout << "Hello World";
    
    
    // By taking advantage of Using-Directives:
    using namespace std;                         // Which specifies that the current
                                                 // scope can refer to names in the
                                                 // 'std' namespace without using
                                                 // full qualifiers. This is mostly
                                                 // used when porting legacy code.
    cout << "Hello World";
    
    
    // Using aliases:
    namespace X
    {
      namespace Y
      {
        class Z { ... };
      }
    }
    
    X::Y::Z                                      // The full qualifier for 'Z' is
                                                 // 'X::Y::Z'
    
    namespace w = X::Y;                          // This declares 'w' as an alias for
                                                 // namespace 'X::Y'
    
    w::Z                                         // Access 'Z' using 'w::Z'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured