使用函数指针类成员。 [英] using function pointer class members.

查看:62
本文介绍了使用函数指针类成员。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个CPP文件定义了一个类:


class xyz {

public:

int(* funcptr)(void);

}


函数foo在C文件中定义< br $>

并且在CPP文件中,我需要进行作业

funcptr = foo;


所以我宣布foo as externC在CPP文件中:

externC int foo(void);


但我在作业中收到错误,因为一方的类型为

" extern C"。这是什么解决方案?


请帮忙。


TIA。

-

jag。

Quaerendo invenietis

解决方案

< ja ** *******@gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com


我有一个CPP文件,它定义了一个类:

class xyz {
public:
int(* funcptr)(void);
在CPP文件中,我需要进行分配
funcptr = foo ;


funcptr是一个成员,因此只作为对象的一部分存在。因此你需要

需要类似


xyz obj;

obj.funcptr = foo;


也许这就是你所做的,但是如果你不发布真实的代码,那么我们就不能知道
,因此无法诊断问题。

所以我宣称foo为externC在CPP文件中:
externC int foo(void);

但是我在赋值中遇到错误,因为一方的类型是
extern C。解决方法是什么?




除了上面描述的问题,它应该有效。如果上面没有解决问题,那么发布展示

问题的最小完整代码(即复制并粘贴未能编译的实际代码) )。


-

John Carson



main.CPP文件

=======

externC int function(void);


class xyz {

public:

int(* funcptr)(void);

}对象;


int main(无效){

int temp = 0;

对象。 funcptr = function; //这一行给了我概率。

temp = object.funcptr();

返回0;

}


second.C文件

=========


int function(void){

返回1;

}


***************

赋值funcptr = function给了我错误。实际上我是在TI的Code Composer Studio上工作的b $ b。所以我不确定错误

是否限制在那个环境中。


代码只是翻拍,只是为了呈现核心理念。


-

jag

Quaerendo invenietis


< BLOCKQUOTE>< JA ********* @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com



main.CPP文件
=======
externC int function(void);

class xyz {
public:
int(* funcptr)(void);
} object;

int main(void){
int temp = 0;
object.funcptr = function; //这行给了我概率。
temp = object.funcptr();
返回0;
}

second.C文件
= ========

int function(void){
返回1;
}

******** *******
分配funcptr = function给了我错误。实际上我正在TI的Code Composer Studio上工作。所以我不确定错误是否会限制在那个环境中。

代码只是翻拍,只是为了呈现核心理念。

-
jag
Quaerendo inventietis



您的代码在VC ++(7.1和8.0)上编译并运行没有问题---

它应该。如果它不能在TI的Code Composer Studio上运行,那么问题就在于

。错误信息实际上是什么意思?

-

John Carson


Hi,
I am having a CPP file which defines a class:

class xyz{
public:
int (*funcptr)(void);
}

A function foo is defined in a "C file"

and in the CPP file, I need to make an assignment
funcptr = foo;

So I declared foo as extern "C" in the CPP file:
extern "C" int foo(void);

But I am getting an error in the assignment, since one side is of type
"extern C". What is the solution for this?

Please do help.

TIA.
--
jag.
"Quaerendo invenietis"

解决方案

<ja*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com

Hi,
I am having a CPP file which defines a class:

class xyz{
public:
int (*funcptr)(void);
}

A function foo is defined in a "C file"

and in the CPP file, I need to make an assignment
funcptr = foo;
funcptr is a member and hence only exists as part of an object. Thus you
need something like

xyz obj;
obj.funcptr = foo;

Perhaps this is what you do, but if you don''t post real code then we can''t
know and hence can''t diagnose the problem.
So I declared foo as extern "C" in the CPP file:
extern "C" int foo(void);

But I am getting an error in the assignment, since one side is of type
"extern C". What is the solution for this?



Aside from the problem described above, it should work. If the above doesn''t
solve the problem, then post the smallest complete code that exhibits the
problem (i.e., copy and paste actual code that has failed to compile).

--
John Carson


Hi,
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I am
working on TI''s Code Composer Studio. So I am not sure if the error
confines to that environment.

The code is just a remake, just to present the core idea.

--
jag
"Quaerendo invenietis"


<ja*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com

Hi,
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I am
working on TI''s Code Composer Studio. So I am not sure if the error
confines to that environment.

The code is just a remake, just to present the core idea.

--
jag
"Quaerendo invenietis"


Your code compiles and runs without a problem on VC++ (7.1 and 8.0) --- as
it should. If it won''t run on TI''s Code Composer Studio, then it looks like
that is where the problem lies. What does the error message actually say?
--
John Carson


这篇关于使用函数指针类成员。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