I have a class ‘Token’ and a class ‘Token_Stream’, as shown in code below. When I try to create an object of class Token_Stream, I get a compile error: (C2280) 'Token_stream::Token_stream(void)': attempting to reference a deleted function
I'm using C++ Visual Studio Community 15.9.1. I am new to "modern" programming and teaching myself C++ from Stroustrup's book PPP C++ 2nd edition. I have read many of the results from searching this site for "c++ attempting to reference a deleted function c2280," but none have helped me solve the problem.
My understanding of constructors is incomplete, despite reading several webpages and book sections about them, but I understand enough (I think) to think this is a constructor problem. I am definitely struggling with the user-defined class that includes a member that is another user-defined class plus member functions.
Code is below. If the declarations of the two member functions of Token_stream would be helpful, let me know and I'll add them.
class Token {
public:
char kind;
double value;
Token(char ch) // make a Token from a char
//- this is a constructor, right?
:kind(ch), value(0) { } // what do the braces here mean?
Token(char ch, double val) // make a Token from a char and a double
:kind(ch), value(val) { }
};
class Token_stream {
public:
Token get();
void putback(Token t);
private:
bool full{ false };
Token buffer;
// Token_stream() = default;
};
Token_stream ts; // ERROR C2280 here
// Token_stream::Token_stream(void)': attempting
// to ref. a deleted function
The problem is your Token
class. You don't have a default-constructor of Token
but you have a member-attribute Token buffer;
in Token_stream
which will be initialized on instantiation of Token_stream
. There is no default constructor for Token
. Therefore the compiler can't add an default-constructor for Token_stream
.
gcc 8.2 shows the following error/note:
note: 'Token_stream::Token_stream()' is implicitly deleted because the default
definition would be ill-formed
error: no matching function for call to 'Token::Token()'
This shows the problem with creating a default constructor for Token_stream
.
You can simply add a default-constructor for Token
and it should work.enter code here