Я пытаюсь скомпилировать программу на C ++ в Ubuntu 12.04. Я использую g ++ 4.7 (устанавливается из ppa). Я использую std :: future, но у меня ошибка.
Код:
$ cat main.cpp
#include <iostream>
#include <boost/thread/thread.hpp>
#include <future>
int foo(const int & i)
{
return i*i;
}
int main()
{
for(int i = 0; i<10; ++i)
{
std::future< int > Foo = boost::bind(&foo, i);
std::cout<<Foo<<std::endl;
}
return 0;
}
Мой Makefile:
$ cat Makefile
all:ex
CC= g++-4.7 -std=gnu++0x
link= -o
arg= -c -o
lib= -lboost_thread-mt
objets= main.o
ex: ${objets}
@${CC} ${link} ex ${objets} ${lib}
%.o: %.cpp %.h
@${CC} ${arg} $@ $< ${lib}
Ошибка:
$ make
g++ -c -o main.o main.cpp
In file included from /usr/include/c++/4.6/future:35:0,
from main.cpp:3:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
main.cpp: In function ‘int main()’:
main.cpp:14:3: error: ‘future’ is not a member of ‘std’
main.cpp:14:16: error: expected primary-expression before ‘int’
main.cpp:14:16: error: expected ‘;’ before ‘int’
main.cpp:17:14: error: ‘Foo’ was not declared in this scope
make: *** [main.o] Error 1
Как от этого избавиться? :)
Измените make-файл как это
CC=g++
CFLAGS=-c -Wall -std=gnu++0x
LDFLAGS=
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=main
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
$(CC) $(CFLAGS) $< -o $@
Когда я пытался скомпилировать, это бросало эту ошибку
main.cpp: In function ‘int main()’:
main.cpp:14:53: error: conversion from ‘boost::_bi::bind_t<int, int (*)(const int&), boost::_bi::list1<boost::_bi::value<int> > >’ to non-scalar type ‘std::future<int>’ requested
main.cpp:15:20: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
In file included from /usr/include/c++/4.7/iostream:40:0,
from main.cpp:1:
/usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::future<int>]’
make: *** [main.o] Error 1
Почему Вы не пробуете пример программы, показанный здесь? http://en.cppreference.com/w/cpp/thread/future