未定义模板的隐式实例化“std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allo

Posted

技术标签:

【中文标题】未定义模板的隐式实例化“std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >”【英文标题】:implicit instantiation of undefined template "std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >" 【发布时间】:2020-05-11 17:28:07 【问题描述】:

我正在学习 C++ 并遵循教程,但在构建初始代码中的代码时遇到了问题。

这是引发此错误的源代码文件。

#include "linux_parser.h"

#include <dirent.h>
#include <unistd.h>
#include <istream>
#include <string>
#include <vector>

using std::stof;
using std::string;
using std::to_string;
using std::vector;

// DONE: An example of how to read data from the filesystem
string LinuxParser::OperatingSystem() 
  string line;
  string key;
  string value;
  std::ifstream filestream(kOSPath);
  if (filestream.is_open()) 
    while (std::getline(filestream, line)) 
      std::replace(line.begin(), line.end(), ' ', '_');
      std::replace(line.begin(), line.end(), '=', ' ');
      std::replace(line.begin(), line.end(), '"', ' ');
      std::istringstream linestream(line);
      while (linestream >> key >> value) 
        if (key == "PRETTY_NAME") 
          std::replace(value.begin(), value.end(), '_', ' ');
          return value;
        
      
    
  
  return value;

以下是我在 mac Catalina 本地运行 make build 时抛出的错误。

Scanning dependencies of target monitor
[ 12%] Building CXX object CMakeFiles/monitor.dir/src/format.cpp.o
[ 25%] Building CXX object CMakeFiles/monitor.dir/src/linux_parser.cpp.o
/Users/Documents/github/cpp/src/linux_parser.cpp:25:26: error: implicit instantiation of undefined template
      'std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >'
      std::istringstream linestream(line);
                         ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:133:32: note: template is declared here
    class _LIBCPP_TEMPLATE_VIS basic_istringstream;
                               ^
/Users/Documents/github/cpp/src/linux_parser.cpp:44:24: error: implicit instantiation of undefined template
      'std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >'
    std::istringstream linestream(line);
                       ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:133:32: note: template is declared here
    class _LIBCPP_TEMPLATE_VIS basic_istringstream;
                               ^
2 errors generated.
make[3]: *** [CMakeFiles/monitor.dir/src/linux_parser.cpp.o] Error 1
make[2]: *** [CMakeFiles/monitor.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [build] Error 2

【问题讨论】:

【参考方案1】:

您需要#include &lt;sstream&gt; 才能使用std::istringstream

【讨论】:

效果很好!我以为是istream。谢谢@brian

以上是关于未定义模板的隐式实例化“std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allo的主要内容,如果未能解决你的问题,请参考以下文章

如何防止特定模板的隐式模板实例化?

MSVC:隐式模板实例化,但未使用模板构造函数

Scala 类型参数化,Shapeless - 找不到参数 Generic 的隐式值

25.scala的隐式转换

MSVC:显式模板实例化失败,而隐式实例化成功

带有 Clang 10 显式模板实例化的 ~queue 的未定义引用