Reputation: 23
I'm new to programming so I apologize if this is an obvious question, however I am having trouble with the syntax presented to me in a book (Programming Principles and Practice using C++ 2nd Edition by the creator of C++, Bjarne Stroustrup). In it, the following is presented for creating a vector of strings:
vector<string> philosopher
={"Kant","Plato","Hume","Kierkegaard"};
However, when passing this through g++ it doesn't like it. My code is as follows:
#include "std_lib_facilities.h" //The author's library for his examples
int main()
{
vector<string>philosopher
={"Kant","Plato","Hume","Kierkegaard"};
}
I'm getting an error compiling:
g++ vecttest.cpp -std=c++11
In file included from /usr/local/include/c++/4.9.0/ext/hash_map:60:0,
from /usr/include/std_lib_facilities.h:34,
from vecttest.cpp:1:
/usr/local/include/c++/4.9.0/backward/backward_warning.h:32:2: warning: #warning
This file includes at least one deprecated or antiquated header which may
be removed without further notice at a future date. Please use a
non-deprecated interface with equivalent functionality instead. For a listing
of replacement headers and interfaces, consult the file backward_warning.h.
To disable this warning use -Wno-deprecated. [-Wcpp]
#warning \
^
In file included from /usr/local/include/c++/4.9.0/locale:41:0,
from /usr/local/include/c++/4.9.0/iomanip:43,
from /usr/include/std_lib_facilities.h:212,
from vecttest.cpp:1:
/usr/local/include/c++/4.9.0/bits/locale_facets_nonio.h:1869:5: error:
template-id ‘do_get<>’ for
‘String std::messages<char>::do_get(std::messages_base::catalog, int, int,
const String&) const’ does not match any template declaration
messages<char>::do_get(catalog, int, int, const string&) const;
^
/usr/local/include/c++/4.9.0/bits/locale_facets_nonio.h:1869:62: note:
saw 1 ‘template<>’, need 2 for specializing a member function template
messages<char>::do_get(catalog, int, int, const string&) const;
^
vecttest.cpp: In function ‘int main()’:
vecttest.cpp:8:42: error: could not convert ‘{"Kant", "Plato",
"Hume","Kierkegaard"}’from ‘<brace-enclosed initializer list>’ to ‘Vector<String>’
={"Kant","Plato","Hume","Kierkegaard"};
I thought maybe my version of GCC was older (Which it was at 4.7) so I updated it to 4.9:
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.9.0/configure
Thread model: posix
gcc version 4.9.0 (GCC)
Any ideas where I'm going wrong?
Thank you very much for your help.
Upvotes: 1
Views: 1446
Reputation:
You're going wrong with the use of this std_lib_facilities.h
. Looking at it online, it shows:
template< class T> struct Vector : public std::vector<T> {
...
};
// disgusting macro hack to get a range checked vector:
#define vector Vector
Unfortunately, this custom Vector
template class lacks some constructors that std::vector
does have.
Use std::vector
directly and it'll work. It's supported in GCC 4.4 and newer.
Note: to use std::vector
, you need to make sure you don't use std_lib_facilities.h
at all, or use #undef vector
. Macro definitions are problematic and pay no attention to namespaces, so std::vector
would become the non-existent std::Vector
.
Note 2: T.C. rightly comments that similar issues also exist for string
: use std::string
instead.
Upvotes: 2