invalid use of non-static member function ‘void Date::init(int, int, int)’

#include<iostream>
using namespace std;

class Date
{
 public:
 int day,month,year;
 void init(int,int,int);
 void print_ymd();
};

void Date::init(int yy, int mm, int dd)
{
 year = yy;
 month = mm;
 day = dd;
}

void Date::print_ymd()
{
 std::cout << year << "-" << month << "-" << day << std::endl;
}
int main()
{
 Date date1;
 Date *p1 = &date1;            //指向对象的指针
 p1->init(2006, 3, 28);
 p1->print_ymd();
 int *p2;
 p2 = &date1.year;             //指向对象数据成员的指针
 std::cout << *p2 << std::endl;
 void (Date:: * p3)(int,int,int);   //指向对象成员函数的指针
 void (Date:: * p4)();              //指向对象成员函数的指针
 p3 = Date::init;
 p4 = Date::print_ymd;
 (date1.*p3)(2006, 4, 8);
 (date1.*p4)();
 return 0;
}

编译的时候会报错:

35:13: error: invalid use of non-static member function ‘void Date::init(int, int, int)’

p3 = Date::init;

36:13: error: invalid use of non-static member function ‘void Date::print_ymd()’

p4 = Date::print_ymd;

将p3= Date::init;改为:

p3 = &Date::init;

将p4 = Date::init;改为:

p4 = &Date::print_ymd;

C++的非静态成员函数在未实例化之前地址是不确定的,所以要将Date的两个成员函数

的地址指明之后才能进行赋值操作。

时间: 2024-10-11 03:38:49

invalid use of non-static member function ‘void Date::init(int, int, int)’的相关文章

static 和 no static Member function学习

以下是做实验的一段代码: #include <iostream> using namespace std; typedef void (*p)(); class Object { public: static void s_fun_1() { cout << "static function 1\n"; } void fun_1() {cout << "no static function 1\n";} }; typedef vo

Virtual Member Functions &amp; Static Member Function

如果一个 normalize() 是一个 virtual member function, 那么以下的调用: ptr->normalize(); 将会被内部转化为: (*ptr->vptr[1])(ptr); 其中:vptr 表示由编译器生成的指针, 指向 virtual table, 它被安插在每一个声明有(或继承自) virtual functinos 的 class object 中. 事实上其名称也会被 mangled, 因为在一个复杂的 class 派生体系中, 可能存在多个 vpt

C++ static member functions

今天复习老师昨天讲的static member functions.总觉得这玩意儿存在真是莫名其妙,度娘之,发现网上讲的都十分不清楚.还是用Google从米国网站找到了答案. class Something { private: static int s_nValue; }; int Something::s_nValue = 1; // initializer int main() { // how do we access Something::s_nValue? } In this cas

C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)

4.4 指向Member Function的指针 (Pointer-to-Member Functions) 取一个nonstatic data member的地址,得到的结果是该member在 class 布局中的byte位置(再加1),它是一个不完整的值,须要被绑定于某个 class object的地址上,才可以被存取. 取一个nonstatic member function的地址,假设该函数是nonvirtual,则得到的结果是它在内存中真正的地址.然而这个值也是不全然的,它也须要被绑定

Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)

Misunderstanding of static member class : For most programmers, comparing to SMC ,we may be more familiar with static field . so we may analogously guess how SMC works according to static field's feature. That leads to a misunderstanding.--SMC may on

转:C语言中的static变量和C++静态数据成员(static member)

转自:C语言中的static变量和C++静态数据成员(static member) C语言中static的变量:1).static局部变量        a.静态局部变量在函数内定义,生存期为整个程序运行期间,但作用域与自动变量相同,只能在定义该变量的函数内使用.退出该函数后, 尽管该变量还继续存在,但不能使用它.        b.对基本类型的静态局部变量若在说明时未赋以初值,则系统自动赋予0值.而对自动变量不赋初值,则其值是不定的.2).static全局变量        全局变量本身就是静

&#39;init&#39;:member function definition looks like a ctor(构造函数的缩写)

具体编译的错误提示如下:warning C4183: 'init': member function definition looks like a ctor, but name does not match enclosing classwarning C4183: 'method': member function definition looks like a ctor, but name does not match enclosing classLinking...main.exe -

Timer.4 - Using a member function as a handler

In this tutorial we will see how to use a class member function as a callback handler. The program should execute identically to the tutorial program from tutorial Timer.3. #include <iostream> #include <boost/asio.hpp> #include <boost/bind.

C++ 之 const member function

一个常量成员函数(const member function), 可以读取类的数据成员,但不能修改类的数据成员. 1  声明 在成员函数声明的参数列表后,加上 const 关键字,将其声明为常量成员函数(const member function),表明其不被允许修改类的数据成员 下面定义了一个 Date 类,分别以年.月.日的形式来表示日期 class Date { public: int day() const { return d; } int month() const { return