+ 1
Passing by value from main to class
The below code gives an error. Why can't zzz.c(2.2) in main() pass values to its corresponding function in class zzz?? #include <iostream> using namespace std; class zzz { public: int c(int x, int y) {return x*y;} }; int main() { cout << zzz.c(2,2); return 0; }
4 Réponses
+ 4
#include <iostream>
using namespace std;
class zzz {
public:
int c(int x, int y) {return x*y;}
};
int main() {
zzz z;
cout << z.c(2,2);
return 0;
}
// you forget to create an object of zzz
+ 4
because you didn't decalre obj of that class.
you have to decare first then you can use the function.
int main(){
zzz obj;
cout<<obj.c(2,2);
}
+ 3
First thing we don't call methods of a class with dot operator attached to classname
In order to call the c(int,int) method you got two options either you make this method as static and then call it with the classname and scope resolution operator, ex:
static int c(int x, int y) { return x*y) }
cout << zzz::c(2,2);
OR
The second option is you create object of the zzz class and then call the method c(int, int) with that object, Ex:
int main() {
zzz obj;
cout << obj.c(2,2);
}
+ 1
please attach your code to your description.