相关疑难解决方法(0)

在C++中禁止复制构造函数的最可靠方法是什么?

有时需要禁止C++类中的复制构造函数,以使类变为"不可复制".当然,operator=应该同时禁止.

到目前为止,我已经看到了两种方法.方法1是将方法声明为private并且不给它实现:

class Class {
//useful stuff, then
private:
    Class( const Class& ); //not implemented anywhere
    void operator=( const Class& ); //not implemented anywhere
};
Run Code Online (Sandbox Code Playgroud)

方法2是将方法声明为private并将其赋予"空"实现:

class Class {
//useful stuff, then
private:
    Class( const Class& ) {}
    void operator=( const Class& ) {}
};
Run Code Online (Sandbox Code Playgroud)

IMO第一个更好 - 即使有一些意外的原因导致从同一个类成员函数调用复制构造函数,稍后会出现链接器错误.在第二种情况下,在运行时之前不会注意到这种情况.

第一种方法有任何严重的缺点吗?什么是更好的方式,如果有的,为什么?

c++ constructor class-design copy-constructor

21
推荐指数
3
解决办法
3164
查看次数

试图引用已删除的功能

我正在尝试了解fstream类,我遇到了一些麻烦.我创建了几个txt文件,一个带有笑话,另一个带有一个妙语(joke.txt)和(punchline.txt),只是为了阅读和显示内容.我向用户询问文件名,如果发现它应该打开它,清除标志然后读取内容.但我甚至无法测试它读取的内容因为我目前收到有关已删除功能的错误但我不知道我知道这意味着什么

错误1:

"IntelliSense: function "std::basic_ifstream<_Elem, _Traits>::basic_ifstream(const std::basic_ifstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 818 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\fstream") cannot be referenced -- it is a deleted function
Run Code Online (Sandbox Code Playgroud)

第二个错误是完全相同但是对于第二个函数(displayLastLine())

和错误3:

Error   1   error C2280: 'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)' : attempting to reference a deleted function
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#include "stdafx.h" 
#include <string>    
#include <iostream>  
#include <fstream>   

using namespace std; 

void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);

 int main()          
{
    string fileName1, fileName2;
    ifstream jokeFile, punchlineFile;


    // Desribe the assigned project …
Run Code Online (Sandbox Code Playgroud)

c++ string fstream function ifstream

20
推荐指数
1
解决办法
7万
查看次数

Boost池分配器不会使用g ++中的std :: allocate_shared进行编译

编辑:

澄清我想要的结果,因为我还没有沟通得好:
为了能够使用std::allocate_sharedboost::fast_pool_allocator作为使用G ++ 4.8或更高升压1.56.0的分配方法.目前这适用于g ++ 4.6,并且在4.7,4.8和4.9上失败.

要清楚,我不打算为g ++ 4.7做这项工作.

测试代码产生错误:

#include "boost/pool/pool.hpp"
#include "boost/pool/pool_alloc.hpp"

#include <memory>

int main(int argc, char** argv)
{
  auto fails = std::allocate_shared<int>( boost::fast_pool_allocator<int>() );

  auto works = std::allocate_shared<int>(boost::fast_pool_allocator<int>(), 5);
}
Run Code Online (Sandbox Code Playgroud)

在我们的代码库中,我们将std :: allocate_shared与boost池分配器结合使用,这会导致一些讨厌的编译错误.然而,这已经在不同版本的g ++中变形和变化:
细节:64位,(4.7,4.8)-std = c ++ 11,(4.6)-std = c ++ 0x,boost 1.56.0
4.6 - 编译愉快
4.7 -崩溃编译器

内部编译器错误:重新输入错误报告例程.如果合适,请提交完整的错误报告,并提供预处理的来源.请参阅说明.存储在/tmp/cca0Emq9.out文件中的预处理源,请将其附加到您的bug报告中.

4.8 - 令人讨厌的编译错误

/XXXXXXXXXX/boost/boost/pool/pool_alloc.hpp:399:
Run Code Online (Sandbox Code Playgroud)

错误:使用删除功能 '的std :: _ Sp_counted_ptr_inplace,(__gnu_cxx :: _ Lock_policy)2U> :: _ Sp_counted_ptr_inplace(常量性病:: _ Sp_counted_ptr_inplace,(__gnu_cxx :: _ Lock_policy)2U>&)' 的{新(PTR)T(t)的; …

c++ boost compiler-errors g++ c++11

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

使用boost :: mutex - 隐式删除错误(因为默认定义不正确)

我在.h文件中有类看起来如下(标题)

#include <boost/thread.hpp>

class MyClass{

    private:
        boost::mutex bPoolMtx_;

        // ... other vars
    public:
        // public vars and methods

}
Run Code Online (Sandbox Code Playgroud)

尝试构建/编译时出现以下错误.

MyClass.h:38:7: note: ‘MyClass::MyClass(const MyClass&)’ is implicitly deleted because the default definition would be ill-formed:
MyClass.h:38:7: error: use of deleted function ‘boost::mutex::mutex(const boost::mutex&)’
Run Code Online (Sandbox Code Playgroud)

我还没有在cpp文件中使用互斥锁.当我注释掉它的boost::mutex线条时它很好.到底是怎么回事?

c++ boost

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