Microsoft(R)F#2.0 Interactive build 4.0.40219.1
我正在尝试定义新的记录类型:
type TestOptions =
{ perRunGC : bool;
collectGCStat : bool;
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,但我们再添加一个字段:
type TestOptions =
{ perRunGC : bool;
collectGCStat : bool;
highPriority : bool;
} ^
Run Code Online (Sandbox Code Playgroud)
我在上面标记的位置得到解析器错误:
error FS0010: Unexpected character ' ' in field declaration
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?是编译器错误吗?
请告诉我为什么我的程序被编译和执行时,我将构造函数视为没有私有和公共部分的类,只需class在其前面写下单词,如下所示:
class sample
{
private:
int a,b;
public:
class sample(int a1){a = a1;}
};
Run Code Online (Sandbox Code Playgroud) 我有CanExecute一个WPF命令看起来有所不同,这取决于我对编译器的明确程度; 问题是,我不希望必须明确.
private bool CanRemoveField()
{
return SelectedField != null &&
Context.Item.Id == 0
? _fieldsByFieldModel.ContainsKey(SelectedField)
: !_hasAnyCosts;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码,当查询一个Id != 0保持为真的项时,按钮启用,尽管 SelectedField是null,所以我希望条件短路并返回false.
代码略有调整:
private bool CanRemoveField()
{
return SelectedField != null &&
(Context.Item.Id == 0
? _fieldsByFieldModel.ContainsKey(SelectedField)
: !_hasAnyCosts);
}
Run Code Online (Sandbox Code Playgroud)
我已经在三元组周围引入了一些括号,并且现在显示了在没有选择字段时禁用按钮的所需行为.
鉴于它是一个三元组,如果,我已经预料到我想要的行为,而不需要括号,因为它应该被视为一个陈述,不是吗?
这是我在learncpp.com上阅读本节时遇到的一个问题.我使用了这里列出的代码,然后对测试做了一些改动.
背景
虚拟继承创建对基类的公共引用,它具有两个效果.
首先,它消除了歧义,因为只创建了一个基础成员的副本(例如,向PoweredDevice添加print()函数并在main()中调用它否则会导致编译器错误).
其次,派生程度最高的类负责调用基础构造函数.如果其中一个中间类尝试在初始化列表中调用基本构造函数,则应忽略该调用.
问题
当我编译并运行代码时,它返回:
PoweredDevice: 3
PoweredDevice: 3
Scanner: 1
PoweredDevice: 3
Printer: 2
Run Code Online (Sandbox Code Playgroud)
它应该返回:
PoweredDevice: 3
Scanner: 1
Printer: 2
Run Code Online (Sandbox Code Playgroud)
当我使用GDB(7.11.1)执行执行时,它表明中间函数也通过初始化列表调用PoweredDevice - 但是应该忽略这些函数.PoweredDevice的这种多次初始化不会导致任何成员的歧义,但确实让我感到困扰,因为代码只执行一次时执行多次.对于更复杂的问题,我不习惯使用虚拟继承.
为什么这些中间类仍在初始化基础?这是我的编译器(gcc 5.4.0)的怪癖,还是我误解了虚拟继承的工作原理?
编辑:代码
#include <iostream>
using namespace std;
class PoweredDevice
{
public:
int m_nPower;
public:
PoweredDevice(int nPower)
:m_nPower {nPower}
{
cout << "PoweredDevice: "<<nPower<<endl;
}
void print() { cout<<"Print m_nPower: "<<m_nPower<<endl; }
};
class Scanner : public virtual PoweredDevice
{
public:
Scanner(int nScanner, int nPower)
: PoweredDevice(nPower) …Run Code Online (Sandbox Code Playgroud) 以下 C++ 程序是否包含任何未定义的行为?
int
main()
{
struct entry
{
uint32_t hash;
uint32_t idx;
};
entry arr[31] = {
{ 7978558, 0}, { 9241630, 1}, { 65706826, 2},
{ 639636154, 3}, {1033996244, 4}, {1225598536, 5},
{1231940272, 6}, {1252372402, 7}, {2019146042, 8},
{1520971906, 9}, {1532931792, 10}, {1818609302, 11},
{1971583702, 12}, {2116478830, 13}, { 883396844, 14},
{1942092984, 15}, {1274626222, 16}, { 333950222, 17},
{1265547464, 18}, { 965867746, 19}, {1471376532, 20},
{ 398997278, 21}, {1414926784, 22}, {1831587680, 23},
{ 813761492, 24}, { …Run Code Online (Sandbox Code Playgroud) GCC 不会编译此代码,而其他编译器(clang、msvc)会编译此代码
template<auto T>
struct S { };
int main() {
S<[]{int x,y; x<<y;}> s1; // compiles fine
S<[]{int x,y; x>>y;}> s2; // error
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: expected ';' before '>>' token
| S<[]{int x,y; x>>y;}> s2;
| ^~
| ;
Run Code Online (Sandbox Code Playgroud)
但是,当我明确调用时operator>>,GCC 接受它
struct S2 {
void operator>>(S2 arg) { }
};
S<[]{S2 x,y; x.operator>>(y);}> s2; // compiles
Run Code Online (Sandbox Code Playgroud)
当我将 lambda 定义移到模板参数列表之外时,它也会进行编译
auto lam = []{int x,y; x>>y;};
S<lam> s; // compiles
Run Code Online (Sandbox Code Playgroud)
这是编译器错误吗?
这是我的片段:
var country = BLLocations.Instance.GetCountries();
ddlCountry.DataSource =
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "CountryCode";
ddlCountry.DataBind();
Run Code Online (Sandbox Code Playgroud)
见第二行:
ddlCountry.DataSource =
Run Code Online (Sandbox Code Playgroud)
它成功编译并发布到云端.奇怪!
我想创建一个实现的枚举I2,它扩展了I1:
package a;
import static a.E1.E1A;
interface I1 extends I1 {}
interface I2 extends I1 {}
enum E1 implements I2 { E1A, A1B; }
class A {
public static void main(String[] args) {
switch (E1A) {
case E1A:
System.out.println("it worked!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
它一直在工作,直到我添加I1并I2扩展I1.现在它可以工作,但它找不到E1A(之前的静态存在,所以这不是问题):
$ javac A.java
A.java:3: cannot find symbol
symbol : static E1A
location: class a.E1
import static a.E1.E1A;
^
The system is out of resources.
Consult the …Run Code Online (Sandbox Code Playgroud) 我相当肯定uint8_t从另一个中减去一个应该导致另一个无符号数,但我的一些结果让我感到困惑.
uint8_t a = 2;
uint8_t b = 1;
uint8_t c = -a;
long d = b - a;
long e = b + c;
Run Code Online (Sandbox Code Playgroud)
当我获得d它产生的价值时-1,e正如我所期望的那样255.这是gcc我正在使用的版本的错误.....对吗?
作为参考,我正在使用arm-none-eabi-g++编译器MSP-432.
看着这似乎表明gcc这里似乎犯了同样的错误.
看看这个问题似乎看起来像神弩和手臂gcc是错误的.
这里发生了什么?
在下面的简单示例程序中,我有一个t_cell用 5 个元素初始化的对象向量。我还有一个整数测试向量。
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class t_cell {
public:
int id;
t_cell operator = ( const t_cell &rhs_cell ){
t_cell lhs_cell;
lhs_cell.id = rhs_cell.id;
return lhs_cell;
}
};
int main(){
std::vector<int > test ( 5 );
std::vector<t_cell> cells( 5 );
for( size_t icell=0; icell < cells.size(); icell++ ){
test [icell] = icell;
cells[icell].id = icell;
}
for( size_t icell=0; icell < cells.size(); icell++ ){
cout << "before =" << icell << …Run Code Online (Sandbox Code Playgroud)