【问题标题】:Can't include iostream in C using MS Visual C++?不能使用 MS Visual C++ 在 C 中包含 iostream?
【发布时间】:2012-04-20 22:40:00
【问题描述】:

我一直在尝试学习 C,但我一直坚持包含库。我需要使用 strcpy(),但该方法包含在 iostream 库中,但是每当我尝试包含该库时,程序都会给我错误。我试过使用 "iostream", "iostream.h", , ,但它要么给我一个 "can't find iostream.h" 错误,要么程序超过 100 个错误并崩溃。即使我的代码是空的,我仍然会得到同样的结果。代码如下:

#include "iostream"

int main(void)
{
}

是的,就是这么多已经让它崩溃了。这是我遇到的部分错误(永远无法将它们全部粘贴在这里):

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2061: syntax error : identifier 'abs'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(37): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'acos'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2061: syntax error : identifier 'asin'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(39): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'atan2'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2061: syntax error : identifier 'ceil'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(40): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): error C2061: syntax error : identifier 'cos'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(41): fatal error C1003: error count exceeds 100; stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

是的,它甚至超过了 100 个错误,并且程序停止计数。我不明白为什么,我只是包括一个普通图书馆。有没有等价的 strcpy()?我主要是想这样使用它(练习):

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "iostream"

int main(void)
{
    struct person 
    { 
        int id; 
        char name[50];
        int age;
    }; 

    struct person p1;

    p1.id = 5595; 
    strcpy(p1.name, "Myname");
    p1.age = 18;

    printf("%d%s%d", p1.id, p1.name, p1.age);
}

【问题讨论】:

  • 请注意,崩溃和报告错误之间存在区别:] 通常编译器会报告错误,但很少会崩溃

标签: c struct iostream strcpy


【解决方案1】:

<iostream> 是 C++ 标头(顾名思义,它处理输入/输出流)。如果你想要strcpy,你需要<string.h>

【讨论】:

  • @user1007059 - 请标记 Oli Charlesworth 的帖子“已解决”;)
  • @paulsm4,正在尝试,但它一直告诉我等待 10 分钟 ^^ 但现在我做到了:D
【解决方案2】:

如果您的源文件是“.c”,您只需将其重命名为“.cpp”即可。

然后它将编译为 C++,您将拥有 C++ 标头,并且您将能够使用 C++ 流。

不过,我认为不需要 iostream。

Strcpy 和朋友在“<string.h>”中。只需包括它和“stdio.h”(就像你正在做的那样);删除“iostreams”#include ...生活应该很好。

【讨论】:

    【解决方案3】:

    iostreams 是仅限 C++ 的功能。 iostreams 头文件是用 C++ 编写的,而不是 C。(是的,它们是不同的语言!)大概是在 C 模式下调用编译器,所以当编译器查看头文件时,它当然会抛出很多错误,因为iostream 中使用的许多构造仅在 C++ 模式下才有意义。

    如果您想使用iostreams,您必须在 C++ 模式下编译(并相应地以适当的现代 C++ 编写代码),使用不同的纯 C 库或通过根据需要实现自己的代码来解决它.

    在这种情况下,您显然想要做的就是使用strcpy()。这是在string.h 中声明的,而不是iostream。 (string.h 是 C 头文件。)只需 #include <string.h>,它应该可以编译。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-23
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 2023-04-09
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多