草庐IT

List_of_segments

全部标签

C++ 继承 : scoping and visibility of members

你能解释一下为什么这是不允许的吗,#includeclassB{private:inta;public:inta;};intmain(){return0;}这是什么时候?#includeclassA{public:inta;};classB:publicA{private:inta;};intmain(){return0;}在这两种情况下,我们在classB中都有一个名为a的公共(public)变量和一个私有(private)变量。现在编辑! 最佳答案 Inboththecases,wehaveonepublicandonepriv

c++ - 指向成员函数的指针 - C++ std::list 排序

如何将指向成员函数的指针传递给std::list.sort()?这可能吗?谢谢structNode{uint32_tID;char*Value;};classmyClass{private:uint32_tmyValueLength;public:listMyQueue;boolcompare(Node*first,Node*second);booldoStuff();}boolmyClass::compare(Node*first,Node*second){unsignedintii=0;while(iiValue[ii]Value[ii]){returntrue;}elseif(f

c++ - 删除 std::list::iterator 不会使迭代器无效并销毁对象吗?

为什么下面打印2?listl;l.push_back(1);l.push_back(2);l.push_back(3);list::iteratori=l.begin();i++;l.erase(i);cout我知道erase返回什么,但我想知道为什么这样可以?或者它是未定义的,还是取决于编译器? 最佳答案 是的,这是未定义的行为。您正在取消引用一种野指针。在erase之后,您不应该使用i的值。是的,erasedestructs指向的对象。但是,对于POD类型,销毁不会执行任何操作。erase不会为被删除的迭代器分配任何特殊的“空”

c++ - 错误 : initializer fails to determine size of ‘K’

我在行中收到错误“初始化器无法确定‘K’的大小”intK[]=newint[Vertices->total];如何解决? 最佳答案 改变intK[]=newint[Vertices->total];到int*K=newint[Vertices->total];第一个是Java创建数组的方法,其中K是对整数数组的引用。但是在C++中,我们需要让K成为一个指向整数类型的指针。 关于c++-错误:initializerfailstodeterminesizeof‘K’,我们在StackOver

c++ - 错误 :incompatible types in assignment of 'const char[5]' to 'char[10]'

我已经将c定义为charc[][10]在函数定义中并像c[i]="gray";一样使用它怎么了?我在网上搜索,它显示相同的语法。谢谢。 最佳答案 您不能对数组使用赋值(=)。如果将c更改为指针数组,这可能会起作用,具体取决于您需要使用它做什么。constchar*c[20];c[i]="gray";或者如果声明的类型必须是数组的数组,您可以使用strncpy:charc[20][10];strncpy(c[i],"gray",sizeof(c[i])); 关于c++-错误:incompa

c++ - 错误 : redefinition of class

这是我的代码://inmain.cpp#include"iostream"#include"circle.cpp"#include"rectangle.cpp"#include"shape.cpp"usingnamespacestd;intmain(){Shapeshapes[10];for(inti=0;iwidth=width;}virtualvoidsetHeigth(intheight){this->height=height;}};//inshape.cppclassShape{public:virtualintgetArea()const=0;};编译时,我得到这个错误:e

c++ - std::shuffle 不能用 std::list 编译

我正在尝试打乱一些生成元素的列表。这是代码:std::default_random_enginegenerator(10);std::listlist(10);intn=0;std::generate(list.begin(),list.end(),[&]{returnn++;});std::shuffle(list.begin(),list.end(),generator);它不编译。以下是错误:/include/c++/v1/algorithm:3059:34:Invalidoperandstobinaryexpression('std::__1::__list_iterator'

C++ list<T>::iterator 不能在派生类模板中使用

g++编译器给出了这个错误:expected`;'在“它”之前templateclassmyList:publicstd::list{public:voidfoo(){std::list::iteratorit;//compilererrorasabovementioned,why???}};谢谢。 最佳答案 在g++中。每当在模板中看到错误时:error:expected';'before'it'怀疑你需要一个类型名:typenamestd::list::iteratorit;当您在模板中声明了一个依赖于一个或多个模板参数的新类型

c++ - 从 std::list 中移除具有特定值的元素

我需要从std::list中删除具有特定值的元素。随着list我使用了remove()方法。现在我有list所以我想我应该使用remove_if()但它的谓词只需要一个参数-要测试的元素。如何编写函数foo(constCMyClass&Bad)从列表中删除所有等于Bad的元素?谢谢附言structCMyClass{void*Ptr;intVar;}boolis_equal(constCMyClass&A,constCMyClass&B){if(A.Ptr==B.PrtandA.Var==B.Var)returntrue;elsereturnfalse;} 最

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