小编saj*_*jas的帖子

是否有可能在c ++中枚举枚举?

是否有可能在c ++中枚举枚举.我必须有类似的东西:

错误类型:

  • 类型1
  • 类型2
  • 类型3

类型1:

  • 原因1
  • 原因2

类型2:

  • cause3
  • cause4

类型3:

  • cause5
  • cause6

这些中的每一个都是整数值.它们应该用在通信协议栈中.在接收端,接收器必须从接收的值中解码错误的类型和原因.如果不能使用枚举,最好的方法是什么?

c++ enums

17
推荐指数
2
解决办法
6277
查看次数

HTC设备中的InputConnectionWrapper.commitText StackOverflowError

在HTC D820U中调试,发现StackOverflowError.设备:HTC D820u,Android 6.0,HTC版本:1.0.0.D820u见日志:

01-11 10:55:33.101 11234-11234/com.xxx E/CrashReport:java.lang.StackOverflowError:堆栈大小为8MB,位于com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:42 )位于android.view.inputmethod.InputConnectionWrapper的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82). com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)中的commitText(InputConnectionWrapper.java:82)位于com的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)位于com.android.tools.profiler.support.event的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)上的.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43) .InputConnectionWrapper.commitText(InputCo nnectionWrapper.java:43)在android.view上的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)位于android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java)的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)中的.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82) :82)位于com.android.tools的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43).在com.android.tools.profiler.support.event.InputConnectionWrappe的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)上的profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)r.commitText(InputConnectionWrapper.java:43)位于com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)位于android.view.inputmethod.InputConnectionWrapper.commitText的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82) (InputConnectionWrapper.java:82)com的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)位于com的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82). android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)位于com.android.tools.profiler.support.event的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82).一世 位于com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)中的nputConnectionWrapper.commitText(InputConnectionWrapper.java:43)位于android.view.inputmethod.InputConnectionWrapper.commitText的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82) (InputConnectionWrapper.java:82)com的com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)位于com的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82). android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)位于com.android.tools.prof的android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)iler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.j 01-11 10:55:33.101 11234-11234/com.xxx E/CrashReport:

++++++++++++++++++++++++++++++++++++++++++

关于日志,关于我的应用程序没有崩溃.这是Android的错误吗?还是HTC Rom的bug?

android

17
推荐指数
1
解决办法
1394
查看次数

如何捕获从类的全局实例的构造函数抛出的异常?

我知道拥有全局变量并不受欢迎.但在本书中,作者Bjarne Stroustrup撰写的C++编程语言称,"在非本地静态对象的初始化器抛出的情况下获得控制权的唯一方法是set_unexpected()".怎么做?

c++ constructor exception-handling global

13
推荐指数
1
解决办法
2562
查看次数

当函数应该改变对象的状态时,是去成员函数还是朋友函数?

在Bjarne Stroustrup撰写的The C++ Programming Language一书中,作者介绍了一个必须实现函数inv()的类Matrix.在第11.5.1节中,他谈到了这样做的两种可能性.一个是建立一个成员函数,另一个是使朋友函数inv().然后在第11.5.2节结束时,他谈到选择是否使用朋友或成员函数,他说:

如果inv()确实反转了Matrix m本身,而不是返回一个与m相反的新Matrix,那么它应该是一个成员.

为什么会这样?朋友函数不能改变Matrix的状态并返回对该矩阵的引用吗?是因为在调用函数时传递临时矩阵的可能性是什么?

c++ member-functions friend-function

6
推荐指数
1
解决办法
5608
查看次数

禁止针对特定模板代码的编译器警告

我已经实现了一个模板类来检测两种类型的可转换性(遵循Andrei Alexandrescu第2.7节"Modern C++ Design"一书中描述的方法).

我的实施如下:

#include <utility>
#include <iostream>

template<typename T, typename U>
class Conversion
{
private:
    using Small = char;
    using Big = class{ char dummy[2]; };
    static Small Test(U);
    static Big Test(...);
public:
    enum
    {
        exists = (sizeof(Test(std::declval<T>())) == sizeof(Small)) // Warning related to conversion.
    };
};

