c++ - No Matching Constructor for Initialization on Mac -


i learning c++ programming: principles , practice using c++ / edition 2 , i've run problem vectors. i'm using included header file provided stroustrup's book here. when compile following vector program, error.

#include "std_lib_facilities.h"  int main() {     vector<int> v = {5, 7, 9, 4, 6, 8};     (int i=0; i<v.size(); ++i)         cout << v[i] << endl; } 

error

vector.cpp:5:21 error: no matching constructor initialization of 'vector<int>'     vector<int> v = {5, 7, 9, 4, 6, 8};                 ^   ~~~~~~~~~~~~~~~~~~ ./std_lib_facilities.h:82:5: note: candidate constructor template not viable:        requires 2 arguments, 6 provided     vector(i first, last) :std::vector<t>(first, last) {}     ^ ./std_lib_facilities.h:79:14: note: candidate constructor not viable: requires 2        arguments 6 provided     vector(size_type n, const t& v) :std::vector<t>(n,v) {}     ^ ./std_lib_facilities.h:79:14: note: candidate constructor not viable: requires        single argument 'n', 6 arguments provided     explicit vector(size_type n) :std::vector<t>(n) {} ./std_lib_facilities.h:75:27: note: candidate constructor (the implicit move        constructor) not viable: requires 1 argument, 6 provided     template< class t> struct vector : public std::vector<t> {                               ^ ./std_lib_facilities.h:75:27: note: candidate constructor (the implicit copy    constructor) not viable: requires 1 argument 6 provided ./std_lib_facilities.h:78:5: note: candidate constructor not viable: requires 0        arguments, 6 provided     vector() { }     ^ 

i'm compiling with: clang++ -std=c++11 -stdlib=libc++ vector.cpp

when check version of clang, get:

apple llvm version 8.0.0 (clang-800.0.42.1) target: x86_64-apple-darwin16.1.0 thread model: posse installeddir: /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin 

i having trouble making sense of error , warnings , not sure go here. thank insight may provide.

std_lib_facilities.h defines class vector<t> as:

template< class t> struct vector : public std::vector<t> {     typedef typename std::vector<t>::size_type size_type;      vector() { }     explicit vector(size_type n) :std::vector<t>(n) {}     vector(size_type n, const t& v) :std::vector<t>(n,v) {}     template <class i>     vector(i first, last) :std::vector<t>(first,last) {}      t& operator[](unsigned int i) // rather return at(i);     {         if (i<0||this->size()<=i) throw range_error(i);         return std::vector<t>::operator[](i);     }     const t& operator[](unsigned int i) const     {         if (i<0||this->size()<=i) throw range_error(i);         return std::vector<t>::operator[](i);     } };  // disgusting macro hack range checked vector: #define vector vector 

as can see, there's no initializer_list constructor.

at point, options limited.

  1. you can eschew using std_lib_facilities.h
  2. you can #undef vector after including std_lib_facilities.h
  3. you can replace vector class template constructors inherited constructors (using vector<t>::vector;):

    template< class t> struct vector : public std::vector<t> {     typedef typename std::vector<t>::size_type size_type;      using std::vector<t>::vector;      t& operator[](unsigned int i) // rather return at(i);     {         if (i<0||this->size()<=i) throw range_error(i);         return std::vector<t>::operator[](i);     }     const t& operator[](unsigned int i) const     {         if (i<0||this->size()<=i) throw range_error(i);         return std::vector<t>::operator[](i);     } }; 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -