相关疑难解决方法(0)

如何使用extern在源文件之间共享变量?

我知道C中的全局变量有时会有extern关键字.什么是extern变量?宣言是什么样的?它的范围是什么?

这与跨源文件共享变量有关,但这是如何工作的?我在哪里用extern

c global-variables extern

942
推荐指数
13
解决办法
67万
查看次数

Eclipse CDT代码分析认为size_t是模糊的

毕竟,它确实在stddef.h和c ++ config.h中定义:

C++的config.h:

namespace std
{
  typedef __SIZE_TYPE__     size_t;
  typedef __PTRDIFF_TYPE__  ptrdiff_t;

#ifdef __GXX_EXPERIMENTAL_CXX0X__
  typedef decltype(nullptr) nullptr_t;
#endif
}
Run Code Online (Sandbox Code Playgroud)

STDDEF.H:

typedef __SIZE_TYPE__ size_t;
Run Code Online (Sandbox Code Playgroud)

因此,当文件执行时using namespace std,Eclipse CDT代码分析会混淆并说符号不明确.我不知道gcc是如何解决这个问题的,但是对于日食代码分析有什么建议吗?

eclipse-cdt

5
推荐指数
1
解决办法
2820
查看次数

为什么不能在C++中重新定义类中的类型名称?

根据C++ Primer一节,7.4.1类型名称是特殊的:

通常,内部作用域可以从外部作用域重新定义名称,即使该名称已在内部作用域中使用过.但是,在类中,如果成员使用外部作用域中的名称并且该名称是类型,则该类可能不会随后重新定义该名称.

因此,例如:

typedef double Money;
class Account {
    public:
        Money balance() { return bal; }
    private:
        typedef double Money;
        Money bal;
};

int main() {
    typedef double Money;
    Money asset;
    typedef double Money;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当你编译上面的例子时,它会抱怨:

a.cc:6:24: error: declaration of ‘typedef double Account::Money’ [-fpermissive]
         typedef double Money;
                        ^
a.cc:1:16: error: changes meaning of ‘Money’ from ‘typedef double Money’ [-fpermissive]
 typedef double Money;
Run Code Online (Sandbox Code Playgroud)

那么为什么我们不能在类中重新定义类型名称,但我们可以在内部范围内吗?


我的编译器版本是g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.
该部分还有一个注释:

虽然重新定义类型名称是错误的,但编译器不需要诊断此错误.即使程序出错,一些编译器也会悄悄地接受这样的代码.

c++ scope class typename

4
推荐指数
1
解决办法
408
查看次数

使用稍后声明的 C 结构体

我想使用尚未定义的 typedef 结构,但它是稍后定义的。有没有类似结构体原型的东西?

文件容器.h

// i would place a sort of struct prototype here
typedef struct 
{
 TheType * the_type;
} Container;
Run Code Online (Sandbox Code Playgroud)

文件 thetype.h

typedef struct {......} TheType;
Run Code Online (Sandbox Code Playgroud)

文件main.c

#include "container.h"
#include "thetype.h"
...
Run Code Online (Sandbox Code Playgroud)

c coding-style typedef forward-declaration

3
推荐指数
2
解决办法
2906
查看次数

包含多次的头文件中的Typedef

基本上,我已经定义并输入了这个结构:

typedef struct{
    void** elements;
    int numElements;
    int itemSize;
    int capacity;
    int dynamicElements;
}array;
Run Code Online (Sandbox Code Playgroud)

我已经编写了动态数组操作函数.但是,我有点问题.在各种函数中,我将此结构作为参数传递.为了模块化代码,我需要在头文件中对这些函数进行原型化(为了允许类型数组的参数,我需要在这些头文件中包含"array.h".)

因此,在包含所有头文件之后,"array.h"头文件已被多次包含.正如所预料的那样,struct类型不止一次被typedef,并导致冲突.

我的问题是:如何在我的头文件中定义这个定义,这样如果多次包含它就不会中断?

c struct typedef header header-files

3
推荐指数
1
解决办法
1万
查看次数

为什么我们不能在文件范围内两次键入一个(未命名的)结构,但是我们可以在没有错误的情况下两次输入"int"两次?

以下代码编译并运行正常:

#include <stdio.h>

typedef int Someint;
typedef int Someint;

int main()
{
    Someint b = 4;
    printf("%d", b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下代码无法编译.它给了我一个错误conflicting types for 'Somestruct'.

#include <stdio.h>

typedef struct
{
    int x;
}
Somestruct;

typedef struct
{
    int x;
}
Somestruct;

int main()
{
    Somestruct b;
    b.x = 4;
    printf("%d", b.x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么我可以typedef 一次type(int在第一个代码中)两次没有错误,但同样的事情失败了另一个type(上面的结构)?这两种情况有什么区别?我正在使用CodeBlocks 12.11附带的MinGW编译器.

c struct typedef

2
推荐指数
2
解决办法
756
查看次数

正确的位置来定义结构和typedef

问题描述

我想知道在C中定义结构以通过其他源文件使用它们的"正确方法".想想以下结构

struct f3{
  double x;
  double y;
  double z;
};
Run Code Online (Sandbox Code Playgroud)

应该在头文件或源文件中声明typedef和结构吗?如果在头文件中,为了符合C软件工程技术,应该在该头文件中包含哪些内容?

到目前为止我做了什么:

我可以将它放入types.h,然后struct f3在其他源文件(#include types.h)中使用,或者它可以放在源文件中type.c.

c struct

2
推荐指数
1
解决办法
117
查看次数

何时使用包括警卫?

我知道在头文件中使用包含警戒是为了防止某些内容被定义两次.但是,使用此代码示例,完全没问题:

foo.c的

#include <stdio.h>
#include <string.h>
#include "bar.h"

int main() {
    printf("%d", strlen("Test String"));
    somefunc("Some test string...");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

bar.h

#ifndef BAR_H_INCLUDED
#define BAR_H_INCLUDED
void somefunc(char str[]);
#endif
Run Code Online (Sandbox Code Playgroud)

bar.c

#include <stdio.h>
#include <string.h>
#include "bar.h"

void somefunc(char str[]) {
    printf("Some string length function: %d", strlen(str));
}
Run Code Online (Sandbox Code Playgroud)

上面的代码片段是用编译的,gcc -Wall foo.c bar.c -o foo没有错误.然而,无论是<stdio.h><string.h>被列入没有包括后卫.将bar.h删除到单个语句时仍然没有错误void somefunc(char str[]);.为什么没有错误?

c macros include-guards header-files c-preprocessor

2
推荐指数
1
解决办法
1133
查看次数