C++编程错误

发布于 2024-08-11 11:58:39 字数 1225 浏览 10 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-08-18 11:58:39

您使用的是哪个版本的 aCC?旧版本使用预标准 STL 实现,将所有内容放入全局命名空间(即不使用 std 命名空间)。

编译时您可能还需要使用 -AA 选项。这告诉编译器使用较新的 2.x 版本的 HP STL 库。

>aCC -AA temp.cpp

它应该始终

<iostream>  

<iostream.h> 

来自该语言的预标准实现,尽管它通常是为了保持与旧代码的向后兼容性而提供的。

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.

千里故人稀 2024-08-18 11:58:39

尝试使用:

#include <iostream>

而不是:

#include <iostream.h>

iostream.h 是一个旧式标头,其中所有函数都在全局命名空间中公开。当然,在这种情况下,使用命名空间 std 可能不起作用,因为 std 命名空间可能不会由 iostream.h 标头(在此编译器中)公开。如上所述,尝试使用#include,这是一个新风格的 C++ 标准库头文件。 (感谢 Shailesh Kumar 的评论!将其包含在答案中)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文