Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is a possible implementation of the classes `Cost`, `Square`, and `Print` in C++ that would allow you to write the expression `Cost(5) >> Square() >> Inc() >> Print()` and have it print `26` to the screen:
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 | #include <iostream> class Cost { public: Cost(int c) : value(c) {} Cost& operator>>(Square& s) { value = value * value; return *this; } friend class Print; private: int value; }; class Square { public: friend Cost; Square& operator>>(Inc& i) { return i; } }; class Inc { public: friend Square; Inc& operator>>(Print& p) { value++; return p; } friend class Print; private: int value = 0; }; class Print { public: friend Inc; Print& operator>>(Cost& c) { c.value = value; return *this; } void operator()() { std::cout << value << std::endl; } }; int main() { Cost(5) >> Square() >> Inc() >> Print(); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Inc { public: friend Square; Inc& operator>>(Print& p) { value++; p.value = value; return p; } friend class Print; private: int value = 0; }; |
1 |
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 | #include <iostream> class Cost { public: Cost(int c) : value(c) {} Cost& operator>>(Square& s) { value = value * value; return *this; } friend class Print; private: int value; }; class Square { public: friend Cost; Square& operator>>(Inc& i) { return i; } }; class Inc { public: friend Square; Inc& operator>>(Print& p) { value++; p.value = value; return p; } friend class Print; private: int value = 0; }; class Print { public: friend Inc; Print& operator>>(Cost& c) { c.value = value; return *this; } void operator()() { std::cout << value << 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 48 49 50 51 52 53 54 55 | #include <iostream> class Cost { public: Cost(int c) : value(c) {} Cost& operator>>(Square& s) { value = value * value; return *this; } friend class Print; private: int value; }; class Square { public: friend Cost; Square& operator>>(Inc& i) { return i; } }; class Inc { public: friend Square; Inc& operator>>(Print& p) { value++; p.value = value; return p; } friend class Print; private: int value = 0; }; class Print { public: friend Inc; Print& operator>>(Cost& c) { c.value = value; return *this; } void operator()() { std::cout << value << std::endl; } }; |
main.cpp:8:20: error: 'Square' has not been declared
8 | Cost& operator>>(Square& s) {
| ^~~~~~
main.cpp:23:22: error: 'Inc' has not been declared
23 | Square& operator>>(Inc& i) {
| ^~~
main.cpp: In member function 'Square& Square::operator>>(int&)':
main.cpp:24:12: error: invalid initialization of reference of type 'Square&' from expression of type 'int'
24 | return i;
| ^
main.cpp: At global scope:
main.cpp:32:19: error: 'Print' has not been declared
32 | Inc& operator>>(Print& p) {
| ^~~~~
main.cpp: In member function 'Inc& Inc::operator>>(int&)':
main.cpp:34:7: error: request for member 'value' in 'p', which is of non-class type 'int'
34 | p.value = value;
| ^~~~~
main.cpp:35:12: error: invalid initialization of reference of type 'Inc&' from expression of type 'int'
35 | return p;
| ^
main.cpp: In member function 'Print& Print::operator>>(Cost&)':
main.cpp:49:15: error: 'value' was not declared in this scope
49 | c.value = value;
| ^~~~~
main.cpp: In member function 'void Print::operator()()':
main.cpp:54:18: error: 'value' was not declared in this scope
54 | std::cout << value << std::endl;
| ^~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is one possible implementation of the `Const`, `Square`, and `Print` classes in C++ that would allow you to use the expression `Cost(5) >> Square() >> Inc() >> Print()` and print the value `26` to the screen:
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 | #include <iostream> class Const { public: Const(int v) : value(v) {} int get_value() const { return value; } Const& operator>>(Square& s) const { s.set_value(value * value); return s; } private: int value; }; class Square { public: Square& operator>>(Inc& i) const { i.set_value(value); return i; } void set_value(int v) { value = v; } private: int value; }; class Inc { public: Inc& operator>>(Print& p) const { p.set_value(value + 1); return p; } void set_value(int v) { value = v; } private: int value; }; class Print { public: operator Const() const { return Const(value); } void set_value(int v) { value = v; std::cout << value << std::endl; } private: int value; }; int main() { Const(5) >> Square() >> Inc() >> Print(); return 0; } |
1 | Const(5) >> Square() >> Inc() >> Print(); |
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 | #include <iostream> class Const { public: Const(int v) : value(v) {} int get_value() const { return value; } Const& operator>>(Square& s) const { s.set_value(value * value); return s; } private: int value; }; class Square { public: Square& operator>>(Inc& i) const { i.set_value(value); return i; } void set_value(int v) { value = v; } private: int value; }; class Inc { public: Inc& operator>>(Print& p) const { p.set_value(value + 1); return p; } void set_value(int v) { value = v; } private: int value; }; class Print { public: operator Const() const { return Const(value); } void set_value(int v) { value = v; std::cout << value << std::endl; } private: int value; }; |
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 | #include <iostream> class Const { public: Const(int v) : value(v) {} int get_value() const { return value; } Const& operator>>(Square& s) const { s.set_value(value * value); return s; } private: int value; }; class Square { public: Square& operator>>(Inc& i) const { i.set_value(value); return i; } void set_value(int v) { value = v; } private: int value; }; class Inc { public: Inc& operator>>(Print& p) const { p.set_value(value + 1); return p; } void set_value(int v) { value = v; } private: int value; }; class Print { public: operator Const() const { return Const(value); } void set_value(int v) { value = v; std::cout << value << std::endl; } private: int value; }; |
main.cpp:8:21: error: 'Square' has not been declared
8 | Const& operator>>(Square& s) const {
| ^~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&) const':
main.cpp:9:7: error: request for member 'set_value' in 's', which is of non-class type 'int'
9 | s.set_value(value * value);
| ^~~~~~~~~
main.cpp:10:12: error: cannot bind non-const lvalue reference of type 'Const&' to an rvalue of type 'Const'
10 | return s;
| ^
main.cpp:6:3: note: after user-defined conversion: 'Const::Const(int)'
6 | Const(int v) : value(v) {}
| ^~~~~
main.cpp: At global scope:
main.cpp:18:22: error: 'Inc' has not been declared
18 | Square& operator>>(Inc& i) const {
| ^~~
main.cpp: In member function 'Square& Square::operator>>(int&) const':
main.cpp:19:7: error: request for member 'set_value' in 'i', which is of non-class type 'int'
19 | i.set_value(value);
| ^~~~~~~~~
main.cpp:20:12: error: invalid initialization of reference of type 'Square&' from expression of type 'int'
20 | return i;
| ^
main.cpp: At global scope:
main.cpp:29:19: error: 'Print' has not been declared
29 | Inc& operator>>(Print& p) const {
| ^~~~~
main.cpp: In member function 'Inc& Inc::operator>>(int&) const':
main.cpp:30:7: error: request for member 'set_value' in 'p', which is of non-class type 'int'
30 | p.set_value(value + 1);
| ^~~~~~~~~
main.cpp:31:12: error: invalid initialization of reference of type 'Inc&' from expression of type 'int'
31 | return p;
| ^
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is one possible implementation of the `Const`, `Square`, and `Print` classes in C++ that would allow you to write the code you described:
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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } Const& operator>>(Square& square) const { square.set_value(value_); return square; } private: int value_; }; class Square { public: Square& operator>>(Inc& inc) { value_ = value_*value_; return inc; } Square& operator>>(Print& print) { value_ = value_*value_; print.set_value(value_); return print; } private: int value_ = 0; friend class Const; void set_value(int value) { value_ = value; } }; class Inc { public: Inc& operator>>(Print& print) { value_ += 1; print.set_value(value_); return print; } private: int value_ = 0; friend class Square; }; class Print { public: Print& operator>>(const Print&) { std::cout << value_ << std::endl; return *this; } private: int value_ = 0; void set_value(int value) { value_ = value; } friend class Inc; }; int main() { Const c(5); c >> Square() >> Inc() >> Print(); return 0; } |
1 2 | Const c(5); c >> Square() >> Inc() >> Print(); |
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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } Const& operator>>(Square& square) const { square.set_value(value_); return square; } private: int value_; }; class Square { public: Square& operator>>(Inc& inc) { value_ = value_*value_; return inc; } Square& operator>>(Print& print) { value_ = value_*value_; print.set_value(value_); return print; } private: int value_ = 0; friend class Const; void set_value(int value) { value_ = value; } }; class Inc { public: Inc& operator>>(Print& print) { value_ += 1; print.set_value(value_); return print; } private: int value_ = 0; friend class Square; }; class Print { public: Print& operator>>(const Print&) { std::cout << value_ << std::endl; return *this; } private: int value_ = 0; void set_value(int value) { value_ = value; } friend class Inc; }; |
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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } Const& operator>>(Square& square) const { square.set_value(value_); return square; } private: int value_; }; class Square { public: Square& operator>>(Inc& inc) { value_ = value_*value_; return inc; } Square& operator>>(Print& print) { value_ = value_*value_; print.set_value(value_); return print; } private: int value_ = 0; friend class Const; void set_value(int value) { value_ = value; } }; class Inc { public: Inc& operator>>(Print& print) { value_ += 1; print.set_value(value_); return print; } private: int value_ = 0; friend class Square; }; class Print { public: Print& operator>>(const Print&) { std::cout << value_ << std::endl; return *this; } private: int value_ = 0; void set_value(int value) { value_ = value; } friend class Inc; }; |
main.cpp:8:21: error: 'Square' has not been declared
8 | Const& operator>>(Square& square) const {
| ^~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&) const':
main.cpp:9:12: error: request for member 'set_value' in 'square', which is of non-class type 'int'
9 | square.set_value(value_);
| ^~~~~~~~~
main.cpp:10:12: error: cannot bind non-const lvalue reference of type 'Const&' to an rvalue of type 'Const'
10 | return square;
| ^~~~~~
main.cpp:6:3: note: after user-defined conversion: 'Const::Const(int)'
6 | Const(int value) : value_(value) {}
| ^~~~~
main.cpp: At global scope:
main.cpp:18:22: error: 'Inc' has not been declared
18 | Square& operator>>(Inc& inc) {
| ^~~
main.cpp:22:22: error: 'Print' has not been declared
22 | Square& operator>>(Print& print) {
| ^~~~~
main.cpp:22:11: error: 'Square& Square::operator>>(int&)' cannot be overloaded with 'Square& Square::operator>>(int&)'
22 | Square& operator>>(Print& print) {
| ^~~~~~~~
main.cpp:18:11: note: previous declaration 'Square& Square::operator>>(int&)'
18 | Square& operator>>(Inc& inc) {
| ^~~~~~~~
main.cpp: In member function 'Square& Square::operator>>(int&)':
main.cpp:20:12: error: invalid initialization of reference of type 'Square&' from expression of type 'int'
20 | return inc;
| ^~~
main.cpp: In member function 'Square& Square::operator>>(int&)':
main.cpp:24:11: error: request for member 'set_value' in 'print', which is of non-class type 'int'
24 | print.set_value(value_);
| ^~~~~~~~~
main.cpp:25:12: error: invalid initialization of reference of type 'Square&' from expression of type 'int'
25 | return print;
| ^~~~~
main.cpp: At global scope:
main.cpp:35:19: error: 'Print' has not been declared
35 | Inc& operator>>(Print& print) {
| ^~~~~
main.cpp: In member function 'Inc& Inc::operator>>(int&)':
main.cpp:37:11: error: request for member 'set_value' in 'print', which is of non-class type 'int'
37 | print.set_value(value_);
| ^~~~~~~~~
main.cpp:38:12: error: invalid initialization of reference of type 'Inc&' from expression of type 'int'
38 | return print;
| ^~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is a possible implementation of the classes `Const`, `Square`, and `Print` in C++ that would allow you to write the expression `Cost(5) >> Square() >> Inc() >> Print()` and have it print the value 26 to the screen:
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 | class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } Const& operator>>(Square& square) const { square.set_value(value_); return square; } private: int value_; }; class Square { public: Square() {} Square& set_value(int value) { value_ = value * value; return *this; } int value() const { return value_; } Square& operator>>(Inc& inc) const { inc.set_value(value_); return inc; } private: int value_; }; class Inc { public: Inc() {} Inc& set_value(int value) { value_ = value + 1; return *this; } int value() const { return value_; } friend std::ostream& operator<<(std::ostream& out, const Inc& inc); private: int value_; }; std::ostream& operator<<(std::ostream& out, const Inc& inc) { out << inc.value(); return out; } int main() { Const(5) >> Square() >> 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 34 35 36 37 38 39 40 41 42 43 44 | class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } Const& operator>>(Square& square) const { square.set_value(value_); return square; } private: int value_; }; class Square { public: Square() {} Square& set_value(int value) { value_ = value * value; return *this; } int value() const { return value_; } Square& operator>>(Inc& inc) const { inc.set_value(value_); return inc; } private: int value_; }; class Inc { public: Inc() {} Inc& set_value(int value) { value_ = value + 1; return *this; } int value() const { return value_; } friend std::ostream& operator<<(std::ostream& out, const Inc& inc); private: int value_; }; std::ostream& operator<<(std::ostream& out, const Inc& inc) { out << inc.value(); return out; } int main() { Const(5) >> Square() >> Inc() >> Print(); 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 | class Const { public: Const(int value) : value_(value) {} int value() const { return value_; } Const& operator>>(Square& square) const { square.set_value(value_); return square; } private: int value_; }; class Square { public: Square() {} Square& set_value(int value) { value_ = value * value; return *this; } int value() const { return value_; } Square& operator>>(Inc& inc) const { inc.set_value(value_); return inc; } private: int value_; }; class Inc { public: Inc() {} Inc& set_value(int value) { value_ = value + 1; return *this; } int value() const { return value_; } friend std::ostream& operator<<(std::ostream& out, const Inc& inc); private: int value_; }; std::ostream& operator<<(std::ostream& out, const Inc& inc) { out << inc.value(); return out; } int main() { Const(5) >> Square() >> Inc() >> Print(); return 0; } |
main.cpp:6:21: error: 'Square' has not been declared
6 | Const& operator>>(Square& square) const {
| ^~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&) const':
main.cpp:7:12: error: request for member 'set_value' in 'square', which is of non-class type 'int'
7 | square.set_value(value_);
| ^~~~~~~~~
main.cpp:8:12: error: cannot bind non-const lvalue reference of type 'Const&' to an rvalue of type 'Const'
8 | return square;
| ^~~~~~
main.cpp:4:3: note: after user-defined conversion: 'Const::Const(int)'
4 | Const(int value) : value_(value) {}
| ^~~~~
main.cpp: At global scope:
main.cpp:19:22: error: 'Inc' has not been declared
19 | Square& operator>>(Inc& inc) const {
| ^~~
main.cpp: In member function 'Square& Square::operator>>(int&) const':
main.cpp:20:9: error: request for member 'set_value' in 'inc', which is of non-class type 'int'
20 | inc.set_value(value_);
| ^~~~~~~~~
main.cpp:21:12: error: invalid initialization of reference of type 'Square&' from expression of type 'int'
21 | return inc;
| ^~~
main.cpp: At global scope:
main.cpp:32:15: error: 'ostream' in namespace 'std' does not name a type
32 | friend std::ostream& operator<<(std::ostream& out, const Inc& inc);
| ^~~~~~~
main.cpp:1:1: note: 'std::ostream' is defined in header '<ostream>'; did you forget to '#include <ostream>'?
+++ |+#include <ostream>
1 |
main.cpp:37:6: error: 'ostream' in namespace 'std' does not name a type
37 | std::ostream& operator<<(std::ostream& out, const Inc& inc) {
| ^~~~~~~
main.cpp:37:1: note: 'std::ostream' is defined in header '<ostream>'; did you forget to '#include <ostream>'?
37 | std::ostream& operator<<(std::ostream& out, const Inc& inc) {
| ^~~
main.cpp: In function 'int main()':
main.cpp:43:12: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
43 | Const(5) >> Square() >> Inc() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:6:10: note: candidate: 'Const& Const::operator>>(int&) const'
6 | Const& operator>>(Square& square) const {
| ^~~~~~~~
main.cpp:6:29: note: no known conversion for argument 1 from 'Square' to 'int&'
6 | Const& operator>>(Square& square) const {
| ~~~~~~~~^~~~~~
main.cpp:43:36: error: 'Print' was not declared in this scope; did you mean 'int'?
43 | Const(5) >> Square() >> Inc() >> Print();
| ^~~~~
| int
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here's a possible solution:
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 | #include <iostream> class Cost { public: Cost(int value) : value_(value) {} Square operator()() const { return Square{value_ * value_}; } friend std::ostream& operator<<(std::ostream& os, const Cost& cost); private: int value_; }; class Square { public: Square(int value) : value_(value) {} Square& operator++() { ++value_; return *this; } friend std::ostream& operator<<(std::ostream& os, const Square& square); private: int value_; }; std::ostream& operator<<(std::ostream& os, const Cost& cost) { return os << cost.value_; } std::ostream& operator<<(std::ostream& os, const Square& square) { return os << square.value_; } Cost operator"" Cost(unsigned long long int value) { return Cost{static_cast<int>(value)}; } Square operator"" Square(unsigned long long int value) { return Square{static_cast<int>(value)}; } int main() { Cost(5) >> Square() >> ++ >> Print(); return 0; } // Define Print() as a regular function, not a member function, // because in the original question it seems to be used as a function, // not as a method of a class. void Print() { static Square square{}; std::cout << square << '\n'; } |
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 59 | #include <iostream> class Cost { public: Cost(int value) : value_(value) {} Square operator()() const { return Square{value_ * value_}; } friend std::ostream& operator<<(std::ostream& os, const Cost& cost); private: int value_; }; class Square { public: Square(int value) : value_(value) {} Square& operator++() { ++value_; return *this; } friend std::ostream& operator<<(std::ostream& os, const Square& square); private: int value_; }; std::ostream& operator<<(std::ostream& os, const Cost& cost) { return os << cost.value_; } std::ostream& operator<<(std::ostream& os, const Square& square) { return os << square.value_; } Cost operator"" Cost(unsigned long long int value) { return Cost{static_cast<int>(value)}; } Square operator"" Square(unsigned long long int value) { return Square{static_cast<int>(value)}; } int main() { Cost(5) >> Square() >> ++ >> Print(); return 0; } // Define Print() as a regular function, not a member function, // because in the original question it seems to be used as a function, // not as a method of a class. void Print() { static Square square{}; std::cout << square << '\n'; } |
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 | #include <iostream> class Cost { public: Cost(int value) : value_(value) {} Square operator()() const { return Square{value_ * value_}; } friend std::ostream& operator<<(std::ostream& os, const Cost& cost); private: int value_; }; class Square { public: Square(int value) : value_(value) {} Square& operator++() { ++value_; return *this; } friend std::ostream& operator<<(std::ostream& os, const Square& square); private: int value_; }; std::ostream& operator<<(std::ostream& os, const Cost& cost) { return os << cost.value_; } std::ostream& operator<<(std::ostream& os, const Square& square) { return os << square.value_; } Cost operator"" Cost(unsigned long long int value) { return Cost{static_cast<int>(value)}; } Square operator"" Square(unsigned long long int value) { return Square{static_cast<int>(value)}; } int main() { Cost(5) >> Square() >> ++ >> Print(); return 0; } // Define Print() as a regular function, not a member function, // because in the original question it seems to be used as a function, // not as a method of a class. void Print() { static Square square{}; std::cout << square << '\n'; } |
main.cpp:8:5: error: 'Square' does not name a type
8 | Square operator()() const {
| ^~~~~~
main.cpp:41:6: warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]
41 | Cost operator"" Cost(unsigned long long int value) {
| ^~~~~~~~
main.cpp:45:8: warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]
45 | Square operator"" Square(unsigned long long int value) {
| ^~~~~~~~
main.cpp: In function 'int main()':
main.cpp:50:23: error: no matching function for call to 'Square::Square()'
50 | Cost(5) >> Square() >> ++ >> Print();
| ^
main.cpp:20:5: note: candidate: 'Square::Square(int)'
20 | Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:20:5: note: candidate expects 1 argument, 0 provided
main.cpp:18:7: note: candidate: 'constexpr Square::Square(const Square&)'
18 | class Square {
| ^~~~~~
main.cpp:18:7: note: candidate expects 1 argument, 0 provided
main.cpp:18:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:18:7: note: candidate expects 1 argument, 0 provided
main.cpp:50:31: error: expected primary-expression before '>>' token
50 | Cost(5) >> Square() >> ++ >> Print();
| ^~
main.cpp:50:34: error: 'Print' was not declared in this scope; did you mean 'printf'?
50 | Cost(5) >> Square() >> ++ >> Print();
| ^~~~~
| printf
main.cpp: In function 'void Print()':
main.cpp:58:26: error: no matching function for call to 'Square::Square(<brace-enclosed initializer list>)'
58 | static Square square{};
| ^
main.cpp:20:5: note: candidate: 'Square::Square(int)'
20 | Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:20:5: note: candidate expects 1 argument, 0 provided
main.cpp:18:7: note: candidate: 'constexpr Square::Square(const Square&)'
18 | class Square {
| ^~~~~~
main.cpp:18:7: note: candidate expects 1 argument, 0 provided
main.cpp:18:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:18:7: note: candidate expects 1 argument, 0 provided
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False