gpt4 book ai didi

c++ - 为什么没有include语句头文件编译成功?

转载 作者:行者123 更新时间:2023-12-01 19:56:35 29 4
gpt4 key购买 nike

下面的代码编译成功,但我认为缺少#include<string> , 为什么?如果我只包含字符串而不是 vector ,则会失败。谁能解释一下吗?

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

编译:

g++ StrBlob.hpp

我只编译这个文件,没有其他的。编译成功。

更新

如果我添加主文件:

main.cc

#include "StrBlob.hpp"
int main() {
int i = 0;
return 0;
}

就这样做:

g++ main.cc

失败了。编译输出:

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.

需要#include <string> 。我想原因可能正如Peter Clark所说。

最佳答案

在某些平台上,某些系统 header 将包含其他平台不包含的文件。在我使用过的任何版本的 MSVC 上 <vector>包括<string> 。在 GCC 上 - 根据我的经验 - 事实并非如此,因此该代码无法编译。一般来说,您要确保包含您使用的所有内容,即使它没有编译。

要查看您的编译器是否存在这种情况,您可以将预处理器输出保存到文件中(例如 this is how you do it in GCCMSVC )。然后您可以搜索 basic_string 并查看它是从哪里包含的。以我的 MSVC 为例:

#line 7 "w:\\program files (x86)\\microsoft visual studio 12.0\\vc\\include\\vector"
#line 1 "w:\\program files (x86)\\microsoft visual studio 12.0\\vc\\include\\stdexcept"

#line 1 "w:\\program files (x86)\\microsoft visual studio 12.0\\vc\\include\\xstring"

您可以看到vector包括stdexcept其中包括xstring这是 implementation of std::basic_string is defined for MSVC 所在的地方

请注意,您也可以只查看标准 header ,但它可能隐藏在不明显的地方(我没想到它会通过 <stdexcept> )。

这个故事的寓意是,即使它可以编译,也要确保您始终包含您使用的内容,以避免以后遇到问题(这适用于包含一般文件,而不仅仅是标准文件)库)。

对更新的响应

正如其他人所说,所有 #include本质上是粘贴包含指令所在的头文件中的代码。因此,在预处理之后(当包含内容被“粘贴”时),编译器将看到的是:

// contents of <vector> header would be pasted here

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;
};

int main() {
int i = 0;
return 0;
}

以前和现在的主要区别在于,现在您既要链接,又要编译。目前,虽然我老实说不确定到底是什么导致了这种情况,但在我看来,这不值得了解,因为它只是编译器标准库的实现细节。

要知道的主要事情是始终包含您使用的内容(您还缺少 <memory> 作为 std::shared_ptr )。 系统 header 可能包含或不包含的内容取决于实现,因此您不应该依赖或(希望)需要担心。

关于c++ - 为什么没有include语句头文件编译成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245240/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com