clang通过引用传递变量

时间:2014-08-12 07:56:01

标签: gcc clang

我正在使用clang 3.4.2 ..

编译以下代码
#include <stdio.h>
void haa(int& j){
    j=1;
}
int main(){
    printf("hello\n");
}

这会出现以下错误:

hello.c:3:13: error: expected ')'
void haa(int& j){
        ^
hello.c:3:9: note: to match this '('
void haa(int& j){
    ^
hello.c:3:13: error: parameter name omitted
void haa(int& j){
        ^
hello.c:4:2: error: use of undeclared identifier 'j'
        j=1;
    ^
3 errors generated.

使用gcc编译相同内容不会出现错误或警告...... 有人可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

问题是通过引用传递(使用引用而不是指针)不是c而是c ++特性。

您需要使用c ++编译器(例如g++clang++)编译代码。将文件扩展名更改为.cpp也可以,因为这会告诉编译器将其视为c ++。

相关问题