Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

vector

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;
}
like image 925
Kori Avatar asked Jan 01 '26 02:01

Kori


1 Answers

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.

like image 112
EJ Googins Avatar answered Jan 02 '26 16:01

EJ Googins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!