C++使用变换(STL)时出错

发布于 2024-12-28 03:09:23 字数 935 浏览 5 评论 0原文

我在使用转换时遇到编译错误:

它与我之前的问题有关: C++:如何将字符串对象复制到 int 数组?

enter code here

 class BinaryCode {

    public:
            int get_digit(char c)
            {
                    return c-'0';
            }
            void decode(string decd)
            {
                    int i;

                    std::vector<int>decoded(decd.size());
                    std::transform(decd.begin(), decd.end(), decoded.begin(), get_digit);

                    int length=decoded.length();

错误是:

enter code here

[root@localhost topcoder]# g++ prog1.c
 prog1.c: In member function `void BinaryCode::decode(std::string)':
 prog1.c:20: error: argument of type `int (BinaryCode::)(char)' does not match `int (BinaryCode::*)(char)'

有人可以帮助我吗?我正在使用 gcc (g++) 编译器。

I am facing a compilation error on using transform:

It is related to my previous question: C++: How to copy a string object to an int array?)

enter code here

 class BinaryCode {

    public:
            int get_digit(char c)
            {
                    return c-'0';
            }
            void decode(string decd)
            {
                    int i;

                    std::vector<int>decoded(decd.size());
                    std::transform(decd.begin(), decd.end(), decoded.begin(), get_digit);

                    int length=decoded.length();

The error is:

enter code here

[root@localhost topcoder]# g++ prog1.c
 prog1.c: In member function `void BinaryCode::decode(std::string)':
 prog1.c:20: error: argument of type `int (BinaryCode::)(char)' does not match `int (BinaryCode::*)(char)'

Can anyone please help me? I am using a gcc (g++) compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

君勿笑 2025-01-04 03:09:23

恕我直言,最好的方法是将其定义更改为

        int get_digit(char c)

        static int get_digit(char c)

应该与此(静态函数)一起使用。可以使用成员函数进行转换,但稍微复杂一些。而且,你不需要它。

The best IMHO would be to change the definition of

        int get_digit(char c)

to

        static int get_digit(char c)

It should work with this (static function). It is possible to transform using member functions, but it's slightly more complicated. Moreover, you don't need it.

故笙诉离歌 2025-01-04 03:09:23

您需要传递函数或函子作为最后一个参数,而不是成员函数。如果您启用了 c++11,则可以使用 lambda :

std::transform(decd.begin(), decd.end(), decoded.begin(),[this](const char it){ this->get_digit(it) } );

由于您没有 c++11 功能,因此您可以将 get_digit, 转换为函数(在类之外):

int get_digit(char c)
{
  return c-'0';
}
class BinaryCode {
  public:
    void decode(string decd)
    {
      int i;

      std::vector<int>decoded(decd.size());
      std::transform(decd.begin(), decd.end(), decoded.begin(), get_digit);

      int length=decoded.length();
    }
};

或创建一个函子:

struct get_digit
{
  int operator()(char c) const
  {
      return c-'0';
  }
};

//...
std::transform(decd.begin(), decd.end(), decoded.begin(), get_digit());
//...

You need to pass a function or functor as the last parameter, not a member function. If you have c++11 enabled, you can use lambda :

std::transform(decd.begin(), decd.end(), decoded.begin(),[this](const char it){ this->get_digit(it) } );

Since you do not have c++11 features, you can convert get_digit, into a function (outside of the class):

int get_digit(char c)
{
  return c-'0';
}
class BinaryCode {
  public:
    void decode(string decd)
    {
      int i;

      std::vector<int>decoded(decd.size());
      std::transform(decd.begin(), decd.end(), decoded.begin(), get_digit);

      int length=decoded.length();
    }
};

or create a functor :

struct get_digit
{
  int operator()(char c) const
  {
      return c-'0';
  }
};

//...
std::transform(decd.begin(), decd.end(), decoded.begin(), get_digit());
//...
献世佛 2025-01-04 03:09:23

1>
您可以将 get_digit 移到 BinaryCode 之外,然后您的代码就可以工作

2>
或者如果你希望 get_digit 成为一个非静态成员函数,那么你可以使用

  class BinaryCode {
    public:
        int get_digit(char c)
        {
                return c-'0';
        }
        void decode(string decd)
        {
            int i;
            std::vector<int>decoded(decd.size());
            std::transform(decd.begin(), decd.end(), decoded.begin(), std::bind1st(std::mem_fun(&BinaryCode::get_digit),this));
        }

};

3> 当然,如果你可以访问 boost 或 c++11,那么你可以轻松地使用 lambda,就像其他人已经向你展示的那样。

1>
You can either move the get_digit outside the BinaryCode then your code would work

2>
or if you want get_digit to be a non-static member function, then you can use

  class BinaryCode {
    public:
        int get_digit(char c)
        {
                return c-'0';
        }
        void decode(string decd)
        {
            int i;
            std::vector<int>decoded(decd.size());
            std::transform(decd.begin(), decd.end(), decoded.begin(), std::bind1st(std::mem_fun(&BinaryCode::get_digit),this));
        }

};

3>of course if you have access to either boost or c++11, then you can easily use lambda as others have already showed u.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文