Click to See Complete Forum and Search --> : using directive in headers


Nakor
November 23rd, 2004, 10:00 AM
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?


#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

cilu
November 23rd, 2004, 10:04 AM
Just put std:: before everything from the std namespace (string, cout, etc).


#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
};

Nakor
November 23rd, 2004, 10:14 AM
Thank you very much. I should have remembered that, but of course I didn't :)

Bob Davis
November 23rd, 2004, 10:46 AM
Alternatively, instead of putting std:: in front of every string object, you could do the following:


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.

Andreas Masur
November 23rd, 2004, 10:51 AM
And to provide the complete picture...

The following shows you the four different methods to map a namespace...

// 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'

Nakor
November 23rd, 2004, 11:14 AM
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.


#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;
}

Nakor
November 23rd, 2004, 12:09 PM
Never mind I just created a work around for it :) :thumb: