标签: function-declaration

如何防止错误:这个旧式函数

我正在学习教程,我的代码看起来很正常,但我收到一条消息,上面写着

This old-style function definition is not preceded by a prototype
Run Code Online (Sandbox Code Playgroud)

代码.c:

void viderBuffer()
{
    int c = 0;
    while (c != '\n' && c != EOF)
    {
        c = getchar();
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。对不起,如果我的帖子不完美,我是新来的。

c function-prototypes function-declaration parameter-list

8
推荐指数
1
解决办法
270
查看次数

替代函数语法差异

这两个功能有什么区别?

auto func(int a, int b) -> int;

int func(int a, int b);
Run Code Online (Sandbox Code Playgroud)

c++ declaration function-declaration c++11

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

命名空间内的本地函数声明

在这种情况下

namespace n {
    void f() {
        void another_function();
    }
}
Run Code Online (Sandbox Code Playgroud)

该函数another_function应该在命名空间内n还是外部定义?VS 2012(11月CTP)表示应该在外面,Mac上的GCC 4.7.2表示应该在里面.如果我做错了,我会从链接器中得到未定义的符号错误.

我一般都相信GCC更符合标准,但这是C++,你永远不能确定.

c++ namespaces function-declaration

7
推荐指数
1
解决办法
809
查看次数

静态内联 vs 内联静态

我注意到两者都有效,在这里使用内联的正确方法是什么?

static inline int getAreaIndex()
Run Code Online (Sandbox Code Playgroud)

或者

inline static int getAreaIndex()
Run Code Online (Sandbox Code Playgroud)

另外,getAreaIndex 包含一个大循环。有时我只调用一个,有时我通过循环调用它,我应该内联它吗?(它有 10 行高)

c static inline c99 function-declaration

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

请帮我理解带参数的C++参数声明

我每天都使用ROOT C++库(root.cern.ch),并在遇到此函数声明时浏览源代码:

TString TString::Format(const char *va_(fmt), ...)
{
    //etc.
Run Code Online (Sandbox Code Playgroud)

它可以在这里找到.

我不明白如何const char *在名称中有一个参数或括号.该表达式va_(fmt)稍后用作简单表达,const char *即使它看起来像函数调用或构造函数.起初我以为它与变量参数列表有关,这对我来说也是新的,但阅读stdarg.h上的文档根本没有帮助解决这个问题.

谷歌寻求帮助非常困难,因为我不确定该怎么称呼它.带参数的声明?这没有给出任何好结果.

我曾经认为我认识C++,但这里发生了什么?所有帮助将不胜感激.

c++ syntax arguments function-declaration

6
推荐指数
1
解决办法
204
查看次数

函数声明或函数表达式

我在块范围中定义函数时遇到了问题.考虑以下程序:

try {
    greet();

    function greet() {
        alert("Merry Christmas!");
    }
} catch (error) {
    alert(error);
}
Run Code Online (Sandbox Code Playgroud)

我希望这个程序能够提醒Merry Christmas!.但是在Firefox中给了我以下内容ReferenceError:

ReferenceError: greet is not defined
Run Code Online (Sandbox Code Playgroud)

在Opera和Chrome上,它会像我预期的那样提醒问候语.

显然,Firefox会将块范围内的功能视为一段FunctionExpression时间,而Opera和Chrome将其视为一个FunctionDeclaration.

我的问题是为什么Firefox表现不同?哪种实现更符合逻辑?哪一个符合标准?

我理解JavaScript中的声明是悬而未决的,因此如果在同一范围内的两个或多个不同的块中声明相同的函数,那么就会出现名称冲突.

但是,每次声明函数时重新声明函数都不是更合乎逻辑,这样你就可以做到这样的事情:

greet(); // Merry Christmas!

function greet() {
    alert("Merry Christmas!");
}

greet(); // Happy New Year!

function greet() {
    alert("Happy New Year!");
}
Run Code Online (Sandbox Code Playgroud)

我认为除了解决上面描述的块范围问题之外,这将非常有用.

javascript scope function-declaration function-expression

6
推荐指数
1
解决办法
185
查看次数

模块模式中的函数声明与函数表达

我刚刚了解了函数声明和函数表达式之间的区别.这让我想知道我是否在AngularJS代码中做得对.我正在遵循John Papa使用的模式,但现在它似乎与模块模式的典型JS方法不一致.John Papa在他的控制器和服务中大量使用嵌套的函数声明.这不好吗?

有没有理由支持这个:

var foo = (function() {
    var bar = function() { /* do stuff */ };
    return {
       bar : bar
    };
}());

foo.bar();
Run Code Online (Sandbox Code Playgroud)

对此:

var foo = (function() {
    return {
       bar : bar
    };

    function bar() { /* do stuff */ };
}());

foo.bar();
Run Code Online (Sandbox Code Playgroud)

我主要是一名C#开发人员,仍然习惯于JavaScript的所有细微差别.我更喜欢后一种方法,因为IIFE中的所有功能都是私有的,顶部的揭示模块模式实际上是公共部分.在C#类中,我总是在私有支持函数之前拥有我的公共属性和方法.但是,我意识到它在JS世界中可能不那么干脆.

使用后一种方法有哪些隐患(如果有的话)?

javascript module-pattern function-declaration function-expression iife

6
推荐指数
1
解决办法
736
查看次数

为什么 ++str 和 str+1 有效而 str++ 无效?

我知道这里有一些关于 p++、++p 和 p+1 之间区别的解释,但我还不能清楚地理解它,尤其是当它不使用该函数时:

void replace(char * str, char c1, char c2){

    if (*str == '\0') {
        return;
    }else if (*str == c1) {
        printf("%c", c2);
    }
    else {
        printf("%c", *str);
    }

    replace(++str, c1, c2);
}
Run Code Online (Sandbox Code Playgroud)

当我这样做replace(++str, c1, c2);replace(str+1, c1, c2);它有效时,但replace(str++, c1, c2);没有。为什么?

c recursion c-strings post-increment function-declaration

6
推荐指数
1
解决办法
333
查看次数

必须声明函数的类型吗?

关于 Haskell 我不完全理解的一件事是声明函数及其类型:这是您必须做的事情还是只是为了清晰起见而应该做的事情?还是在某些情况下您需要这样做,而不是全部?

haskell declaration function function-declaration

6
推荐指数
1
解决办法
1163
查看次数

为什么声明带参数和不带参数的同一个函数不会导致编译错误?

对于代码:

int hi(int);
int hi();
int main()
{
    hi(3);
}
Run Code Online (Sandbox Code Playgroud)

我没有收到任何编译错误(调用hi(); 不带参数确实会出现编译错误)。

我预计编译器会抱怨该函数已经以不同的方式声明了。知道为什么会出现这种行为吗?

c compiler-warnings integer-promotion function-declaration

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