The codes below is compiled successfully, but I think it lacks #include<string>
, why?
If I just include string not vector, it fails. Could anybody give an explanation?
StrBlob.hpp:
#ifndef STRBLOB_H_
#define STRBLOB_H_
#include <vector>
class StrBlob {
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> i1);
bool empty() const {return data->empty();}
void push_back(const std::string &s) {data->push_back(s);}
void pop_back();
std::string &front();
std::string &back();
private:
std::shared_ptr<std::vector<std::string>> data;
void check(size_type i, const std::string &msg) const;
};
#endif
compile:
g++ StrBlob.hpp
I only compile this file without others. It compiles successfully.
update
If I add a main file:
main.cc
#include "StrBlob.hpp"
int main() {
int i = 0;
return 0;
}
just do:
g++ main.cc
It fails. The compilation output:
In file included from test_strblob.cc:1:
In file included from ./StrBlob.hpp:3:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:1540:9: error: implicit
instantiation of undefined template 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >'
++this->__end_;
^
./StrBlob.hpp:11:49: note: in instantiation of member function 'std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >
>::push_back' requested here
void push_back(const std::string &s) {data->push_back(s);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iosfwd:188:33: note: template is declared
here
class _LIBCPP_TYPE_VIS_ONLY basic_string;
^
In file included from test_strblob.cc:1:
In file included from ./StrBlob.hpp:3:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__bit_reference:15:
▽
3 #ifndef STRBLOB_H_
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:627:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1641:31: error: implicit
▽
instantiation of undefined template 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1568:18: note: in instantiation of
function template specialization 'std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>
> >::construct<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, const std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > &>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:1449:14: note: in instantiation of
function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > > >::__construct<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > &>' requested
here
{__construct(__has_construct<allocator_type, pointer, _Args...>(),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:1538:25: note: in instantiation of
function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > > >::construct<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > &>' requested
here
__alloc_traits::construct(this->__alloc(),
^
./StrBlob.hpp:11:49: note: in instantiation of member function 'std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >
>::push_back' requested here
void push_back(const std::string &s) {data->push_back(s);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iosfwd:188:33: note: template is declared
here
class _LIBCPP_TYPE_VIS_ONLY basic_string;
^
2 errors generated.
It needs #include <string>
. I think the reason may be as Peter Clark said.