struct A
{
int a = 5; //OK
const int b = 5; //OK
static const int c = 5; //OK
static int d = 5; //Error!
}
error: ISO C++ forbids in-class initialization of non-const static member 'A::d'
为什么会这样呢?有人能给我解释一下背后的原因吗?
,作者说
只有POD类型可以作为省略号“.”的参数。而std::string不是POD类型。
我把它理解为Passing NON-POD type to Variadic function is undefined behavior。是对的吗?
不过,他说的是C/C++标准吗?我试图在n3242 C++规范中找到它。却找不到。
我想知道我的理解是正确的,这是一个标准。
最近,我在一个在线编译器上测试了简单的跟踪程序。参见现场演示。它编译得很好&给出了预期的输出,但是当我在Dev C++ IDE上测试它时,它在编译期间失败了。
这是我的节目:
#include <iostream>
class Test
{
int s=9;
public:
int get_s()
{
return s;
}
};
int main()
{
Test s;
Test& t{s}; // error in C++11 but not in C++14 why???
当我尝试在R中安装Acumos包时,我失败了。这似乎是由于protobuf和最特殊的common.h文件造成的。我使用的是R版本3.5.1。
install.packages("acumos",,c("http://r.research.att.com","http://rforge.net"))
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependency ‘RProtoBuf’
我是C++的新手。我经常在一些网站上看到这个变种成员?
class School
{
int x; -> data member.
}
我认识数据会员。但是什么是变体成员呢?
注意:来自c++规范:在构造器页面下。
X is a union-like class that has a variant member with a non-trivial default constructor.
我正在尝试构建sysrepo库作为对travis ci的依赖,并且得到一个位字段的问题。当我在14.04或16.04版本的ubuntu虚拟机上安装时,我没有得到这个问题,我很困惑为什么在使用travis ci时会出现这个问题。我安装所有内容的构建脚本如下:
#!/bin/bash
set -e
#this script installs sysrepo and all of its dependencies.
INSTALL_PREFIX_DIR=$HOME/local
export PKG_CONFIG_PATH=$INSTALL_PREFIX_DIR/lib/pkgconfig:$PKG_
我正在使用来自的Protoc-3.18.0-Win 32版本。成功编译.proto文件后,QtCreator 5 (C++11)程序中出现以下错误:
C:\Users\MyName\MyProject\lib\include\google\protobuf\stubs\mutex.h:124: error: temporary of non-literal type 'google::protobuf::internal::CallOnceInitializedMutex<std::mutex>' in a constant expression
In file i
我相信这个密码:
#include <stdio.h>
struct foo {
char array[1024];
};
int main() {
fprintf(stderr, "sizeof(foo::array): %zd\n", sizeof(foo::array));
}
是有效的C++。g++很好地用-ansi -pedantic编译它。但是,在使用Intel的icc 12.1.3编译时,我得到:
error #288: a nonstatic member reference must be relative to a
在下面的C++程序中,从函数修改静态数据成员const可以正常工作:
class A
{
public:
static int a; // static data member
void set() const
{
a = 10;
}
};
但是,从const函数修改非静态数据成员不起作用:
class A
{
public:
int a; // non-static data member
void set() const
{
a = 10;
}
};
为什么const
我在Windows上用2015编写了一个项目(包括几个cpp和h文件),但我需要在Ubuntu上运行它。有什么办法我能做到吗?我尝试了一下$ g++ -std=c++0x *.cpp,但是我犯了很多错误:
In file included from DataTable.cpp:1:0:
DataTable.h:9:14: sorry, unimplemented: non-static data member initializers
DataTable.h:9:14: error: in-class initialization of static data member ‘data
我用的是GCC 4.8.2:
$ g++ --version
g++ (GCC) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我有这样的定义:
#pragma once
#in
编译Objective-C++时,我得到以下诊断结果:
'MyClass' cannot be shared between ARC and non-ARC code; add a non-trivial copy assignment operator to make it ABI-compatible
缺少复制构造函数和析构函数也会出现类似的错误。添加必要的方法很容易,但它们通常没有意义,特别是对于我想使其不可复制的类,而且我不会混淆ARC和非ARC代码(至少不是故意的)。
为什么我会收到这样的消息,我可以在不为所有C++类编写无意义的成员函数的情况下摆脱它吗?
我试着去做
MyClass& x;
x = MyClass(a,b,c);
但是C++不允许我这样做,因为它认为x在一开始是未初始化的。
所以我试着
MyClass& x = MyClass(a,b,c);
但是在说invalid initialization of non-const reference of type 'MyClass&' from an rvalue of type 'MyClass'的时候出错
它有什么问题?看起来我现在什么都做不了。如何解决初始化问题?
我有一个在Java中用Protobuf序列化数据的程序,方法是在byte[]数组中编写二进制数据,然后将其保存在".txt“文件中。我在字符串流中接收C++端的数据。现在我想用C++解析二进制数据,但是Protobuf解析-方法"parseFromString()“不起作用!未设置测试消息中的字段。我为此编写了一个小测试,我可以向您展示一些代码:
Java序列化
byte[] s = test.build().toByteArray(); //This is serialized to "C:\test.txt" as binary
C++解析:
Test
我正在使用这个类:
public ref class x: public System::Windows::Forms::Form
{
private: gcroot<std::string> name;
}
我得到了以下错误:
IntelliSense: a member of a managed class cannot be of a non-managed class type
我知道我可以使用char*,但如果我使用大量char*,我将不得不手动执行delete[],否则会出现一些堆损坏问题
我已经被困在这里两天了
注意:我必须在c++中使用c++和UI