int main()
{
    std::cout << "Conversion int to float :" << Conversion<int, float>::exists << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2013(Visual C++ 2013)上编译此代码时,我收到有关从int到float的转换的以下警告

警告C4244:'argument':从'int'转换为'float',可能会丢失数据.

由于这是一个隐含的要求,有没有办法抑制这个警告?

我只是想单独为这个案子压制它.如果在其他地方进行此类转换,编译器仍应生成警告.

c++ templates type-conversion compiler-warnings visual-studio-2013

5
推荐指数
1
解决办法
558
查看次数

将 std::reference_wrapper&lt;Derived&gt; 视为 std::reference_wrapper&lt;Base&gt; 的最佳方法

我有两个类,即“ Base”和“ Derived”,其中Derived类继承Base类。

然后是对派生类实例的引用的容器 ( std::vector< std::reference_wrapper< Derived > > myContainer)。

最后,我有一个作为std::vector< std::reference_wrapper< Base > >参数的函数。

如果我将容器(myContainer)传递给函数,它不会编译:

在此输入图像描述

如果我更改容器来保存对 Base 的引用,一切都会正常工作,并且由于它是一个引用包装器,我相信,我仍然会具有我需要的多态行为。但这感觉不干净,因为我确信我的容器不会容纳除派生实例之外的任何东西。

同时,接受向量的函数应该适用于派生类和基类。

最小代码:

#include <vector>
#include <functional>

class Base
{ /*some virtual stuff*/ };

class Derived : public Base
{};

void Fun( std::vector< std::reference_wrapper< Base > > const & container )
{}

int main()
{
    std::vector< std::reference_wrapper< Derived > > myContainer;
    Fun( myContainer ); // Error: no viable conversion
    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism stl type-conversion reference-wrapper

5
推荐指数
1
解决办法
864
查看次数

为std :: shared_ptr指定一个Deleter,它适用于特定类型的所有对象或其派生类型

我的项目中有以下课程

class Base
{
    public:
    virtual ~Base(){};
}

class Der1: public Base
{
    public:
    virtual ~Der1(){};
}

class Der2: public Base
{
    public:
    virtual ~Der2(){};
}
Run Code Online (Sandbox Code Playgroud)

我持有这些类的对象为std::shared_ptr.我需要为所有类型Base或任何类型的对象提供自定义删除器derived types.

我想在删除方法中使用的代码将为所有这些对象做同样的事情,比方说

class Deleter
{
    public:
    void operator()( Base * b )
    {
        //Do something
        delete b;
    }
}
Run Code Online (Sandbox Code Playgroud)

而不是在每个对象的构造过程中提供删除器

std::shared_ptr< Der1 > pDer1( new Der1(), Deleter() );
std::shared_ptr< Der2 > pDer2( new Der2(), Deleter() );
Run Code Online (Sandbox Code Playgroud)

有没有办法指定类似"对于所有类型为Base的对象的共享指针或其派生类型Deleter用于删除的东西?由于删除类只是在shared_ptr的构造函数中采用,如何有人为特定类型指定删除器?

c++ smart-pointers shared-ptr delete-operator c++11

4
推荐指数
1
解决办法
376
查看次数

为什么构造函数Zlib_init在Bjarne Stroustrup的书中的Zlib_init类中保持私有

在Bjarne Stroustrup撰写的The C++ Programming Language一书中,作者说:

有时,当你设计一个库时,有必要或者只是方便地发明一个带有构造函数和析构函数的类型,其唯一目的是初始化和清理.这样的类型只能使用一次:分配一个静态对象,以便调用构造函数和析构函数.例如:

 class  Zlib_init
{
    Zlib_init() ; //get Zlib ready for use
   ~Zlib_init()  ; //clean up after Zlib
};
Class Zlib
{
   static  Zlib_init   x;
   /  /...
};
Run Code Online (Sandbox Code Playgroud)

遗憾的是,不能保证这样的对象在第一次使用之前被初始化,并且在最后一次使用之后被销毁在由单独编译的单元组成的程序中.

为什么作者将构造函数和析构函数保留为私有成员?如果我们在由单独编译的单元组成的程序中使用它,为什么这个方法不起作用?是否需要定义成员x来调用构造函数Zlib_init()和析构函数~Zlib_init()?那么这种方法的用途是什么?这是在本书的第10.4.9节.

c++ private-constructor

3
推荐指数
1
解决办法
180
查看次数

在模板参数推导中,sqrt <T>(complex <T>)matche sqrt <T>(<T>)函数调用如何?

template<class T> T sqrt (T);
template<class T> complex<T> sqrt(complex<T>);
double sqrt(double);
void f(complex<double> z)
{
    sqrt(z);
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,如何sqrt<double>(complex<double>)最终成为模板参数推导的候选者?作者说任何匹配的电话sqrt<T>(complex<T>)也匹配sqrt<T>(<T>).怎么样?

代码来自Bjarne Stroustrup撰写的The C++ Programming Language.第13.3.2节

c++ templates arguments template-argument-deduction

2
推荐指数
1
解决办法
131
查看次数

专门用于获取指向特定类或指向派生类对象的指针的模板类

我如何专门化类模板,以便模板参数可以是类型:指向特定类的指针或指向该特定类型的派生类的指针?是否可以在不使用Boost的情况下完成?

可能重复: 仅接受某些类型的C++模板

我只是想知道即使我使用指向实例的指针,答案是否相同.

c++ templates template-specialization

2
推荐指数
1
解决办法
507
查看次数

链接某些Windows API时出现链接器错误

我的系统中有一个makefile项目。最近,我添加了一些使用以下Windows API的新功能:

RegOpenKeyEx
RegEnumKeyEx
RegCloseKey
RegGetValue
Run Code Online (Sandbox Code Playgroud)

为了拥有这些API,我还添加了windows.h头文件。代码可以在我的机器上正常编译和链接。但是,在我的同事机器中链接失败。我们都在64位Windows计算机上工作。在他的电脑上,我得到了错误:

错误LNK2001:无法解析的外部符号__imp_RegOpenKeyExW

错误LNK2001:无法解析的外部符号__imp_RegGetValueW

错误LNK2001:无法解析的外部符号__imp_RegCloseKey

错误LNK2001:无法解析的外部符号__imp_RegEnumKeyExW

我尝试过的:由于正在使用的库Advapi32.lib位于C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64

我尝试添加以下行:

LINKFLAGS + = -L“ C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Lib \ x64”

我将库的路径添加到环境变量 PATH

我将库复制到输出文件夹。

没事。

如前所述,该代码在我的PC上运行正常,但在另一台PC上失败。

c++ windows registry winapi linker-errors

1
推荐指数
1
解决办法
1668
查看次数