我们有一个接受C函数指针的结构:
int one(int x)
{
}
int two(int x)
{
}
struct Cstruct
{
int (*fn1)(int);
int (*fn2)(int);
};
现在我有了一个C++类,它的方法如下:
class A
{
public:
int one(int x)
{
}
int two(int x)
{
}
int three(int x)
{
struct Cstruct cstr = {&this->one, &a
当我试图使用声明为类变量的map 'freq‘的内容时,在比较器函数中对向量进行排序(使用map按频率排序)时,我遇到了这个错误。有谁能指出我哪里出了问题,我该怎么做?下面是代码:
class Solution {
map<int, int> freq;
public:
static bool comp(int a, int b){
if(a!=b){
int c1 = freq[a];
int c2 = freq[b];
if(c1!=c2)
我正在用c++写我自己的计时器。我想知道是否有可能将一个函数传递给计时器构造函数,然后调用这个函数。
我正在考虑为此使用函数指针,但是我无法找到在类本身内传递非静态函数的解决方案。
G++给出了这个错误:
Server.cpp:61:54:错误:无效使用非静态成员函数serverTimer =新计时器::Timer(onTimerTick,3000);
我的类Server.cpp如下所示:
private:
void onTimerTick(){
//do something with class variables, so can't us
很抱歉问这样一个问题,因为我相信以前已经回答过了,但我正在努力寻找答案,这不是为了看起来不够……不管怎样..。
class foo
{
void read(void (*func)(obj&))
{
// many things happen to obj...
(*func)(obj); // Calls the function pointer to the handler.
}
};
class bar : public foo
{
void handler(obj&)
{
/
我在一个c++类中使用了一个c++。其中一个成员函数应调用以下函数,该函数生成中断并调用服务例程:
signed short __stdcall SetIrqConditions
(
signed short DevNum,
unsigned short bEnable,
unsigned int dwIrqMask,
void (__stdcall * funcExternalIsr)
(
signed short DevNum,
unsigned int dwIrqStatus
)
);
我正在尝试调用与此函数
我在C++中有以下结构:
class A
{
protected:
int a;
};
class B : public A
{
public:
static void initThreads(); //Initialize threads (pthreads)
/* Threads */
static void* send(void*);
static void* receive(void*);
}
这些函数“发送”和“接收”操作在"a“上。但不能在静态函数中编辑非静态成员。由于发送和接收都是线程,因此需要将它们定义为静态。我将只有一个类B的对象,尽管有多个类
我有以下问题:
void MyClass::LoopFunction(vector<Item>& items,void (MyClass::*funcy)(vector<Item>&,int))
{
for(SInt32 i = 0; i < 50; i++)
{
funcy(items,i);
}
}
上面写着:
Called object type 'void(MyClass::*)(vector<Item>&,int)' is not a function or f
我指的是std::is_member_function_pointer 的可能实现。
template< class T >
struct is_member_function_pointer_helper : std::false_type {};
template< class T, class U>
struct is_member_function_pointer_helper<T U::*> : std::is_function<T> {};
template< class T >
struct is_member_fu
所以,我使用的是C++11,我做了一个类
Class C
{
private:
queue<std::int> s;
pthread_t x;
public:
C() {phthread_create(&x, NULL, d_q, NULL);
void log(int p); // pushes into q.
void* d_q(void* q); // this is a function which will pop s. assume s is thread safe.
}
问题是行pthread_create(&a
有这样的代码:
void foo(void (*fun_ptr)()){
}
class B{
public:
B(){
foo(some_fun);
}
void some_fun(){}
};
编译错误:
error: argument of type ‘void (B::)()’ does not match ‘void (*)()’
在这种情况下如何指向成员函数?我不能更改函数foo的声明,只能更改类B!
我有一个树类,如下所示:
class avl
{
node *root;
public:
avl(int data){
root->data = data;
}
int get_height(node *head = root){ //error here
if (head->right == head->left) return 0;
int l = get_height(head->left);
int r = get_height(head->right);
我在一个类中创建四个线程时遇到了困难,每个线程使用另一个成员函数打印出每个向量的内容。然而,当我创建线程时,我在这4行上得到了一个错误no instance of constructor "std::thread::thread" matches the argument list。如果我试图为线程使用另一个成员函数,我不知道为什么它不能工作。难道是因为他们在一个类里吗?我该如何纠正这4个错误?
class PrintfourVectors
{
private:
vector<string> one;
vector<string> two
我有一个类A,其中有一个静态成员函数passName
int A::passName()
{
.... // skip some code
std::string name = ...; // result from codes above
assign(); // this is a static member function in class A
pointerA->passMethodName(name); // pointerA is a class-A static member variable, but of type
我拥有的:
所以我有一个带有私有成员的类,还有一个静态函数。函数必须是静态的,我不能改变它。
我想要的:
我需要从静态函数访问私有成员。有什么想法吗?)
请检查下面的代码:
class Base
{
private:
int m_member;
public:
Base() : m_member(0) {};
~Base() {};
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); /* This must really be stati
在C++中,我有很多函数(一、二、三……)它看起来像这样:
int MyClass::one(Mynamespace::Data* data)
{
//do something
return 0;
}
我还有一个动作函数,我想用它来调用一,二,三…具有函数指针的函数。
int MyClass::action(Mynamespace::Data* data)
{
int (*actionFunction)(Mynamespace::Data*);
actionFunction = data->name; // the name is a string wit
下面有一个代码,我想在静态函数中修改类的变量,但是有一些错误。我如何用“这个”指针来修正它?
类中的静态成员没有访问"this“指针的权限,另一方面,我试图访问静态成员函数中的类变量,因此我正在寻找一种方法来使用类"me”的指针。
class me {
public:
void X() { x = 1;}
void Y() { y = 2;}
static void Z() {
x = 5 ; y = 10;
}
public:
int x, y;
};
int main() {
me M;
M.X();
M.Y();
M
我是C++中的工厂模式的新手,当我尝试实现一个方法时,我得到了标题,我得到了以下错误:
在静态成员函数中对成员'creationFunctions‘的使用无效
typedef Shape (*createShapeFunction)(void);
/* thrown when a shape cannot be read from a stream */
class WrongFormatException { };
class ShapeFactory {
public:
static void registerFunction(const std::string &a