|
-
November 23rd, 2004, 11:00 AM
#1
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)
-
November 23rd, 2004, 11:04 AM
#2
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
};
-
November 23rd, 2004, 11:14 AM
#3
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)
-
November 23rd, 2004, 11:46 AM
#4
Re: using directive in headers
Alternatively, instead of putting std:: in front of every string object, you could do the following:
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.
-
November 23rd, 2004, 11:51 AM
#5
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'
-
November 23rd, 2004, 12:14 PM
#6
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)
-
November 23rd, 2004, 01:09 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|