CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2004
    Location
    Peterborough, Ontario, Canada
    Posts
    35

    Question using directive in headers

    Hi,

    I have been told by someone on this board not to use the using directive in headers (or at least in the std instance) because it would force all .cpp files that included that header to use the std which may not be desirable. This makes sense to me, but I've run into a slight problem.

    My problem is that I am trying to use strings in a class that I have written. I get aproximately 6 errors for every string that I include in my class. The only way I have been able to get these errors to dissappear is to put using namespace std; near the beginning of the header file. This conflicts with what I've been told however, so is this bad to do? Is there another way to use strings inside of my class?

    Code:
    #ifndef AAGOSELI_DVD_CLASS
    #define AAGOSELI_DVD_CLASS
    #include <string>
    using namespace std;
    const int MAX = 30;
    class clsDVD
    {
    private:
     string title;
     char idNumber[MAX];
     //string director;
     //string date;
     static int counter;
    public:
     clsDVD()
     { counter++; };
     int getCount();
     //void getData();
     //void displayData();  // Display all data
     //void displaySpecData(); // Display only specified data
    };
    
    #endif
    Thanks,

    Nakor
    ----
    Indolence is Bliss

    College is one big dissapointment. Where's the challenge? Buried somewhere in all the bull**** classes some dumb company I'll never work for says I have to take?

    C, C++, PHP, Perl, VB, QB(lol)

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: using directive in headers

    Just put std:: before everything from the std namespace (string, cout, etc).

    Code:
    #pragma once
    
    #include <string>
    
    const int MAX = 30;
    class clsDVD
    {
    private:
     std::string title;
     char idNumber[MAX];
     std::string director;
     std::string date;
     static int counter;
    public:
     clsDVD()
     { counter++; };
     int getCount();
     void getData();
     void displayData();  // Display all data
     void displaySpecData(); // Display only specified data
    };
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2004
    Location
    Peterborough, Ontario, Canada
    Posts
    35

    Re: using directive in headers

    Thank you very much. I should have remembered that, but of course I didn't
    ----
    Indolence is Bliss

    College is one big dissapointment. Where's the challenge? Buried somewhere in all the bull**** classes some dumb company I'll never work for says I have to take?

    C, C++, PHP, Perl, VB, QB(lol)

  4. #4
    Join Date
    Jan 2001
    Posts
    588

    Re: using directive in headers

    Alternatively, instead of putting std:: in front of every string object, you could do the following:

    Code:
    using std::string;
    This will allow you to still just use "string" as the type in your header, but it won't pollute the namespace of other files that include it.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: using directive in headers

    And to provide the complete picture...

    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'

  6. #6
    Join Date
    Nov 2004
    Location
    Peterborough, Ontario, Canada
    Posts
    35

    Exclamation Re: using directive in headers

    Ok thanks to you all

    I have another question and it's rather simple really. My teacher showed me how to get past this problem but I've spent the last 25 mins trying to recall how to do it and it's not comming. When you use strings there is a bug where it'll skip the next input you try to do. It had something to do with using getch() but I can't seem to get it to work.

    Code:
     
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <conio.h>
    #include "clsDVD.h"
    using namespace std;
    int clsDVD::counter = 0; 
    int clsDVD::getCount()
    {
     return counter;
    }
    void clsDVD::getData()
    {
     char temp;
     cout << "Please enter the title of the DVD:  ";
     cin >> title;
     temp = static_cast<char>(getchar());
     //temp = getchar();
     cout << endl << "Please enter the DVD's ID number:  ";
     cin >> idNumber;
     cout << endl << "Please enter the DVD's Director:  ";
     cin >> director;
     cout << endl << "Please enter the DVD's release date:  ";
     cin >> date;
    }
    ----
    Indolence is Bliss

    College is one big dissapointment. Where's the challenge? Buried somewhere in all the bull**** classes some dumb company I'll never work for says I have to take?

    C, C++, PHP, Perl, VB, QB(lol)

  7. #7
    Join Date
    Nov 2004
    Location
    Peterborough, Ontario, Canada
    Posts
    35

    Re: using directive in headers

    Never mind I just created a work around for it
    ----
    Indolence is Bliss

    College is one big dissapointment. Where's the challenge? Buried somewhere in all the bull**** classes some dumb company I'll never work for says I have to take?

    C, C++, PHP, Perl, VB, QB(lol)

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