namespace

Status
Not open for further replies.
It's not a rule, and it's not required. Namespaces provide an additional level of scoping, which helps reduce name clashes. It's design to serve much the same purpose as packages in Java and Ada.

Many purists would say you should never bring an entire namespace into scope, in all but the simplest programs, otherwise what is the point of haviong them at all? I think I tend to agree.
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
Hmm. This has been on my nerves too. In Visual C++ 6.0, I can't include string.h and use the string class without:

using namespace std;

and then, the ".h" gets dropped off of #include <string.h> for anything to work.

It's pissing me off because I have to get my programs to ALSO compile in Borland 4.52 (the prof uses it) and the borland compiler just doens't understand the line "using namespace std;" and I can't even GET the string class to work on the borland compiler, no matter what i do

"and yes, <string.h> is in the /library directory".

So i'm stuck between using strings (but being limited to VC++, which isn't acceptble) or not using strings (I AM NOT GOING TO USE CHAR ARRAYS THE ENTIRE SEMESTER) which is also not acceptable.

-Alpha
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
That's correct. All of the (formerly) standard libraries that end in .h have been deprecated for several years now. String.h is one of a few that dates back to C; many others (e.g. algorithm.h) are definitely C++ era but still not part of the "real" STL. Avoid unless you're maintaining large existing codebases; any recent compiler should prefer the new libraries, if anything (to the extent a compiler "prefers" anything...ok bad word choice, you get the picture).

You definitely don't have to use the std namespace for your programs; chances are, in fact, you shouldn't. (Namespace conflicts with included libraries would be a bitch of a bug to track down if you didn't know what you were dealing with.) For example:
code:<HR>
Code:
#include<string>
#include<iostream><P>int main(void)
{
  std::string myString = "Hello world";
  std::cout << myString << std::endl;
  return 0;
}
 

DrPizza

Well-known member
93,870
Many purists would say you should never bring an entire namespace into scope, in all but the simplest programs, otherwise what is the point of haviong them at all? I think I tend to agree.
I would for all but the <tt>std</tt> namespace, as anyone clashing with that needs their head examined.

The real point of namespaces is so that third-party libraries (whose names are beyond your control) don't clash with each other. That kind of clashing is unavoidable (you can't know what someone else has named their functions and classes). Clashing with <tt>std</tt>, OTOH, is idiotic.
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
In which case, i'm not sure how to force the compiler to give me a string class.

it borks on using namespace std, and if i don't have anything in there, it doens't recognize that string is a data type.

Does this mean i have to type:

code:<HR>
Code:
 std::string myString ;

for every string object that i'd want to make?

-Alpha
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
I'm here at the comp lab, and the compiler is still refusing to recognize the string. Here's a list of the prototype program:
code:<HR>
Code:
#include <iostream.h>
#include <string.h><P>using std::string;<P>int main()
{
  string strung;
  cin>>strung;
  cout<<strung<<endl;
  return 0;
}

The 4 errors i get are:

"Declaration Syntax error" (line 4 (the using std::string) line)
"Undefined symbol strung in function main"(line 8)
"statement missing ; in function main"(line 8)
"Undefined symbol strung in function main"(line 9)

I don't know how to convince this compiler that there is a string class. it's Borland C++ 4.52, and i suspect that any programs i write MUST be compilable on this version because this is the version that the professor uses.

-Alpha
 
I don't know how to convince this compiler that there is a string class. it's Borland C++ 4.52, and i suspect that any programs i write MUST be compilable on this version because this is the version that the professor uses.
That compiler is ancient. It probably doesn't even have namespace support.

I have an old borland compiler which has a string class under "cstring.h". It's not the STL string, but it's better than nothing if you need one. Writing code for old compilers is a pain though :(
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
Where are you going to school? That is the compiler I used when I was learning C++. In 1994/1995. It also predates the standard by quite a few years... Ugh.

Right now, a local community college. I woudl note however, that Microsoft Visual C++ is available to all students via EVERY computer on the network (It's novell (the network that is), but that's a seperate issue :) )

It's just my professor refuses to teach with it. I think he thinks that it's too much overhead to compile our small programs (and i would agree with him on that......i see Visual C++'s strengths more in working with large projects, where a lot of user defined classes are used rather than small, one file cpp files)

At home, I program on visual C++ Introductory (the version that came with the book)

-Alpha
 
It's just my professor refuses to teach with it. I think he thinks that it's too much overhead to compile our small programs
Quite frankly, I think that's a bunch of crap. Opening a .cpp file in the VS IDE and pressing F5, and clicking "yes" a couple times is too difficult? He could just as easily use "cl hw.cpp -o hw.exe" at the command line.

And if he just doesn't like Microsoft, there's no reason for him not to at least use the free version of Borland C++. Teaching C++ as it was circa 1994 is pointless. The language has changed dramatically since that compiler was written.
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
Quite frankly, I think that's a bunch of crap. Opening a .cpp file in the VS IDE and pressing F5, and clicking "yes" a couple times is too difficult? He could just as easily use "cl hw.cpp -o hw.exe" at the command line.

I disagree. I think if you're just going to work with one file, and work with an IDE, then the bunk about workspaces (going from experience in VC++ 6.0 because that's what offered here at the college) is a bit superfluous. Now for large programs with multiple files and many user defined classes and debugging, I agree VC++ is teh win.

