我试图在VC++中创建一个函数指针,但我不断收到语法错误.
我的头文件中的声明如下所示:
void ApplyFuncToCellsInSelection(void(*func)(CPoint, *CSpreadWnd));
Run Code Online (Sandbox Code Playgroud)
这是定义:
void CSpreadWnd::ApplyFuncToCellsInSelection(void(*func)(CPoint, *CSpreadWnd)) { ... }
Run Code Online (Sandbox Code Playgroud)
以下是我收到的错误消息:
c:\...\spreadwnd.h(274) : error C2059: syntax error : 'function-style cast'
c:\...\spreadwnd.h(274) : error C2059: syntax error : ')'
c:\...\spreadwnd.h(274) : error C2143: syntax error : missing ')' before ';'
Run Code Online (Sandbox Code Playgroud)
我知道这可能是一件非常简单的事情,但我似乎无法解决这个问题.
简单的问题; 现在我有这样的事情:
typedef void(*MyFunctionPointer)(int);
typedef std::vector < MyFunctionPointer > MyFunctionPointerContainer;
Run Code Online (Sandbox Code Playgroud)
但是,我想在一行中输入这个容器,跳过第一个typedef,我该怎么做?
以下代码(函数原型):
void parse_ini(FSFILE *fp, void(*secFunc)(char*), void(*varFunc)(char*, char*));
Run Code Online (Sandbox Code Playgroud)
编译时出现错误:
util\setup.c:38: error: syntax error before '*' token
util\setup.c:38: error: 'parse_ini' declared as function returning a function
util\setup.c:38: error: syntax error before 'void'
util\setup.c:50: error: syntax error before '*' token
Run Code Online (Sandbox Code Playgroud)
是什么造成的?使用MPLAB C30,这是用于PIC24F/dsPIC 16位微控制器的GCC v3.23版本.
(C++)我有很多Entry类,并且得到了BaseProcessor接口,它包含了Entry处理逻辑.(见下面的代码)
Entry不提供运算符<().BaseProcessor提供指向较少(Entry,Entry)函数的指针,该函数特定于特定的BaseProcessor实现.
我可以使用函数指针来比较程序中的Entry实例.但是我需要为Entry类创建std :: set(或std :: map,或其他使用less()的东西).我试图使用std :: binary_function派生类将它传递给std :: set,但看起来我无法将函数指针值传递给模板.
我怎样才能做到这一点?用C++ 03可以吗?
谢谢.
struct Entry
{
// ...
private:
bool operator< (const Entry &) const; // Should be defined by BaseProcessor.
};
typedef bool (*LessFunc)(const Entry &, const Entry &);
class BaseProcessor
{
public:
// ...
virtual LessFunc getLessFunc () const = 0;
};
// ...
BaseProcessor *processor = getProcessor();
LessFunc lessfunc = processor->getLessFunc();
Entry e1;
Entry e2;
bool isLess = lessfunc(e1, e2); // OK
typedef std::set<Entry, ???> EntrySetImpl; …Run Code Online (Sandbox Code Playgroud) 我试着让myFunction给我一个数组中的值的总和,但我知道我不能使用返回值,当我用代码运行我的程序时,所有我得到的是打印出来的值并没有总结为什么?
void myFunction (int i) {
int total = 0;
total += i;
cout << total;
}
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for_each( array, array+10, myFunction);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要传递类函数作为glutIdleFunc的参数.这种结构不起作用:
void (*func)(void) = this->step();
glutIdleFunc(func);
Run Code Online (Sandbox Code Playgroud)
这也是:
void (*func)(void) = Core::step();
glutIdleFunc(func);
Run Code Online (Sandbox Code Playgroud)
我该怎么做?抱歉我的愚蠢...... :)
我有一系列结构.该struct有两个函数指针.数组的每个元素都需要函数指针指向不同的函数,以便可以在不知道特定函数名的情况下调用对应于特定元素的函数.作为函数指针的新手,在我看来,我正在做的事情不会起作用,但我不确定如何正确地做到这一点.还将理解如何调用所指向的函数之一的示例.
以下是我试图引用的函数的原型:
int edit_translate_concise(struct ged *gedp, const union edit_cmd * const cmd);
int edit_translate_add_arg(union edit_cmd * const cmd, struct edit_arg * const arg);
Run Code Online (Sandbox Code Playgroud)
结构和该结构的数组如下:
struct edit_cmd_tab {
char *name;
char *opt_global;
char *usage;
char *help;
int (*exec_concise)(struct ged *gedp, const union edit_cmd *const cmd);
int (*add_arg)(union edit_cmd *const cmd, struct edit_arg *const arg);
};
static const struct edit_cmd_tab edit_cmds[] = {
...
{"translate", (char *)NULL,
"[FROM] TO OBJECT ...",
"[[-n] -k {FROM_OBJECT | FROM_POS}]\n"
"[-n] [-a | -r] {TO_OBJECT | …Run Code Online (Sandbox Code Playgroud) C一直是个谜!
我正在实现一个工作组线程执行模型,我试图使用alloca作为更快的内存分配选项.尝试通过使用alloca存储在堆栈中的函数指针执行代码时,我有一个奇怪的分段错误.
这是一个牙齿挑选代码,导致类似的分段错误:
#include <stdlib.h>
#include <stdio.h>
typedef void* (*foo)(void*);
typedef struct task
{
foo f;
} task;
void *blah(void* v)
{
printf("addr:%p\n", &v);
return v;
}
int main()
{
void *queue[10];
task *t = (task*) alloca (sizeof(task));
// No null check, excuse me!
t->f = blah;
queue[0] = (void*)t;
char string[10] = "Bingo!";
char *c = &string[0];
task *tnew = (task*)&queue[0];
tnew->f((void*)c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我执行上面的代码时,我在tnew-> f()行得到了一个分段错误.GDB回溯对我没什么帮助.
请解释上面代码中的错误..我是第一次使用alloca.
非常感谢你!
为什么我的代码只适用于object2?
function object1() {
this.URL = "yahoo.com";
this.getURL = function() { return this.URL; };
}
var object2 = new Object();
object2.URL = "gmail.com";
object2.getURL = function() { return this.URL; };
var sources = new Array();
sources[0] = object1;
sources[1] = object2;
i=0;
var source1 = sources[0];
console.log(source1.getURL());
var source2 = sources[1];
console.log(source2.getURL());
Run Code Online (Sandbox Code Playgroud)
如果我尝试从object1中获取URL,我会收到此错误:TypeError:source1.getURL不是函数
我正在尝试处理多个HTTP连接,它使用不同的URL,每个都返回一个我必须解析的不同的XML.我想通过创建一个对象数组并为每个对象执行一个操作,获取URL并连接到它.
对于std::vector<double*> v;我可以double通过调用删除s
std::for_each(v.begin(), v.end(), (void(*)(void* a))operator delete);
Run Code Online (Sandbox Code Playgroud)
现在,如果我有std::map<int, double*> m;,我可以在不使用显式循环,增强或非标准STL扩展的情况下执行类似的操作吗?即什么是???在
std::for_each(m.begin(), m.end(), ???);
Run Code Online (Sandbox Code Playgroud) c++ ×6
c ×3
abstraction ×1
alloca ×1
arrays ×1
class ×1
foreach ×1
gcc ×1
javascript ×1
map ×1
stl ×1
struct ×1
templates ×1
typedef ×1
visual-c++ ×1