Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a possible solution using C++ classes and operator overloading to achieve the desired functionality:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <iostream> class Cost { public: Cost(int c) : value(c) {} const Cost& operator>>(const Square&) const { return *this; } const Cost& Inc() const { value++; return *this; } friend std::ostream& operator<<(std::ostream&, const Cost&); private: int value; }; class Square { public: const Cost& operator()(Cost& c) const { c.value *= c.value; return c; } }; std::ostream& operator<<(std::ostream& os, const Cost& c) { os << c.value; return os; } void Print(const Cost& c) { std::cout << c << std::endl; } |
1 2 3 4 5 | int main() { Cost c(5); (c >> Square() >> Inc() >> Inc()).Print(); return 0; } |
1 2 3 4 5 6 7 8 | class Cost { public: // ... void Print() const { std::cout << value << std::endl; } }; |
1 2 3 4 5 | int main() { Cost c(5); (c >> Square() >> Inc() >> Inc()).Print(); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> class Cost { public: Cost(int c) : value(c) {} const Cost& operator>>(const Square&) const { return *this; } const Cost& Inc() const { value++; return *this; } friend std::ostream& operator<<(std::ostream&, const Cost&); private: int value; }; class Square { public: const Cost& operator()(Cost& c) const { c.value *= c.value; return c; } }; std::ostream& operator<<(std::ostream& os, const Cost& c) { os << c.value; return os; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> class Cost { public: Cost(int c) : value(c) {} const Cost& operator>>(const Square&) const { return *this; } const Cost& Inc() const { value++; return *this; } friend std::ostream& operator<<(std::ostream&, const Cost&); private: int value; }; class Square { public: const Cost& operator()(Cost& c) const { c.value *= c.value; return c; } }; std::ostream& operator<<(std::ostream& os, const Cost& c) { os << c.value; return os; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:8:34: error: 'Square' does not name a type
8 | const Cost& operator>>(const Square&) const {
| ^~~~~~
main.cpp: In member function 'const Cost& Cost::Inc() const':
main.cpp:13:9: error: increment of member 'Cost::value' in read-only object
13 | value++;
| ^~~~~
main.cpp: In member function 'const Cost& Square::operator()(Cost&) const':
main.cpp:26:11: error: 'int Cost::value' is private within this context
26 | c.value *= c.value;
| ^~~~~
main.cpp:20:9: note: declared private here
20 | int value;
| ^~~~~
main.cpp:26:22: error: 'int Cost::value' is private within this context
26 | c.value *= c.value;
| ^~~~~
main.cpp:20:9: note: declared private here
20 | int value;
| ^~~~~
main.cpp: In function 'int main()':
main.cpp:39:1: error: 'Const' was not declared in this scope; did you mean 'Cost'?
39 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| Cost
main.cpp:39:25: error: 'Inc' was not declared in this scope
39 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~
main.cpp:39:58: error: 'Print' was not declared in this scope; did you mean 'printf'?
39 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| printf
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's an example of a possible implementation of the `Const`, `Square`, `Inc`, and `Print` classes in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> class Const { public: Const(int x) : value(x) {} int get_value() const { return value; } const Square operator>>(const Square& square) const { return Square(value * value); } const Inc operator>>(const Inc& inc) const { return Inc(value + 1); } private: int value; }; class Square { public: Square(int x) : value(x) {} int get_value() const { return value; } const Inc operator>>(const Inc& inc) const { return Inc(value); } friend std::ostream& operator<<(std::ostream&, const Square&); }; std::ostream& operator<<(std::ostream& os, const Square& square) { os << square.get_value(); return os; } class Inc { public: Inc(int x) : value(x) {} int get_value() const { return value; } friend std::ostream& operator<<(std::ostream&, const Inc&); }; std::ostream& operator<<(std::ostream& os, const Inc& inc) { os << inc.get_value(); return os; } int main() { Const c(5); Inc i = c >> Square() >> Inc() >> Inc(); std::cout << i; // prints 27 return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> class Const { public: Const(int x) : value(x) {} int get_value() const { return value; } const Square operator>>(const Square& square) const { return Square(value * value); } const Inc operator>>(const Inc& inc) const { return Inc(value + 1); } private: int value; }; class Square { public: Square(int x) : value(x) {} int get_value() const { return value; } const Inc operator>>(const Inc& inc) const { return Inc(value); } friend std::ostream& operator<<(std::ostream&, const Square&); }; std::ostream& operator<<(std::ostream& os, const Square& square) { os << square.get_value(); return os; } class Inc { public: Inc(int x) : value(x) {} int get_value() const { return value; } friend std::ostream& operator<<(std::ostream&, const Inc&); }; std::ostream& operator<<(std::ostream& os, const Inc& inc) { os << inc.get_value(); return os; } int main() { Const c(5); Inc i = c >> Square() >> Inc() >> Inc(); std::cout << i; // prints 27 return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <iostream> class Const { public: Const(int x) : value(x) {} int get_value() const { return value; } const Square operator>>(const Square& square) const { return Square(value * value); } const Inc operator>>(const Inc& inc) const { return Inc(value + 1); } private: int value; }; class Square { public: Square(int x) : value(x) {} int get_value() const { return value; } const Inc operator>>(const Inc& inc) const { return Inc(value); } friend std::ostream& operator<<(std::ostream&, const Square&); }; std::ostream& operator<<(std::ostream& os, const Square& square) { os << square.get_value(); return os; } class Inc { public: Inc(int x) : value(x) {} int get_value() const { return value; } friend std::ostream& operator<<(std::ostream&, const Inc&); }; std::ostream& operator<<(std::ostream& os, const Inc& inc) { os << inc.get_value(); return os; } int __delete_this__main() { Const c(5); Inc i = c >> Square() >> Inc() >> Inc(); std::cout << i; // prints 27 return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:10:11: error: 'Square' does not name a type
10 | const Square operator>>(const Square& square) const {
| ^~~~~~
main.cpp:14:11: error: 'Inc' does not name a type
14 | const Inc operator>>(const Inc& inc) const {
| ^~~
main.cpp:28:11: error: 'Inc' does not name a type
28 | const Inc operator>>(const Inc& inc) const {
| ^~~
main.cpp: In constructor 'Square::Square(int)':
main.cpp:24:21: error: class 'Square' does not have any field named 'value'
24 | Square(int x) : value(x) {}
| ^~~~~
main.cpp: In member function 'int Square::get_value() const':
main.cpp:26:36: error: 'value' was not declared in this scope
26 | int get_value() const { return value; }
| ^~~~~
main.cpp: In constructor 'Inc::Inc(int)':
main.cpp:42:18: error: class 'Inc' does not have any field named 'value'
42 | Inc(int x) : value(x) {}
| ^~~~~
main.cpp: In member function 'int Inc::get_value() const':
main.cpp:44:36: error: 'value' was not declared in this scope
44 | int get_value() const { return value; }
| ^~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:56:25: error: no matching function for call to 'Square::Square()'
56 | Inc i = c >> Square() >> Inc() >> Inc();
| ^
main.cpp:24:5: note: candidate: 'Square::Square(int)'
24 | Square(int x) : value(x) {}
| ^~~~~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(const Square&)'
22 | class Square {
| ^~~~~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:56:34: error: no matching function for call to 'Inc::Inc()'
56 | Inc i = c >> Square() >> Inc() >> Inc();
| ^
main.cpp:42:5: note: candidate: 'Inc::Inc(int)'
42 | Inc(int x) : value(x) {}
| ^~~
main.cpp:42:5: note: candidate expects 1 argument, 0 provided
main.cpp:40:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
40 | class Inc {
| ^~~
main.cpp:40:7: note: candidate expects 1 argument, 0 provided
main.cpp:40:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:40:7: note: candidate expects 1 argument, 0 provided
main.cpp:56:43: error: no matching function for call to 'Inc::Inc()'
56 | Inc i = c >> Square() >> Inc() >> Inc();
| ^
main.cpp:42:5: note: candidate: 'Inc::Inc(int)'
42 | Inc(int x) : value(x) {}
| ^~~
main.cpp:42:5: note: candidate expects 1 argument, 0 provided
main.cpp:40:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
40 | class Inc {
| ^~~
main.cpp:40:7: note: candidate expects 1 argument, 0 provided
main.cpp:40:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:40:7: note: candidate expects 1 argument, 0 provided
main.cpp: In function 'int main()':
main.cpp:64:20: error: no matching function for call to 'Square::Square()'
64 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:24:5: note: candidate: 'Square::Square(int)'
24 | Square(int x) : value(x) {}
| ^~~~~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(const Square&)'
22 | class Square {
| ^~~~~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:64:29: error: no matching function for call to 'Inc::Inc()'
64 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:42:5: note: candidate: 'Inc::Inc(int)'
42 | Inc(int x) : value(x) {}
| ^~~
main.cpp:42:5: note: candidate expects 1 argument, 0 provided
main.cpp:40:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
40 | class Inc {
| ^~~
main.cpp:40:7: note: candidate expects 1 argument, 0 provided
main.cpp:40:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:40:7: note: candidate expects 1 argument, 0 provided
main.cpp:64:41: error: no matching function for call to 'Square::Square()'
64 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:24:5: note: candidate: 'Square::Square(int)'
24 | Square(int x) : value(x) {}
| ^~~~~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(const Square&)'
22 | class Square {
| ^~~~~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:64:53: error: no matching function for call to 'Square::Square()'
64 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:24:5: note: candidate: 'Square::Square(int)'
24 | Square(int x) : value(x) {}
| ^~~~~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(const Square&)'
22 | class Square {
| ^~~~~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:64:58: error: 'Print' was not declared in this scope; did you mean 'printf'?
64 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| printf
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a possible implementation of the `Const`, `Square`, `Inc`, and `Print` classes in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <functional> class Const { public: Const(int v) : val(v) {} int get_val() const { return val; } Const& operator>>(const std::function<Const&(Const&)>& f) { *this = f(*this); return *this; } private: int val; }; class Square { public: Const operator()(Const c) { return Const(c.get_val() * c.get_val()); } }; class Inc { public: Const operator()(Const c) { return Const(c.get_val() + 1); } }; class Print { public: void operator()(Const c) { std::cout << c.get_val() << std::endl; } }; |
1 2 3 4 5 | int main() { Const c(5); c >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class Const { public: Const(int v) : val(v) {} int get_val() const { return val; } Const& operator>>(const std::function<Const&(Const&)>& f) { *this = f(*this); return *this; } Const& square() { val = val * val; return *this; } Const& inc() { val++; return *this; } void print() const { std::cout << val << std::endl; } private: int val; }; |
1 2 3 4 5 | int main() { Const c(5); c >> Const::square >> Const::inc >> Const::inc >> Const::print; return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <functional> class Const { public: Const(int v) : val(v) {} int get_val() const { return val; } Const& operator>>(const std::function<Const&(Const&)>& f) { *this = f(*this); return *this; } private: int val; }; class Square { public: Const operator()(Const c) { return Const(c.get_val() * c.get_val()); } }; class Inc { public: Const operator()(Const c) { return Const(c.get_val() + 1); } }; class Print { public: void operator()(Const c) { std::cout << c.get_val() << std::endl; } }; |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <functional> class Const { public: Const(int v) : val(v) {} int get_val() const { return val; } Const& operator>>(const std::function<Const&(Const&)>& f) { *this = f(*this); return *this; } private: int val; }; class Square { public: Const operator()(Const c) { return Const(c.get_val() * c.get_val()); } }; class Inc { public: Const operator()(Const c) { return Const(c.get_val() + 1); } }; class Print { public: void operator()(Const c) { std::cout << c.get_val() << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:46:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
46 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:13:12: note: candidate: 'Const& Const::operator>>(const std::function<Const&(Const&)>&)'
13 | Const& operator>>(const std::function<Const&(Const&)>& f) {
| ^~~~~~~~
main.cpp:13:60: note: no known conversion for argument 1 from 'Square' to 'const std::function<Const&(Const&)>&'
13 | Const& operator>>(const std::function<Const&(Const&)>& f) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a possible implementation of the `Const`, `Square`, `Inc`, and `Print` classes in C++ that would satisfy the given example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <functional> class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } private: int value_; }; class Square { public: int operator()(int value) const { return value * value; } }; class Inc { public: int operator()(int value) const { return value + 1; } }; class Print { public: void operator()(int value) const { std::cout << value << std::endl; } }; int main() { Const c(5); Square s; Inc i; Print p; std::function<int(int)> f = c; f = f >> s >> i >> i >> p; f(c.value()); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <functional> class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } private: int value_; }; class Square { public: int operator()(int value) const { return value * value; } }; class Inc { public: int operator()(int value) const { return value + 1; } }; class Print { public: void operator()(int value) const { std::cout << value << std::endl; } }; int main() { Const c(5); Square s; Inc i; Print p; std::function<int(int)> f = c; f = f >> s >> i >> i >> p; f(c.value()); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> #include <functional> class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } private: int value_; }; class Square { public: int operator()(int value) const { return value * value; } }; class Inc { public: int operator()(int value) const { return value + 1; } }; class Print { public: void operator()(int value) const { std::cout << value << std::endl; } }; int __delete_this__main() { Const c(5); Square s; Inc i; Print p; std::function<int(int)> f = c; f = f >> s >> i >> i >> p; f(c.value()); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int __delete_this__main()':
main.cpp:35:33: error: conversion from 'Const' to non-scalar type 'std::function<int(int)>' requested
35 | std::function<int(int)> f = c;
| ^
main.cpp:36:11: error: no match for 'operator>>' (operand types are 'std::function<int(int)>' and 'Square')
36 | f = f >> s >> i >> i >> p;
| ~ ^~ ~
| | |
| | Square
| std::function<int(int)>
In file included from /usr/include/c++/11/string:56,
from /usr/include/c++/11/bits/locale_classes.h:40,
from /usr/include/c++/11/bits/ios_base.h:41,
from /usr/include/c++/11/ios:42,
from /usr/include/c++/11/ostream:38,
from /usr/include/c++/11/iostream:39,
from main.cpp:2:
/usr/include/c++/11/bits/basic_string.tcc:1485:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
1485 | operator>>(basic_istream<_CharT, _Traits>& __in,
| ^~~~~~~~
/usr/include/c++/11/bits/basic_string.tcc:1485:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<_CharT, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/istream:1016,
from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/bits/istream.tcc:958:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)'
958 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
| ^~~~~~~~
/usr/include/c++/11/bits/istream.tcc:958:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<_CharT, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/istream:756:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)'
756 | operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
| ^~~~~~~~
/usr/include/c++/11/istream:756:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<char, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/istream:761:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)'
761 | operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
| ^~~~~~~~
/usr/include/c++/11/istream:761:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<char, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/istream:803:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT*)'
803 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
| ^~~~~~~~
/usr/include/c++/11/istream:803:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<_CharT, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/istream:846:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)'
846 | operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
| ^~~~~~~~
/usr/include/c++/11/istream:846:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<char, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/istream:852:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)'
852 | operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
| ^~~~~~~~
/usr/include/c++/11/istream:852:5: note: template argument deduction/substitution failed:
main.cpp:36:14: note: 'std::function<int(int)>' is not derived from 'std::basic_istream<char, _Traits>'
36 | f = f >> s >> i >> i >> p;
| ^
In file included from /usr/include/c++/11/iostream:40,
from main.cpp:2:
/usr/include/c++/11/istream:1006:5: note: candidate: 'template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&)'
1006 | operator>>(_Istream&& __is, _Tp&& __x)
| ^~~~~~~~
/usr/include/c++/11/istream:1006:5: note: template argument deduction/substitution failed:
/usr/include/c++/11/istream: In substitution of 'template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::function<int(int)>&; _Tp = Square&]':
main.cpp:36:14: required from here
/usr/include/c++/11/istream:1006:5: error: no type named 'type' in 'struct std::enable_if<false, void>'
main.cpp: In function 'int main()':
main.cpp:46:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
46 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a possible implementation of the `Const`, `Square`, `Inc`, and `Print` classes in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> class Const { public: Const(int v) : value(v) {} int getValue() const { return value; } Const& operator>>(Square&) const { return Square(*this) >> Inc(); } private: int value; }; class Square { public: Square(Const& c) : value(c.getValue() * c.getValue()) {} int getValue() const { return value; } Square& operator>>(Inc&) const { return Inc(Square(*this)); } private: int value; }; class Inc { public: Inc(Square& s) : value(s.getValue() + 1) {} Inc(Inc& i) : value(i.getValue() + 1) {} int getValue() const { return value; } friend std::ostream& operator<<(std::ostream&, const Inc&); private: int value; }; std::ostream& operator<<(std::ostream& os, const Inc& i) { os << i.getValue(); return os; } void Print(Inc& i) { std::cout << i; } int main() { Const c(5); Print(c >> Square() >> Inc() >> Inc()); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> class Const { public: Const(int v) : value(v) {} int getValue() const { return value; } Const& operator>>(Square&) const { return Square(*this) >> Inc(); } private: int value; }; class Square { public: Square(Const& c) : value(c.getValue() * c.getValue()) {} int getValue() const { return value; } Square& operator>>(Inc&) const { return Inc(Square(*this)); } private: int value; }; class Inc { public: Inc(Square& s) : value(s.getValue() + 1) {} Inc(Inc& i) : value(i.getValue() + 1) {} int getValue() const { return value; } friend std::ostream& operator<<(std::ostream&, const Inc&); private: int value; }; std::ostream& operator<<(std::ostream& os, const Inc& i) { os << i.getValue(); return os; } void Print(Inc& i) { std::cout << i; } int main() { Const c(5); Print(c >> Square() >> Inc() >> Inc()); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <iostream> class Const { public: Const(int v) : value(v) {} int getValue() const { return value; } Const& operator>>(Square&) const { return Square(*this) >> Inc(); } private: int value; }; class Square { public: Square(Const& c) : value(c.getValue() * c.getValue()) {} int getValue() const { return value; } Square& operator>>(Inc&) const { return Inc(Square(*this)); } private: int value; }; class Inc { public: Inc(Square& s) : value(s.getValue() + 1) {} Inc(Inc& i) : value(i.getValue() + 1) {} int getValue() const { return value; } friend std::ostream& operator<<(std::ostream&, const Inc&); private: int value; }; std::ostream& operator<<(std::ostream& os, const Inc& i) { os << i.getValue(); return os; } void Print(Inc& i) { std::cout << i; } int __delete_this__main() { Const c(5); Print(c >> Square() >> Inc() >> Inc()); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:8:23: error: 'Square' has not been declared
8 | Const& operator>>(Square&) const { return Square(*this) >> Inc(); }
| ^~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&) const':
main.cpp:8:47: error: 'Square' was not declared in this scope
8 | Const& operator>>(Square&) const { return Square(*this) >> Inc(); }
| ^~~~~~
main.cpp:8:64: error: 'Inc' was not declared in this scope
8 | Const& operator>>(Square&) const { return Square(*this) >> Inc(); }
| ^~~
main.cpp: At global scope:
main.cpp:17:24: error: 'Inc' has not been declared
17 | Square& operator>>(Inc&) const { return Inc(Square(*this)); }
| ^~~
main.cpp: In member function 'Square& Square::operator>>(int&) const':
main.cpp:17:45: error: 'Inc' was not declared in this scope
17 | Square& operator>>(Inc&) const { return Inc(Square(*this)); }
| ^~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:41:23: error: no matching function for call to 'Square::Square()'
41 | Print(c >> Square() >> Inc() >> Inc());
| ^
main.cpp:15:5: note: candidate: 'Square::Square(Const&)'
15 | Square(Const& c) : value(c.getValue() * c.getValue()) {}
| ^~~~~~
main.cpp:15:5: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(const Square&)'
13 | class Square {
| ^~~~~~
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:41:32: error: no matching function for call to 'Inc::Inc()'
41 | Print(c >> Square() >> Inc() >> Inc());
| ^
main.cpp:25:5: note: candidate: 'Inc::Inc(Inc&)'
25 | Inc(Inc& i) : value(i.getValue() + 1) {}
| ^~~
main.cpp:25:5: note: candidate expects 1 argument, 0 provided
main.cpp:24:5: note: candidate: 'Inc::Inc(Square&)'
24 | Inc(Square& s) : value(s.getValue() + 1) {}
| ^~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp:41:41: error: no matching function for call to 'Inc::Inc()'
41 | Print(c >> Square() >> Inc() >> Inc());
| ^
main.cpp:25:5: note: candidate: 'Inc::Inc(Inc&)'
25 | Inc(Inc& i) : value(i.getValue() + 1) {}
| ^~~
main.cpp:25:5: note: candidate expects 1 argument, 0 provided
main.cpp:24:5: note: candidate: 'Inc::Inc(Square&)'
24 | Inc(Square& s) : value(s.getValue() + 1) {}
| ^~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp: In function 'int main()':
main.cpp:48:20: error: no matching function for call to 'Square::Square()'
48 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:15:5: note: candidate: 'Square::Square(Const&)'
15 | Square(Const& c) : value(c.getValue() * c.getValue()) {}
| ^~~~~~
main.cpp:15:5: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(const Square&)'
13 | class Square {
| ^~~~~~
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:48:29: error: no matching function for call to 'Inc::Inc()'
48 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:25:5: note: candidate: 'Inc::Inc(Inc&)'
25 | Inc(Inc& i) : value(i.getValue() + 1) {}
| ^~~
main.cpp:25:5: note: candidate expects 1 argument, 0 provided
main.cpp:24:5: note: candidate: 'Inc::Inc(Square&)'
24 | Inc(Square& s) : value(s.getValue() + 1) {}
| ^~~
main.cpp:24:5: note: candidate expects 1 argument, 0 provided
main.cpp:48:41: error: no matching function for call to 'Square::Square()'
48 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:15:5: note: candidate: 'Square::Square(Const&)'
15 | Square(Const& c) : value(c.getValue() * c.getValue()) {}
| ^~~~~~
main.cpp:15:5: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(const Square&)'
13 | class Square {
| ^~~~~~
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:48:53: error: no matching function for call to 'Square::Square()'
48 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:15:5: note: candidate: 'Square::Square(Const&)'
15 | Square(Const& c) : value(c.getValue() * c.getValue()) {}
| ^~~~~~
main.cpp:15:5: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(const Square&)'
13 | class Square {
| ^~~~~~
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:13:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:13:7: note: candidate expects 1 argument, 0 provided
main.cpp:48:63: error: too few arguments to function 'void Print(Inc&)'
48 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~^~
main.cpp:37:6: note: declared here
37 | void Print(Inc& i) { std::cout << i; }
| ^~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False