You're competing with opining an .cpp file and clicking one button to run here.

That is, IF your'e going to use an IDE. I'd prefer that we used gcc or somesort, If he were to ask for my opinion. After using command lines at Maryland, i prefer them to IDEs, but if i'm going to use an IDE, i'd prefer it to be VC++. For example, yesterday, trying to work between 2 workspaces (one of them was a project, and the other one was a test workspace) was totally too much overhead. Maybe i just haven't learned how to compile and execute different .cpp files within the same workspace. I don't know. I open up the fact that it could be PEBCAK but it's so much simpler for small projects to use commandline or the borland compiler, for me

-Alpha
 

earl

Well-known member
3,066
Um, you don't understand how to use workspaces. First, if you have two projects (a lib and a test), they should both be in the same workspace. And you can set up dependencies for the test project to allow it to automatically build the right version of the lib you are testing. Or whatever...

Second, a workspace and project are the equivalent of a makefile. No matter what you are doing, you should be using them; even a makefile is better than g++ -Wall -c test.cpp; g++ -o test test.o

-earl-
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
Um, you don't understand how to use workspaces.

Okay, i left myself open for that one.

First, if you have two projects (a lib and a test), they should both be in the same workspace. And you can set up dependencies for the test project to allow it to automatically build the right version of the lib you are testing. Or whatever...

Again, I left myself open for not knowing the compiler. But while we're talking about it, let me fully explain the behavior that i'd want to see out of VC++.

On the first project, i created a workspace, Project, with the intension that all of my school Project files would go into the workspace. I then wrote and compiled p1, which went fine. Then, P2 comes around, and i wrote, and attempted to compile it. At which point it compiled p1.cpp and executed it, even though i hadn't edited p1.cpp that time. until today, i still couldn't make it execute. So i made a workspace, p2, and compiled and exectued there.

p1 and p2 are completely separate projects.

Now, what i'm talkinga bout with the test program. Because i can't get another bit of code to execute completely independant of the code around it, i had to create another workspace, test, simply for cases where i want to test syntax....the ohter day i was working with enums i think, and instead of scrapping everything had (i'd already written half the program) i opened the test workspace, wrote a small bit of code, and found it didn't work. so half the time was jumping back and forth between workspaces to try and fix the error, at which point i consulted #programming to which they said what i was trying to do was not possible in C++.

So if you can find a way so that i can complile two completely independant programs/pieces of code, then i'll be happy, and i'll think better of VC++.

-Alpha
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
Hmm. I tried that, and VC++ forced a workspace on me. If i could get around using workspaces for now, i will.

or in the least grab the free borland compiler to use as the back end for the borland ide frontend (I think they are switch compatible).

No, because then i'd have to tell the professor to install it. When i get in class tommorow, i'm going to ask him about all this. I don't like the idea of using a compiler so far behind, that it can't read proper C++ code. I'll see if i can't get him to work out compiling/reading my programs with VC++ in mind.

-Alpha

EDIT: i tried putting a second 'project' (I think i'm mixing up my terminology here. from now on i'll distinguish betwen my school 'project' and VC++ 'project') into one of my workspaces, as earl suggested, and it tried to create another workspace instead. I'll try and fiddle around with that concept somemore though.
 

earl

Well-known member
3,066
As euzeka said, Workspaces contain projects. To create a workspace that has two projects, do this:\
VS6:
File->New workspace
put c:\test in the directory

File->New Project->win32 static library
make project name lib
also make sure that add to current workspace is selected instead of create new workspace

Do the same for a command line executable

On the left, choose FileView tab (not classview)
Right click on your second project, choose Set as Active Project

Go to Project Menu->Dependencies
Check the checkbox by your first project

Now, when you build the second project, it will rebuild the first if you changed anything. When you want to work inside a particular project, make sure to set it as active project.

Hope this helps,

-earl-

PS: note that these projects can be as related (not at all, or dependent on each other) as you want. At work, when I create libs, I often add a second project to the workspace that is a standard exe to allow me to easily test the lib.
 
Status
Not open for further replies.