草庐IT

method-declaration

全部标签

c++ - 在 C++ 中 : why does a constructor get called when an array of objects is declared?

MyClassmc2[]={MyClass(),MyClass()};//thiscallstheconstructortwiceMyClassmc1[4];//thiscallstheconstructor4times.Why?所以,我的问题是:为什么没有初始化的对象数组声明会导致调用默认构造函数? 最佳答案 在C++中,大小为4的MyClass数组是四个实际对象。它有点像包含该类型的四个成员的结构,当然您可以使用不同的语法访问这些成员,并且存在其他技术差异。因此,定义该数组导致构建4个对象的原因(并且在大致相同的情况下)与定义该

C++ 错误 : definition of implicitly-declared

我正在用C++编写这个链表程序当我测试程序时,我得到了错误linkedlist.cpp:5:24:error:definitionofimplicitly-declared'constexprLinkedList::LinkedList()'LinkedList::LinkedList(){这是代码链表.h文件:#include"node.h"usingnamespacestd;classLinkedList{Node*head=nullptr;intlength=0;public:voidadd(int);boolremove(int);intfind(int);intcount(i

c++ - C++模板函数中,依赖函数调用为什么报 "not declared"错误?

在C++模板函数foo()中,调用::bar(TT*)在gcc4.4.3下会出现以下错误:g++-ohello.o-c-ghello.cpphello.cpp:Infunction'voidfoo(std::vector>&)':hello.cpp:8:error:'::bar'hasnotbeendeclared这是有问题的代码://hello.cpp#includetemplatevoidfoo(std::vector&vec){TT*tt;::bar(tt);vec.push_back(tt);}classBlah{};voidbar(Blah*&){}intmain(intar

c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

我无法理解这个错误。这个错误不在我正在调试的类中。(是吗?)错误是:c:\programfiles\microsoftvisualstudio10.0\vc\include\fstream(890):errorC2248:'std::basic_ios::basic_ios':cannotaccessprivatememberdeclaredinclass'std::basic_ios'1>with1>[1>_Elem=char,1>_Traits=std::char_traits1>]1>c:\programfiles\microsoftvisualstudio10.0\vc\inc

C++11 decltype : How to declare the type that a pointer points to?

我有以下代码:#includeintmain(){int*a=newint(2);std::unique_ptrp(a);}导致这些错误信息:Infileincludedfroma.cpp:1:Infileincludedfrom/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/

c++ - GCC 编译错误 : declaration of ‘strlen’ must be available

我的问题是,当我想制作一个下载的库时,我从GCC得到了一些奇怪的编译错误。编译器要求更正的代码似乎是正确的。报错都是这样的:Catalogue.h:96:error:therearenoargumentsto‘strlen’thatdependonatemplateparameter,soadeclarationof‘strlen’mustbeavailable这是第96行附近的代码:GaCatalogueEntry(constchar*name,T*data){if(name){_nameLength=(int)strlen(name);//LINE96//copyname_name

c++ - 如何处理失败的方法 : by using exceptions or making the methods return bool?

如何处理失败的方法:使用异常使方法返回bool值第一种方法是当出现问题时抛出异常。但是有问题的代码需要放在tryblock中,然后你需要编写catchblock。您需要检查返回值的第二种方法方法,然后做一些事情。那么基本上不是同一个机制吗?你有两个部分:检测到出现问题然后采取措施。那么我使用哪种方法重要吗? 最佳答案 异常的主要好处是它们是非本地的。您可以在抛出异常的地方捕获几个调用层之外的异常。这样,介于两者之间的代码就不必关心异常(除了确保在展开期间进行适当的清理,即异常安全),这使得异常情况不太可能被遗忘。但这种好处是有代价的

c++ - Qt Creator "override method"快捷方式?

QtCreator有没有像eclipse一样的“覆盖方法”快捷方式?对于那些不了解Eclipse的人,有一个显示对话框的上下文菜单“Source>OverrideMethod”。检查您要覆盖的方法,它将为这些方法生成stub。 最佳答案 右键单击类名从上下文菜单中选择Refactor->InsertVirtualFunctionsofBaseClasses顺便说一句,我使用的是QtCreator3.4.1。 关于c++-QtCreator"overridemethod"快捷方式?,我们在

c++ - Superclass::method 或 this-> 方法之间的区别

如何以及何时调用父类(superclass)方法?两个选项请引用代码段:classSuperClass{public:voidmethod();};classSubClass:publicSuperClass{public:voidsomeOtherMethdo(){this->method();SuperClass::method();}}; 最佳答案 使用this->method()调用一个函数,该函数要么在您的父类(superclass)中实现,要么由您自己的类实现。当使用superClass::method()时,您一定要调

C++ 风格约定 : Parameter Names within Class Declaration

我是一个相当新的C++程序员,我想听听支持和反对在类声明中命名参数的争论。这是一个例子:Student.h#ifndefSTUDENT_H_#defineSTUDENT_H_#includeusingnamespacestd;classStudent{private:stringname;unsignedintage;floatheight,GPA;public:Student(string,unsignedint,float,float);voidsetAge(unsignedint);};#endif/*STUDENT_H_*/对比#ifndefSTUDENT_H_#defineS