小编use*_*047的帖子

哪个转换在C++中是正确的?

我想将一个void*转换为char*reinterpret_cast和static_cast,哪一个适合?static_cast<char*> or reinterpret_cast<char*>

c++ type-conversion

6
推荐指数
1
解决办法
216
查看次数

点运算符可以将表达式作为右操作数吗?为什么?

以下陈述在任何情况下都是合法的吗?

s.*p = 5;
Run Code Online (Sandbox Code Playgroud)

那么,
点运算符可以将表达式作为右操作数吗?为什么?

c++

2
推荐指数
1
解决办法
142
查看次数

为什么我不能使用函数适配器compose2?

我在linux上使用GCC 4.1.2,STL必须是SGI STL.我写完之后:

#include <functional>
 std::find_if(PirateFlush, PirateFlush + size,
                compose2(std::logical_and<bool>(), bind2nd(std::greater<UInt32>(), time),
                    bind2nd(std::less<UInt32>(), now)));
Run Code Online (Sandbox Code Playgroud)

编译说:

错误:'compose2'未在此范围内声明

怎么了?

c++ stl sgi

2
推荐指数
1
解决办法
418
查看次数

如何在类中使用成员函数专业化?

#include <iostream>
#include <string>

using namespace std;

struct Uid {
  typedef int type;  
};

struct Name {
  typedef string type;
};


struct Age {
  typedef int type;
};


template <class T1, class T2, class T3>
class People {
private:
  typename T1::type val1;
  typename T2::type val2;
  typename T3::type val3;
  //add a get function here
  }
};


int main() {
  People<Uid, Name, Age> people;
  people.get<Uid>(); //make this validate
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我想在类中添加一个get函数,使函数调用get in main validate.我尝试在类中添加一个tempalte get及其特化版本,但它是一个无效的方法,编译器说:非命名空间范围'class People'中的显式特化.有人说这种方法适用于vs,但它打破了标准.

c++ templates

2
推荐指数
1
解决办法
73
查看次数

如何用C语言覆盖函数(系统调用)?

在许多脚本语言中,我们有一个这样的编程方法:

首先,有一个名为func的函数:

void func()
{
}
Run Code Online (Sandbox Code Playgroud)

其次,我想在客户端调用此函数时记录一些信息,但我不想修改函数,所以我可以这样做:

void (*pfunc)(void) = func;
void func()
{
    log("Someone call fund");
    pfunc();
}
Run Code Online (Sandbox Code Playgroud)

之后,任何拨打电话的人都会调用我的"覆盖"功能.这在许多脚本语言中都可以.我可以用C语言做同样的事情吗?以及如何编码?

我想用这个方法在一些3party库中做一些工作,所以我必须做一些影响链接过程的事情,而不仅仅是编译过程.

c

1
推荐指数
1
解决办法
2078
查看次数

如何修改程序使其编译?

#include <iostream>
#include <vector>
#include <algorithm> 

using namespace std;

void push_back(int v, vector<int>& coll) 
{
    coll.push_back(v); 
}

int main() 
{
    int a[] = {1, 2, 3, 4, 5};
    std::vector<int> b;
    for_each(a, a + 5, bind2nd(ptr_fun(push_back), b)); 
}
Run Code Online (Sandbox Code Playgroud)

编译说:

/usr/include/c++/4.1.2/bits/stl_function.h: In instantiation of ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >’:
tt5.cpp:15:   instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:435: error: forming reference to reference type ‘std::vector<int, std::allocator<int> >&’
/usr/include/c++/4.1.2/bits/stl_function.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>, _Tp = …
Run Code Online (Sandbox Code Playgroud)

c++ stl compiler-errors

0
推荐指数
1
解决办法
229
查看次数

标签 统计

c++ ×5

stl ×2

c ×1

compiler-errors ×1

sgi ×1

templates ×1

type-conversion ×1