简体   繁体   English

将迭代器与对象向量一起使用c ++

[英]using iterator with a vector of objects c++

i have class contains a ofstream file, i face problem when using iterator to delete an object of a vector of objects of this class. 我有一个类包含一个ofstream文件,使用迭代器删除该类对象向量的对象时遇到问题。

Here is the error: 这是错误:

Error 10 error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>' c:\\program files (x86)\\microsoft visual studio 10.0\\vc\\include\\ostream

Code: 码:

static vector<VOIP> calls;
...
for(...)
{
    VOIP v = VOIP(...);
    calls.push_back(v);
}
...
for( int i = 0 ; i < calls.size() ; i++)
{
    if(...)
    {
        vector<VOIP>::iterator it = calls.begin() + i;
        calls.erase(it);//"?"
        break;
    }
}

Face Error C2249 raises from line "?", any help and this is the class Face Error C2249从行“?”引发任何帮助,这是该类

class VOIP
{
public:
    VOIP(string SourceDirectoryPath, string startTime, signalling sig, int callerIp[], int calleeIp[], int callerPort, int calleePort);
    VOIP(const VOIP & voip);
    ~VOIP(void);
    //caller and callee voice vectors 
    vector<u_char> callerVoiceVector;
    vector<u_char> calleeVoiceVector;
    //caller and callee voice files and path
    ofstream callerVoiceFile;
    ofstream calleeVoiceFile;   
    string outFilePath;
    string log;
    ofstream logFile;
    string startTime;
    string endTime;
    string length;
}

commented line in copy constructor raises the same error, ie Error C2249 复制构造函数中的带注释的行会引发相同的错误,即错误C2249

VOIP::VOIP(const VOIP & voip)
    /*:calleePort(voip.calleePort), calleeVoiceFile(voip.calleeVoiceFile), calleeVoiceVector(voip.calleeVoiceVector),
     callerPort(voip.callerPort), callerVoiceFile(voip.callerVoiceFile), callerVoiceVector(voip.callerVoiceVector),
     callType(voip.callType), endTime(voip.endTime), length(voip.length), log(voip.log), logFile(voip.logFile),
     outFilePath(voip.outFilePath), startTime(voip.startTime),
     pleaseDial(voip.pleaseDial), DLHmm(voip.DLHmm)//signalings*///TODO
{
    cout<<"inside copy constractor"<<endl;
}

VOIP::~VOIP(void)
{
    callerVoiceVector.clear();
    calleeVoiceVector.clear();
    callerVoiceFile.close();
    calleeVoiceFile.close();
    logFile.close();
    cout<<"inside destractor"<<endl;
}

The standard library containers require, in general, that the objects they contain can be copy constructible and copy assignable. 通常,标准库容器要求它们包含的对象可以是可复制构造的和可复制分配的。 This is, they require access to the copy constructor (in your case, VOIP::VOIP(const VOIP&) ) and the copy assignment operator (in your case, VOIP::operator=(const VOIP&) ). 这就是说,它们需要访问副本构造函数(在您的情况下为VOIP::VOIP(const VOIP&) )和副本分配运算符(在您的情况下为VOIP::operator=(const VOIP&) )。 If your class does not provide them explicitly, these functions are automatically generated by your compiler as long as it can do it: this is, as long as all class members are also copy constructible or copy assignable, which is not the case for VOIP , since ofstream cannot be copied. 如果您的类没有明确提供它们,则编译器会自动生成这些函数:只要所有类成员都是可复制构造的或可复制分配的,这就是VOIP的情况,由于ofstream无法复制。

Nevertheless, VOIP provides an explicit copy constructor, so no problems there. 尽管如此, VOIP提供了一个显式的复制构造函数,因此没有问题。 This is the reason why vector<VOIP>::push_back() , which needs the copy constructor, works. 这就是需要复制构造函数的vector<VOIP>::push_back()起作用的原因。 Unfortunately it does not provide a copy assignment operator and, strange as it seems, vector<VOIP>::erase() needs it. 不幸的是,它没有提供拷贝分配运算符,而且看起来很奇怪, vector<VOIP>::erase()需要它。 The reason is that, when you erase an element from the vector, all elements after it must be copied to the previous position in order to fill the gap. 原因是,当从向量中删除一个元素时,必须将其后的所有元素复制到先前的位置以填补空白。 And this is what the error message is telling you: ofstream is not copyable because its base class std::basic_ios<> is not copyable, so VOIP is not copyable. 这就是错误消息告诉您的内容: ofstream不可复制,因为其基类std::basic_ios<>不可复制,因此VOIP不可复制。

To solve your problem, you may provide a copy assignment operator for VOIP following the same ideas you used for the copy constructor. 为了解决您的问题,您可以按照用于复制构造函数的相同思路,为VOIP提供一个复制分配运算符。 Incidentally, in C++ there is something called the rule of three : if a class has a explicit destructor, copy constructor or copy assignment operator, it usually needs all three of them. 顺便说一句,在C ++中,有一种叫做规则 :如果一个类具有显式的析构函数,复制构造函数或复制赋值运算符,则通常需要全部三个。 VOIP has the first two, but misses the third. VOIP有前两个,但没有第三个。 Write it and everything will be fine. 写出来,一切都会好起来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM