Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to compile a c++ program in Ubuntu 12.04. I'm using g++ 4.7 (installed from a ppa). I'm using std::future, but I have an error.

The code:

$ 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;
}

My 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}

The error:

$ 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

How to get rid of this ? :)

share|improve this question

1 Answer 1

Please change the makefile like this

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 $@

When I tried to compile, it was throwing this error

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

Why don't you try the sample program shown here? http://en.cppreference.com/w/cpp/thread/future

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.