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; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

23rd Dec 2016, 11:10 AM
Cody Arthur
Cody Arthur - avatar
3 Answers
+ 3
Just add "()" at the end of the functioncall of your getcarspeed method.
23rd Dec 2016, 11:30 AM
Simon
Simon - avatar
+ 3
thanks Simon. I made the change and it worked!.
23rd Dec 2016, 12:57 PM
Cody Arthur
Cody Arthur - avatar
+ 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?
23rd Dec 2016, 11:19 AM
visph
visph - avatar