C++ programing error

1.1k views Asked by At

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;
2

There are 2 answers

2
Glen On BEST ANSWER

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.

1
Pablo Santa Cruz On

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).