草庐IT

forward-declaration

全部标签

C++ 错误 : Member declaration not found

我是一个C++新手。今天遇到一个问题:在头文件中,我定义了一个类:templateclassPtr_to_const{private:Array_Data*ap;unsignedsub;public:...Ptr_to_const&operator=(constPtr_to_const&p);};在源文件中,我编程为:templatePtr_to_const&Ptr_to_const::operator=(constPtr_to_const&p){...return*this;}编译时,编译器总是说:'找不到成员声明'。为什么?我使用eclipseCDT+CygwinGCC非常感谢!

c++ - std::tie 和 std::forward_as_tuple 有什么区别

对于一个给定的类,如果我想写所有的比较运算符,为了避免代码重复,我会这样写:classB{public:booloperator==(Typeconst&rhs)const{returnas_tuple()==rhs.as_tuple();}booloperator!=(Typeconst&rhs)const{returnas_tuple()!=rhs.as_tuple();}//..andsameforotheroperators..private:autoas_tuple()const{returnstd::tie(a,b,c);//allthemembers}};我可以用std:

c++ - 错误 : uint64_t was not declared in this scope when compiling C++ program

我正在尝试一个简单的程序来打印steady_clock的时间戳值,如下所示:#include#includeusingnamespacestd;intmain(){cout(steady_clock::now().time_since_epoch()).count();cout但是每当我像这样编译时g++-oabcabc.cpp,我总是会遇到错误:Infileincludedfrom/usr/include/c++/4.6/chrono:35:0,fromabc.cpp:2:/usr/include/c++/4.6/bits/c++0x_warning.h:32:2:error:#er

c++ - 我可以用 C++ "forward declare"做什么?

我知道我能做到classFoo;可能structBar;和全局函数boolIsValid(intiVal);类型化的枚举呢?未声明类中的类型化枚举怎么样?带有未声明类的函数呢?未声明的类中的静态成员呢?未知命名空间中的这些怎么办?我是否遗漏了任何其他可以预先声明的内容? 最佳答案 可以转发声明模板,包括部分特化明确的特化嵌套类(这包括结构、“真实”类和union)非嵌套和本地类变量(“外部整数;”)职能如果“前向声明”是严格意义上的“声明但不定义”,您也可以前向声明成员函数。但是一旦它们被声明,你就不能在它们的类定义中重新声明它们。

C++11 std::forward 一个指针

我有一个Signal我的应用程序中的类为类提供了公开事件的选项(与.NET中相同)。类(class)正常,一切顺利。昨天我看到thisSOquestion(anditsanswer)并熟悉std::forward.我决定尝试在我的代码中使用它,所以我更改了每个std::function至std::function在raise函数(operator())中,我使用了我在上面链接中看到的相同逻辑,所以现在该函数采用Args&&...args并且回调使用std::forward(args)...这是我的Signal类的简化版本(为了使其成为一个很好的示例而进行了一些更改):templatec

c++ - 为什么 std::forward 在这种情况下没用

我注意到std::forward在这种情况下是无用的:voidconsumeObject(std::unique_ptr&&robj){myvector.emplace_back(std::forward>(robj));}这是为什么呢?我认为当一个右值引用绑定(bind)到一个函数参数时(即我可以通过名称引用它)它在那个范围内变成了一个左值并且为了向前传递需要被转换为一个右值引用(就像在完美转发中一样).为什么它是错误的/没用的? 最佳答案 使用std::forward并没有错误在这里(本身),但它是不合适的,因此具有误导性(从这

c++ - 为什么 gcc 提示 "declaration of ' foo' 隐藏了之前的调用 [-Werror=shadow]”

我有这个MCVE:autobar()->double{return8.0;}intmain(){if(autofoo=bar()){returnfoo;}elseif(autofoo=bar()){returnfoo;}}使用gcc7.3和这些选项-c-Werror-Wextra-Wall-Wshadow编译它会生成以下错误消息:test-shadow.cpp:Infunction‘intmain()’:test-shadow.cpp:9:17:error:declarationof‘foo’shadowsapreviouslocal[-Werror=shadow]elseif(aut

c++ - 错误 : out-of-line definition of 'test' does not match any declaration in 'B<dim>'

我有一个小问题让我很烦!!我不知道下面的代码似乎有什么问题。我应该能够实现从父类(superclass)继承的功能,不是吗?但我得到error:out-of-linedefinitionof'test'doesnotmatchanydeclarationin'B'templateclassA{public:virtualdoubletest()const;};templateclassB:publicA{};templatedoubleB::test()const{return0;}我在Mac上使用clang(AppleLLVM5.1版)。 最佳答案

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

收到此错误,我很确定它在operatorvoidCRational::print()const{print(cout);}voidCRational::print(ostream&sout)const{if(m_denominator==1)cout 最佳答案 您需要通过引用而不是值返回ostream。它试图调用构造函数。也可以传递'a'作为引用:ostream&operator我还注意到打印方法可能是错误的。它有sout作为流的名称传递,但随后直接使用cout实现。应该是voidCRational::print(ostream&s

c++ - 了解 std::forward

为什么编译器无法推导出std::forward的模板参数?我的意思是:#include#includestructX{};structA{A(constX&){std::coutT*factory(Arg&&a){returnnewT(std::forward(a));//----------^^^^^^^^^^^^^^^error:can'tdeducetemplateparameter}intmain(){factory(foo());}我知道这是一个设计选择(由于std::forward定义中的std::remove_reference)以避免用户忘记指定类型.我无法得到的是:为