+ 2
can someone please explain why this code keeps telling me complication error instead of outputting the value of speed. #include <iostream> using namespace std; class myClass { public: void setcarspeed (int a){ speed=a; } int getcarspeed(){ return speed; } private: int speed; }; int main() { myClass obj; obj.setcarspeed (40); cout<<obj.getcarspeed ; return 0; }
3 Respuestas
+ 3
Just add "()" at the end of the functioncall of your getcarspeed method.
+ 3
thanks Simon. I made the change and it worked!.
+ 2
I think ( not tested ) you must access your class var ( speed ) with "this" keyword:
this.speed=a;
and/or maybe move the speed declaration on top of the class, before functions declarations?