Jul 13, 2013 at 8:11pm UTC
How do you call a member function from another member function? My first thought would be
this ->member_func()
, but htat gave an error, then i tried
Basic.print()
, but htat also gives an error. How do you replicate python's self?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream>
#include <string>
class Basic{
public :
void print(int );
void print(double );
void print(std::string);
void test_prints();
};
void Basic::print(int num){
std::cout << num << std::endl;
}
void Basic::print(double num){
std::cout << num << std::endl;
}
void Basic::print(std::string s){
std::cout << s << std::endl;
}
void test_prints(){
Basic.print(1);
this ->print(1.1);
this ->print("test" );
this ->print(1 + 1);
this ->print(1.1 + 1);
}
int main(){
Basic basic;
basic.test_prints();
}
Last edited on Jul 13, 2013 at 8:12pm UTC
Jul 13, 2013 at 8:50pm UTC
Line 21: test_prints is not a member of your class. It should be:
void Basic::test_prints()
Line 22 becomes simply:
Last edited on Jul 13, 2013 at 8:50pm UTC
Jul 13, 2013 at 8:55pm UTC
this ->print()
would've worked had you declared the method properly with Basic::
as mentioned above.