+ 3

Error token

What will be the output of this code? #include <iostream> using namespace std; class Calc{ public: int a(int x, int y){return x+y;} int b(int x, int y){return x-y;} int c(int x, int y){return x*y;} int d(int x, int y){return x/y;} }; int main() { cout<<Calc.c(2,2); return 0; }

17th Jan 2019, 8:03 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
3 ответов
+ 3
The output is an instantiation error. Your code should be something like the following: #include <iostream> class Calc { public: int a(int x, int y){return x+y;} int b(int x, int y){return x-y;} int c(int x, int y){return x*y;} int d(int x, int y){return x/y;} }; int main() { Calc calculate; std::cout << calculate.c(2,2); return 0; } i.e., you must actually instantiate a Calc object (being calculate).
17th Jan 2019, 8:42 PM
Dread
Dread - avatar
+ 2
Edin Hajdarevic you don't get that you can't just say Calc.c() without instantiation of a Calc object? C++ expects a primary-expression before the '.' token, hence the need for an object.
17th Jan 2019, 10:27 PM
Dread
Dread - avatar
+ 1
I don't see why it's an error? I guess the output would be just 4, since 2*2 is 4. And I didn't get what are you talking about, Default
17th Jan 2019, 9:50 PM
Edin Hajdarevic
Edin Hajdarevic - avatar