C++编程错误
我是 C++ 编程新手。 所以我正在尝试执行一些小程序。 我正在开发 HP-UX ,它有一个编译器,其 可执行文件名为aCC。
我正在尝试执行一个小程序,
#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;
}
当我编译它时,它给我一个错误:
> aCC temp.cpp
Error 697: "temp.cpp", line 2 # Only namespace names are valid here.
using namespace std;
^^^
到底是什么问题? std
是否不被视为 aCC 编译器中的命名空间,或者 aCC 是否存在一些严重的缺陷?
如果我将
更改为
,则会添加更多错误,如下所示。
>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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是哪个版本的 aCC?旧版本使用预标准 STL 实现,将所有内容放入全局命名空间(即不使用 std 命名空间)。
编译时您可能还需要使用 -AA 选项。这告诉编译器使用较新的 2.x 版本的 HP STL 库。
它应该始终
来自该语言的预标准实现,尽管它通常是为了保持与旧代码的向后兼容性而提供的。
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.
And it should always be
is from a pre-standard implementation of the language, though it is usually shipped so as to maintain backwards compatibility with older code.
尝试使用:
而不是:
iostream.h 是一个旧式标头,其中所有函数都在全局命名空间中公开。当然,在这种情况下,使用命名空间 std 可能不起作用,因为 std 命名空间可能不会由 iostream.h 标头(在此编译器中)公开。如上所述,尝试使用#include,这是一个新风格的 C++ 标准库头文件。 (感谢 Shailesh Kumar 的评论!将其包含在答案中)。
Try with:
Instead of:
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).