我注意到以下代码与最近的编译器一起编译:
int main()
{
int x;
struct x;
x = 210; // ←
}
我记得它几年前没有编译。
在C++11或C++14中是否更改了查找规则以使该代码“工作”(从而中断了对struct variable_name;的使用,以确保在下面的代码中不使用变量)?
更新:显然我记错了。我已经验证了即使使用VisualC++ 2010编译的代码也是正确的。但是,当用于参数时,struct名称位于内部作用域和阴影中,如下代码所示:
void foo( int x )
{
st
没有关于好的风格的争论,我只是对标准c++中的合法内容感兴趣。这是一个小例子,在一个更大的代码中出现了一些东西。
Clang和Visual没有错误地编译它,而且它看起来工作得很好。我甚至都没有收到任何警告。
GCC给出了以下错误,甚至拒绝编译代码。
test.cpp:1:8:错误:从“结构测试”-fpermissive中更改“测试”的含义
这到底是法律法规吗?我明白gcc在说什么,但是这个代码实际上是错误的吗?
struct test
{
int data;
};
struct app
{
test test;
};
int main()
{
app my
请看我的示例代码
struct A
{
int member;
};
int main()
{
int A; //Line 1
A b; //Line 2
b.member = int(); //Line 3
}
错误是
prog.cpp: In function ‘int main()’:
prog.cpp:9: error: expected `;' before ‘b’
prog.cpp:9: warning: statement has no effect
prog.cpp:10: error: ‘b’ was not declared in t
有没有办法在蟒蛇身上做这样的事?
from enum import Enum, auto
def some_func(mode = Mode.A):
class Mode(Enum):
A = auto()
print(mode == Mode.A)
期望结果
>>> some_func()
True
上面的片段在NameError: name 'Mode' is not defined中失败,大概是因为当解释器读取def some_func(mode = Mode.A):时没有定义class Mode
当然,你可以把E
enum是用户定义的类型.一般情况下,C中的enum与C++之间没有很大的区别。除了C++中的作用域:如果在函数或类中声明了某些enum,则不能在声明的函数/类之外访问它。这不适用于C。
声明没有什么不同。例如,可以按以下方式声明新的enum (对于C和C++):
enum newEnum { zero = 0, one, two, three };
在定义新变量方面几乎没有区别。要用新定义的类型定义新变量,可以使用以下行:
enum newEnum varA = zero; // though allowed to skip enum keyword in C++
但有一点很有趣。在C++中
这是美国证券交易委员会的报价。3.3.1/4 N3797工作草案:
给定单个声明区域中的一组声明,每个声明区域指定相同的非限定名称,
-确切地说,一项声明应声明一个类名或枚举名称,而该名称或枚举名称不属于类型胡枝子名称,而其他声明均应引用相同的变量或枚举器,或所有声明均指函数和函数模板;
我们可以在一个声明区域中多次声明enum类型名称:
enum A; // declared first time, the other declaration with the same
// unqualified name shall all refer to the same v
我正在做一个项目,在多次搜索标题中的错误后,我几乎迷路了。如果我能得到一些帮助来弄清楚这件事。
class ManagedArray
{
public:
float *elements;
int numberOfElements;
/* default constructor */
ManagedArray() :elements(NULL){};
ManagedArray() :numberOfElements(0){}; <--Where the error is
/*accessor*/
int
我有一个关于在java类中编写多个枚举的问题。我们可以要这样的吗?
你能帮我完成这个吗?
对于Ex:
public final class Test{
public enum Unit{
HORIZONTAL("HORIZONTAL");
}
public enum Code {
COMPANY ("COMPANY");
}
public enum Version{
ONE(1)
}
}
在以下示例中:
class Test
{
public:
Test(int _value) { value = _value; };
const bool operator==(int _value) const { return value == _value; };
private:
int value;
};
int main(void)
{
int a;
a == 1;
Test b(1);
b == 1;
return 0;
}
汇编提供了以下内容:
$ g++ -Wall -pedantic -o test t
以下代码未编译:
#include <iostream>
#include <stdio.h>
int a=5;
char a='a';
int main(){ std::cout << a;}
这是因为:
test.cpp:5:6: error: conflicting declaration ‘char a’
test.cpp:4:5: error: ‘a’ has a previous declaration as ‘int a’
但这一限制在标准中具体规定在哪里?我找不到了。请给我一个推荐信。
以下程序是否符合c++标准?
namespace X { class A; }
namespace Y { using X::A; class A {}; }
int main() {}
通过不同的编译器,我得到了不同的结果:
gcc没有错误地编译它。
visual c++给出错误C2888:'X::A':符号不能在命名空间'Y‘内定义
在c++标准中,我没有发现我的程序违反了任何规则。
如果程序格式良好,为什么visual studio会出现错误?
如果程序格式不完善,那么c++标准中的哪些规则被违反了,为什么gcc没有给出一个错误?
我不是想让我
令我惊讶的是,我发现c++对象的名称可以与类名相同。当我将a类的对象声明为a a1()时,它不会引发错误,但也不会调用构造函数。为什么会这样?
我的代码:
#include<iostream>
using namespace std;
class a
{
public:
a()
{
cout << "in a\n";
}
};
int main()
{
a a1();
a a;
}
public void loadTill() throws IOException {
Scanner infile = new Scanner(new FileReader(SHOP_TILL_DATA_FILE));
int tillSize = infile.nextInt();
for (int i = 0;i<tillSize*2;i++){
UKDenomination ct =infile.next();
int nc = infile.nextInt();
DenominationFloat m = new Den
我正在学习Java,并且写了一个非常简单的程序。在它中,我可以将枚举放在最顶层,但不能放在方法中。在我看来,枚举几乎就像常量,所以为什么不在方法中使用它们呢?
在我的程序中,允许使用enum1,但不允许使用enum2。为什么?
enum enum1 {A, B, C};
public static void main(String[] args)
{
enum enum2 {A, B, C}; // only on a top level class or interface
}
第一部分:
我已经花了几天的时间来研究一些细节--不透明的--枚举声明和详细说明类型_s,我真的希望有人能证实这一点。GCC和VS2013不编译这个代码(clang ),我相信clang是符合§7.1.6.3/1的,因为enum E是一个详尽的类型说明符,并不是声明enum E e = E::b;的唯一组成部分。我的分析正确吗?
#include <iostream>
enum class E : char {a = 'a', b};
int E;
enum E e = E::b; // Doesn't compile in GCC and V
为什么下面的代码在g++下编译时没有任何警告或错误?我看到的问题是,在第一行中定义的变量x在if作用域中是可访问的,尽管它被重新定义了。
int main() {
int x = 5;
std::cout << x;
if (true) {
int x = 6;
std::cout << x;
}
}
我知道枚举
enum Year
{
First, Second, Third, Fourth;
}
被转换为
final class Year extends Enum<Year>
{
public static final Year First = new Year();
public static final Year Second = new Year();
public static final Year Third = new Year();
public static final Year Fourt
我有一个标头(@接口),我在其中声明了一个枚举数。
我将枚举数的类型引用为我的initWithDelegate构造函数的参数,但XCode并不认为它是有效的类型。
请给我一些关于这方面的建议。
下面是代码:
#import <Foundation/Foundation.h>
@interface ReportLoader : NSObject
@property (nonatomic, strong) NSString* fileKey;
//Issue with this line at the pReportType parameter declaration.
-(NS
我在下面所示的一些开源代码(JSoup的旧版本)中看到了下面的一些Java代码。
这是一份班级声明。在类声明中是声明了一个enum。但是枚举似乎更像是一个类,它包含构造函数方法、私有成员和公共方法。声明的实际枚举值将使用父类的静态成员进行初始化。
我习惯于使用vanilla声明,但我以前从未见过这种语法或模式。
你把这个模式称为什么,它是如何工作的,它能实现什么?
public class Entities {
public enum EscapeMode {
/** Restricted entities suitable for XHTML output: lt,