将ofstream的实例插入容器

时间:2017-04-15 18:24:53

标签: c++ design-patterns copy-constructor ofstream

我创建了一个类file,它是std::ofstream的包装器。我创建了一个Container来包含file的所有实例。

class file
{
private:
      std::ofstream ofs;

public:
      void open(std::string filename);
      void write(std::string s);
      void close();
};

class Container
{
private:
  std::map<int, file> m;


public:
      void insert(file f,int i)
      {
            m.insert(std::pair<int,file> (i,f));
      }
      void get(int i)
      {
            m.at(i);
      }
};

但是,此代码中存在问题。在insert方法中,我尝试复制std::pair<int,file>无法完成的std::ofstream,因为file的复制构造函数已被删除(请参阅下面的编译错误)。

我想在容器中连续添加In file included from src/test.cpp:1: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:627: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:274:23: error: call to implicitly-deleted copy constructor of 'file' : first(__x), second(__y) {} ^ ~~~ src/test.cpp:35:22: note: in instantiation of member function 'std::__1::pair<int, file>::pair' requested here m.insert(std::pair<int,file> (i,f)); ^ src/test.cpp:9:21: note: copy constructor of 'file' is implicitly deleted because field 'ofs' has a deleted copy constructor std::ofstream ofs; ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/fstream:1168:5: note: copy constructor is implicitly deleted because 'basic_ofstream<char, std::__1::char_traits<char> >' has a user-declared move constructor basic_ofstream(basic_ofstream&& __rhs); ^ 1 error generated. 的实例。我怎么能这样做?

这是编译错误

from scipy.io import wavfile
from scipy import signal
import pyaudio
import wave
import sys
# import matplotlib.pyplot as plt
# import numpy as np
import scikits.audiolab


def processAudio(audio, X, X2, X3, X4, X5):

## Import Audio Signal



samplerate, data = wavfile.read(audio)


data = data.astype(float) ## floating points

d = (data[:,0] + data[:,1]) / 2 ## make it mono

# d = d/max(d)


## Create the Low Pass filter

centFL = 45.0 ## center frequency of the lowpass

R = float((centFL * 2)/samplerate)


b, a = signal.butter(4,R,'low', analog=False) ### create a lowpass filter

# w,h = signal.freqs(b,a) ## frequency response coefficients to be plot

# plt.semilogx(w, 20 * np.log10(abs(h)))
# plt.title('Butterworth filter frequency response')
# plt.xlabel('Frequency [radians / second]')
# plt.ylabel('Amplitude [dB]')
# plt.margins(0, 0.1)
# plt.grid(which='both', axis='both')
# plt.axvline(100, color='green') # cutoff frequency
# plt.show()

YL = signal.filtfilt(b, a, d)  ## signal fiter with low pass coefficients

YL = YL/max(YL) ## normalize YL

G = 2**(X/6)



YL = YL * G ### mutliplicacion filtro uno



#scikits.audiolab.play(YL,samplerate)

## High pass filter

centFH = 15000.0 ## center frequency of the highpass

RH = float((centFH * 2)/samplerate)

bH,aH = signal.butter(4,RH, 'high', analog=False) ## create a highpass filter

YH = signal.filtfilt(bH,aH,d) ## apply the filter

YH = YH/max(YH) ##normalize

G2 = 2**(X2/6)

YH = YH * G2 ## multiplicacion filtro 2

# scikits.audiolab.play(YH,samplerate)


# w2,h2 = signal.freqs(bH,aH) ## frequency response coefficients to be plot

##  Three Band Pass filter

## 700 central freq
centFB = 700.0

RcentFB = float((centFB*2)/samplerate)

# start = centFL ## start of the first filter = cutoff of the low pass

start = float((centFL*2)/samplerate) ## convert to radiants

end = 2*RcentFB - start



bB, aB = signal.butter(4,[start,end],btype='band',analog=False) ## fitler coefficients

YB = signal.filtfilt(bB,aB,d) ## fitler the signal

YB = YB/max(YB) ####normalize

G3 = 2**(X3/6)

YB = YB * G3 ### multiplicacion filtro tres

# scikits.audiolab.play(YB,samplerate)

# W3, h3 = signal.freqs(bB,aB)

## Band Pass Filter 2khz

centFB2 = 2000.0

RcentFB2 = float((centFB2*2)/samplerate)

start2 = end

end2 = 2*RcentFB2 - start2

bB2,aA2 = signal.butter(4,[start2, end2],btype = 'band', analog=False)

YB2 = signal.filtfilt(bB2,aA2,d)

YB2 = YB2/max(YB2)

G4 = 2**(X4/6)

YB2 = YB2 * G4 ### multiplicacion filtro 4

# scikits.audiolab.play(YB2,samplerate)

centFB3 = 8000.0

RcentFB3 = float((centFB3*2)/samplerate)

start3 = end2

end3 =  RH

bB3, aA3 = signal.butter(4,[start3, end3], btype='band',analog=False)

YB3 = signal.filtfilt(bB3,aA3,d)
YB3 = YB3/max(YB3)

G5 = 2**(X5/6) #### tranformacion de db a linear

YB3 = YB3 * G5 ### multiplicacion filtro 5

# scikits.audiolab.play(YB3,samplerate)



Equalizer = YL + YB + YB2 + YB3 + YH


#### Play Audio

1 个答案:

答案 0 :(得分:1)

如果您希望使用Container初始化file而不复制它们,那么您可以依赖std::ofstream的移动构造函数:

void insert(file &&f, int i)
{
    m.insert(std::pair<int,file>(i, std::move(f)));
}

然后你会做以下事情:

cont.insert(file{}, 0); // Construct a file when inserting, you already have an rvalue

或者:

file myfile;
myfile.open(...); // Do whatever you want with myfile...
cont.insert(std::move(myfile), 0); // And then move it, as long as you are not using if after

如果您的file不是可移动构造的,则可以在适当的位置构建它:

template <typename... Args>
void insert(int i, Args&&... args) {
    m.emplace(std::piecewise_construct, std::tuple<int>(i),
              std::tuple<Args>(std::forward<Args>(args)...));
}

另外,如果要插入默认初始化文件(如第一个示例中所示),您可以执行以下操作:

void insert(int i)
{
    m[i];
}