相关疑难解决方法(0)

将bind1st用于通过引用获取参数的方法

我有这样的结构:

struct A {
    void i(int i) {}
    void s(string const &s) {}
};
Run Code Online (Sandbox Code Playgroud)

现在当我尝试这个:

bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");
Run Code Online (Sandbox Code Playgroud)

第一行编译好,但第二行生成错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>,
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
          with
          [
              _Fn2=std::mem_fun1_t<void,A,const std::string &>
          ]
          c:\work\sources\exception\test\exception\main.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
          with …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++

15
推荐指数
2
解决办法
2811
查看次数

C++定义跨文件常量的最佳方法

我正在开发一款游戏并且有一个有趣的问题.我有一些游戏范围的常量值,我想在一个文件中实现.现在我有这样的事情:

constants.cpp

extern const int BEGINNING_HEALTH = 10;
extern const int BEGINNING_MANA = 5;
Run Code Online (Sandbox Code Playgroud)

constants.hpp

extern const int BEGINNING_HEALTH;
extern const int BEGINNING_MANA;
Run Code Online (Sandbox Code Playgroud)

然后文件只是#include"constants.hpp"这很好用,直到我需要使用其中一个常量作为模板参数,因为外部链接的常量不是有效的模板参数.所以我的问题是,实现这些常量的最佳方法是什么?我担心简单地将常量放在头文件中会导致在每个翻译单元中定义它们.而且我不想使用宏.

谢谢

c++ templates const constants extern

10
推荐指数
3
解决办法
1万
查看次数

使用extern const将结构传递给模板.什么是外部人员?

我问自己为什么以下代码有效以及说明符extern在实例化时的作用baz_instance:

struct baz {
    int value;
};

extern const baz baz_instance = {3};

template<baz const& b>
int foo(){
    return b.value;
}

int main(){
    foo<baz_instance>();
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

为什么上面的代码首先编译,如果extern省略说明符,为什么不编译?什么是extern符在这个例子吗?

c++ templates struct const extern

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

标签 统计

c++ ×3

const ×2

extern ×2

templates ×2

constants ×1

struct ×1

visual-c++ ×1