草庐IT

c++ - 根据 value_type 调用适当的构造函数 : integer or float

我有一个函数,它使用均匀分布将最小值和最大值之间的随机值填充到容器中。#include#include#include#includetemplatevoiduniform_random(TContainer&container,consttypenameTContainer::value_typemin,consttypenameTContainer::value_typemax){std::random_devicerd;std::mt19937gen(rd());//Belowlinedoesnotworkwithintegerscontainerstd::uniform_rea

C++ is_member_pointer 实现

在c++标准库中,is_member_pointer实现为templatestruct__is_member_pointer_helper:publicfalse_type{};templatestruct__is_member_pointer_helper:publictrue_type{};///is_member_pointertemplatestructis_member_pointer:public__is_member_pointer_helper::type>::type{};有人可以解释一下_Cp是如何推导出来的吗?它像魔术一样工作。 最佳答

c++ - 在哪里可以找到 C++ STL 中 vector 的 size_type 定义?

将vector的size()函数的结果转换为unsignedint似乎是安全的。不过,我怎么能确定呢?我的文档不清楚size_type是如何定义的。 最佳答案 不要假定容器大小的类型(或在其中键入的任何其他内容)。今天?目前最好的解决方案是使用:std::vector::size_type其中T是您的类型。例如:std::vector::size_typei;std::vector::size_typej;std::vector>::size_typek;(使用typedef可以帮助使其更好地阅读)对于迭代器和“内部”STL容器中的

c++ - typeid 不返回正确的类型

cout在我看来,这应该返回int&作为类型,而不是int,但是在GCC4.5.1和VS2010SP1beta上它返回整数。这是为什么? 最佳答案 这就是typeid应该如何工作的。当您将typeid应用于引用类型的type-id时,type_info对象引用引用的类型。ISO/IEC14882:2003,5.2.8/4[expr.typeid]:Whentypeidisappliedtoatype-id,theresultreferstoatype_infoobjectrepresentingthetypeofthetype-id

Qt : has initializer but incomplete type 中的 C++ 错误

voidFindWords::getTextFile(){QFilemyFile(":/FindingWords2.txt");myFile.open(QIODevice::ReadOnly);QTextStreamtextStream(&myFile);QStringline=textStream.readAll();myFile.close();ui->textEdit->setPlainText(line);QTextCursortextCursor=ui->textEdit->textCursor();textCursor.movePosition(QTextCursor::S

c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?

我的代码有问题。在Xcode或使用C++11编译器中,此代码运行良好。但是,当我将此代码提交给在线法官时,判决显示“编译错误”。我认为他们使用的是C++4.7.1编译器,当我尝试编译它(使用Ideone)时,它说:prog.cpp:Infunction'voidprintArray(int)':prog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-ob

c++ - 在 std::conditional 中使用不完整类型的 sizeof

这是一个最小的例子:structincomplete_type;templatestructfoo{usingtype=std::conditional_t,std::conditional_t,double>;};foof;会导致错误,因为它会对类型执行sizeof,即使incomplete_type不是算术类型(iow,它不会在逻辑上进入sizeof分支)。livedemo所以,我想推迟sizeof:第一次尝试(失败)templateautofoo_aux(){if(sizeof(T)conditional_t,decltype(foo_aux()),double>仍然触发相同的错

c++ - "missing type specifier"构造函数声明错误

我在2个不同的文件中有2个类:正则矩阵.h:#ifndef_RM_H#define_RM_H#include"SparseMatrix.h"...classRegMatrix{...RegMatrix(constSparseMatrix&s){...}//ctor...};#endif稀疏矩阵.h:#ifndef_SM_H#define_SM_H#include"RegMatrix.h"...classSparseMatrix{...SparseMatrix(constRegMatrix&r){...}//ctor...};#endif在构造函数行上我得到了错误:错误C4430:缺少类

c++ - 错误 : Variable length array of Non-POD element type 'string'

在开始之前,我必须首先声明,我已经研究过针对此错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我项目的要求。另外,我目前正在学习CS入门类(class),所以我的经验几乎没有。数组的用途是从文件中收集名称。因此,为了初始化数组,我计算了名称的数量并将其用作大小。问题是标题中所述的错误,但我仍然使用一维数组时看不到解决方法。主要.cpp#include#include#include#include#include#include"HomeworkGradeAnalysis.h"usingnamespacestd;intmain(){ifstreaminfile;ofstrea

c++ - 在 C++ 中(不是在 C 中)指向具有未指定边界的数组的指针的可用情况

考虑以下代码:intmain(){int(*p)[];//pointertoarraywithunspecifiedboundsinta[]={1};intb[]={1,2};p=&a;//worksinCbutnotinC++p=&b;//worksinCbutnotinC++return0;}在纯C中,您可以将指针分配给任何维度数组的此类地址。但在C++中你不能。我发现了一种情况,编译器允许为此类指针赋值:structC{staticintv[];};intmain(){int(*p)[]=&C::v;//worksinC++if'v'isn'tdefined(onlydeclar