views:

200

answers:

2

I am new to C++ programming. So I was trying my luck executing some small programs. I am working on HP-UX which has a compiler whose executable is named aCC.

I am trying to execute a small program

#include <iostream.h>
using namespace std;
class myclass {
public:
    int i, j, k; 
};

int main()
{
    myclass a, b;
    a.i = 100; 
    a.j = 4;
    a.k = a.i * a.j;
    b.k = 12; 
    cout << a.k << " " << b.k;
    return 0;
}

When I compile this it gives me an error:

 > aCC temp.cpp
Error 697: "temp.cpp", line 2 # Only namespace names are valid here.
    using namespace std;
                    ^^^

What exactly is the problem? Is std not considered as a namespace in the aCC compiler or is there some serious drawback with aCC?

If I change the <iostream.h> to <iostream>, I get some more errors added as below.

>aCC temp.cpp
Error 112: "temp.cpp", line 1 # Include file <iostream> not found.
    #include <iostream>
             ^^^^^^^^^^
Error 697: "temp.cpp", line 2 # Only namespace names are valid here.
    using namespace std;
                    ^^^
Error 172: "temp.cpp", line 14 # Undeclared variable 'cout'.
    cout << a.k << " " << b.k;
+1  A: 

Try with:

#include <iostream>

Instead of:

#include <iostream.h>

iostream.h is an old style header in which all functions are exposed in global namespace. naturally in such a case, using namespace std may not work since std namespace is probably not exposed by iostream.h header (in this compiler). As explained above, try with # include which is a new style C++ standard library header. (thanks Shailesh Kumar for the comment! included it in the answer).

Pablo Santa Cruz
iostream.h is an old style header in which all functions are exposed in global namespace. naturally in such a case, using namespace std may not work since std namespace is probably not exposed by iostream.h header (in this compiler). As explained above, try with # include <iostream> which is a new style C++ standard library header.
Shailesh Kumar
+9  A: 

Which version of aCC are you using? Older versions used a pre-standard STL implemenntation that put everything in the global namespace (i.e. didn't use the std namespace)

You might also need to use the -AA option when compiling. This tells the compiler to use the newer 2.x version of HP's STL library.

>aCC -AA temp.cpp

And it should always be

<iostream>  

<iostream.h>

is from a pre-standard implementation of the language, though it is usually shipped so as to maintain backwards compatibility with older code.

Glen
Thanks a lot glen.this worked.but by default which version is selected?
cpp_Beginner
You should aim to use standard C++ all the time. So you should always compile with the -AA flag. It's a pain that aCC defaults to the non-standard version, but it can't be helped. One thing to be aware of, all libraries that you link against must also have been compiled with the -AA flag, or you'll run into problems
Glen
thats a very useful information.:) thanks
cpp_Beginner