相关疑难解决方法(0)

在成员函数内的lambda捕获列表中使用成员变量

以下代码使用gcc 4.5.1编译,但不使用VS2010 SP1编译:

#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <set>
#include <algorithm>

using namespace std;
class puzzle
{
        vector<vector<int>> grid;
        map<int,set<int>> groups;
public:
        int member_function();
};

int puzzle::member_function()
{
        int i;
        for_each(groups.cbegin(),groups.cend(),[grid,&i](pair<int,set<int>> group){
                i++;
                cout<<i<<endl;
        });
}
int main()
{
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

error C3480: 'puzzle::grid': a lambda capture variable must be from an enclosing function scope
warning C4573: the usage of 'puzzle::grid' requires the compiler to capture 'this' but the current default capture mode does …
Run Code Online (Sandbox Code Playgroud)

c++ lambda visual-studio-2010 c++11

127
推荐指数
4
解决办法
13万
查看次数

C++ 0x线程静态链接问题

我有一些问题试图使用c ++ 0x线程功能静态链接程序.代码看起来:(编译器是Debian x86_64测试的gcc 4.6.1)

#include <iostream>
#include <thread>

static void foo() {
  std::cout << "FOO BAR\n";
}

int main() {
  std::thread t(foo);
  t.join();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我把它链接到:

g++ -static -pthread -o t-static t.cpp -std=c++0x
Run Code Online (Sandbox Code Playgroud)

当我执行该程序时,我有以下错误:

terminate called after throwing an instance of 'std::system_error'
  what(): Operation not permitted
Aborted
Run Code Online (Sandbox Code Playgroud)

GDB Debug输出如下所示:

Debugger finished
Current directory is ~/testspace/thread/
GNU gdb (GDB) 7.2-debian
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11

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

标签 统计

c++ ×2

c++11 ×2

lambda ×1

multithreading ×1

visual-studio-2010 ×1