scope - C++.Passing to functions.Syntax issue -


i pursuing interest in c++ programming way of self instruction. working on basic stuff , having issue getting classes talking/instantiated?.

i trying main cpp file compile alongside header , call class functions through main using more efficient command method.

i stuck , appreciate help. include both files. trying return value header calling function.

error: main.cpp:6.21 error: cannot call member function 'void myclass::setnumber(int) without object

the code works when compiled main, 'scope resolution operator' think. first main.cpp

#include <iostream>  #include "myclass.h" using namespace std; int main(){ myclass::setnumber(6);  {  return number; } } 

then header file myclass.h

// myclass.h #ifndef myclass_h #define myclass_h   class myclass {       private:          int number;//declares int 'number'         float numberfloat;//declares float 'numberfloat       public:          void setnumber(int x) {             number = x;//wraps argument "x"  "number"         }         void setnumberfloat(float x) {             numberfloat = x;         }          int getnumber() {//defines function within class.           number += 500;             return number;         }         float getnumberfloat() {//defines function         numberfloat *= 1.07;             return numberfloat;     }  };  #endif 

any help?

the error message says everything:

cannot call member function 'void myclass::setnumber(int)' without object 

you need create object first:

myclass obj; 

then call class method on object:

obj.setnumber(6); 

the value 6 assigned number field of obj variable.


Comments

  1. Wow, yeah it's a very good Blogs for you Query.Other wise If If Any one who want learn C++ & Advance contact us on 9311002620 or visit :- https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete

Post a Comment

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -