繁体   English   中英

奇怪的 C++ 错误,不合格的 ID,重新声明

[英]Weird C++ error, unqualified ID, redeclaration

我想创建一个项目,我有 3 个文件,一个test.cppsomething.h和一个something.cpp 他们来了:

测试.cpp:

#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;

int main(void)
{
    int x;
    register(x);
    return 0;
}

某事.h:

#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>

void register(int x);
#endif

某事.cpp:

#include "something.h"

void register(int x)
{
    std::cout << x << '\n';
}

这是我得到的错误:

In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
 void register(int x);
               ^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: ISO C++ forbids declaration of ‘x’ with no type [-fpermissive]
     register(x);
               ^
test.cpp:10:15: error: redeclaration of ‘int x’
test.cpp:9:9: note: ‘int x’ previously declared here
     int x;
         ^
In file included from something.cpp:1:0:
something.h:5:15: error: expected unqualified-id before ‘int’
 void register(int x);
               ^~~
something.h:5:15: error: expected ‘)’ before ‘int’
something.cpp:3:15: error: expected unqualified-id before ‘int’
 void register(int x)
               ^~~
something.cpp:3:15: error: expected ‘)’ before ‘int’

为什么它告诉我我重新定义了 x? 当我只想用它的值调用寄存器时。

register是 C++ 中的保留字 因此,您必须提供另一个(未保留的)名称。

更多信息: 在 C++ 中注册关键字 - Thinbug

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM