【问题标题】:Compiler error: memset was not declared in this scope编译器错误:memset 未在此范围内声明
【发布时间】:2011-01-31 02:42:49
【问题描述】:

我正在尝试在 Ubuntu 9.10 (gcc 4.4.1) 中编译我的 C 程序。

我收到此错误:

Rect.cpp:344: error: ‘memset’ was not declared in this scope

但问题是我已经包含在我的 cpp 文件中:

#include <stdio.h>
#include <stdlib.h>

同样的程序在 Ubuntu 8.04 (gcc 4.2.4) 下编译良好。

请告诉我我错过了什么。

【问题讨论】:

  • 我仍然不明白的是为什么它在旧版本的 gcc 和 ubuntu 上编译得很好,而不是在新版本上。谁能解释一下为什么?
  • @Dennis:可能是因为必要的标头 &lt;string.h&gt;#included 间接通过旧 gcc/Ubuntu 配置中的其他 #included 标头。
  • @Dennis 在 gcc 选项中添加 -fpermissive 也可能对那些奇怪的情况有所帮助,尽管这只是一种解决方法

标签: c++ gcc


【解决方案1】:

您应该包含&lt;string.h&gt;(或它的C++ 等价物&lt;cstring&gt;)。

【讨论】:

    【解决方案2】:

    无论你遇到这样的问题,只要转到有问题的man page for the function,它就会告诉你缺少什么标题,例如

    $ man memset
    
    MEMSET(3)                BSD Library Functions Manual                MEMSET(3)
    
    NAME
         memset -- fill a byte string with a byte value
    
    LIBRARY
         Standard C Library (libc, -lc)
    
    SYNOPSIS
         #include <string.h>
    
         void *
         memset(void *b, int c, size_t len);
    

    请注意,对于 C++,通常最好使用适当的等效 C++ 头文件 &lt;cstring&gt;/&lt;cstdio&gt;/&lt;cstdlib&gt;/etc,而不是 C 的 &lt;string.h&gt;/&lt;stdio.h&gt;/&lt;stdlib.h&gt;/etc。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多