4

Hi,

I have some simple code shown below, saved into a file (let us say stock_portfolio.cxx).

I am trying to compile this as: g++ stock_portfolio.cxx

but I get the following error during compiling stage:

error: scalar object 'v' requires one element in initializer

The gcc version I have is: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52)

#include<iostream>
#include<vector>
int main() {
 std::vector<int>v = {1,2,3,4,5,6};
 //std::vector<string> four_star_stocks;
 for(int i = 0; i < v.size(); ++i){
    std::cout << "Stock S&P: " << v[i] << "\n";
 }
 std::cout << "========================" << "\n";
 std::cout << "Totals   : " << v.size() << "\n";
 return 0;
}
4
  • 4.1.2 ? Um... does anyone know off the top of their head when gcc added support for initializer-lists ? Commented Oct 9, 2014 at 21:34
  • @WhozCraig 4.4 (Source) Commented Oct 9, 2014 at 21:35
  • @cdhowie yeah, i kinda figured 4.1.2 was a little ancient for that feature. Commented Oct 9, 2014 at 21:36
  • Note that prior to C++11 you can use boost::assign for this task: std::vector<int> v = boost::assign::list_of(1)(2)(3)(4)(5)(6);. Commented Oct 9, 2014 at 21:37

2 Answers 2

2

list-initialization was only introduced to C++ in C++11. gcc version 4.1 doesn't support C++11 (see https://gcc.gnu.org/projects/cxx0x.html)

It's unclear to me if you're question is asking for a suggested solution/fix or an explanation of why your code will not compile.

Sign up to request clarification or add additional context in comments.

Comments

0

Initialize your vector in a loop, like this:

for(int i = 1; i <= 6; ++i)
  v.push_back(i);

As cdhowie suggests, your version of gcc doesn't support the initializer list (you need at least g++ version 4.4, source). In case you get a newer one (adding the flag -std=c++0x or -std=gnu++0x), then you could see the following:

std::vector<int> v = {1,2,3,4,5,6};

or

If you want to do that with an initializer list, then you should use std::initializer_list like this:

#include <iostream>
#include <vector>
#include <initializer_list>

template <class T>
struct S {
    std::vector<T> v;
    S(std::initializer_list<T> l) : v(l) {
         std::cout << "constructed with a " << l.size() << "-element list\n";
    }
    void append(std::initializer_list<T> l) {
        v.insert(v.end(), l.begin(), l.end());
    }
    std::pair<const T*, std::size_t> c_arr() const {
        return {&v[0], v.size()};  // list-initialization in return statement
                                   // this is NOT a use of std::initializer_list
    }
};

template <typename T>
void templated_fn(T) {}

int main()
{
    S<int> s = {1, 2, 3, 4, 5}; // direct list-initialization
    s.append({6, 7, 8});      // list-initialization in function call

    std::cout << "The vector size is now " << s.c_arr().second << " ints:\n";

    for (auto n : s.v) std::cout << ' ' << n;

    std::cout << '\n';

    std::cout << "range-for over brace-init-list: \n";

    for (int x : {-1, -2, -3}) // the rule for auto makes this ranged for work
        std::cout << x << ' ';
    std::cout << '\n';

    auto al = {10, 11, 12};   // special rule for auto

    std::cout << "The list bound to auto has size() = " << al.size() << '\n';

//    templated_fn({1, 2, 3}); // compiler error! "{1, 2, 3}" is not an expression,
                             // it has no type, and so T cannot be deduced
    templated_fn<std::initializer_list<int>>({1, 2, 3}); // OK
    templated_fn<std::vector<int>>({1, 2, 3});           // also OK
}

Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.