Hi, So I\'m having an issue with the code that I\'ve made for my Object Oriented
ID: 3698783 • Letter: H
Question
Hi,
So I'm having an issue with the code that I've made for my Object Oriented Programming class. I'm trying to get through one of the challenges MyProgrammingLab 70046 to be exact. I keep getting errors:
Severity Code Description Project File Line Suppression State
Error (active) function "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits]" (declared at line 1035 of "c:Program Files (x86)Microsoft Visual Studio 14.0VCincludestream") cannot be referenced -- it is a deleted function ConsoleApplication3 \acad.dvuadmin.netsceHOMEDIRD40724303DocumentsVisual Studio 2015ConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp 127
Severity Code Description Project File Line Suppression State
Error (active) function "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits]" (declared at line 1035 of "c:Program Files (x86)Microsoft Visual Studio 14.0VCincludestream") cannot be referenced -- it is a deleted function ConsoleApplication3 \acad.dvuadmin.netsceHOMEDIRD40724303DocumentsVisual Studio 2015ConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp 139
Severity Code Description Project File Line Suppression State
Error (active) function "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits]" (declared at line 1035 of "c:Program Files (x86)Microsoft Visual Studio 14.0VCincludestream") cannot be referenced -- it is a deleted function ConsoleApplication3 \acad.dvuadmin.netsceHOMEDIRD40724303DocumentsVisual Studio 2015ConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp 150
Severity Code Description Project File Line Suppression State
Error C2280 'std::basic_ofstream>::basic_ofstream(const std::basic_ofstream> &)': attempting to reference a deleted function ConsoleApplication3 \acad.dvuadmin.netscehomedird40724303documentsisual studio 2015consoleapplication3consoleapplication3consoleapplication3.cpp 127
I keep getting a red line under ofs in the main. I bolded the spots where I'm having the issue. Here's my code so far:
#include
#include
#include
using namespace std;
class Filter
{
private:
istream& is;
ostream& os;
public:
Filter(void) : is(cin), os(cout)
{
}
Filter(ifstream& ifs, ofstream& ofs) : is(ifs), os(ofs)
{
}
virtual char transform(char c) const = 0;
void doFilter(void)
{
char ch = '?';
while (is.peek() >-1);
{
ch = is.get();
os << transform(ch);
}
}
};
class Copy : public Filter
{
public:
Copy(void) : Filter()
{
}
Copy(ifstream& ifs, ofstream ofs) : Filter(ifs, ofs)
{
}
virtual char transform(char ch) const
{
return ch;
}
};
class Encr :public Filter
{
public:
Encr(void) : Filter()
{
}
Encr(ifstream& ifs, ofstream ofs) : Filter(ifs, ofs)
{
}
virtual char transform(char ch) const
{
if (ch >= 'A' && ch <= 'Z')
{
ch = ch - 65;
ch = ch + 13;
ch = ch % 26;
ch = ch + 65;
}
else if (ch >= 'a' && ch <= 'z')
{
ch = ch - 97;
ch = ch + 13;
ch = ch % 26;
ch = ch + 97;
}
else
{
return ch;
}
}
};
class Upper : public Filter
{
public:
Upper(void) : Filter()
{
}
Upper(ifstream& ifs, ofstream ofs) : Filter(ifs, ofs)
{
}
virtual char transform(char ch) const
{
if (ch >= 'a' && ch <= 'z')
return toupper(ch);
else
return ch;
}
};
int main(void)
{
Filter* fp;
string ifname = "input.txt";
string ofname;
ifstream ifs;
ofstream ofs;
ifs.open(ifname.c_str());
ofname = ifname + "_copy";
ofs.open(ofname.c_str());
if (ifs.is_open() && ofs.is_open())
{
fp = new Copy(ifs, ofs);
fp->doFilter();
ifs.close();
ofs.close();
delete fp;
}
ifs.open(ifname.c_str());
ofname = ifname + "_encr";
ofs.open(ofname.c_str());
if (ifs.is_open() && ofs.is_open())
{
fp = new Encr(ifs, ofs);
fp->doFilter();
ifs.close();
ofs.close();
delete fp;
}
ifs.open(ifname.c_str());
ofname = ifname + "_upper";
ofs.open(ofname.c_str());
if (ifs.is_open() && ofs.is_open())
{
fp = new Upper(ifs, ofs);
fp->doFilter();
ifs.close();
ofs.close();
delete fp;
}
return 0;
}
Any help would be greatly appreciated!
Explanation / Answer
A deleted function is the one which is being explicitly disabled. Your error clearly tells that function basic_ofstream is disabled. The reason is that whenever you call Upper()/Encr() the flow is as below:
Call Function:
Encr(ifs, ofs);
Upper(ifs, ofs);
Function Signature:
Encr(ifstream& ifs, ofstream ofs) : Filter(ifs, ofs)
Upper(ifstream& ifs, ofstream ofs) : Filter(ifs, ofs)
In this execution when function is called, ifs and ofs passed in the calling statements are copied to local copy of ifs and ofs in the function definition i.e. a local copy is being created and copy of ifstream/ofstream is not allowed. So similar to ifs, pass ofs with & to make this work.
Hope this works. Good Luck!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.