【问题标题】:Xcode C++ Vectors: Implicit instantiation of undefined templateXcode C++ 向量:未定义模板的隐式实例化
【发布时间】:2018-03-12 23:49:35
【问题描述】:

我在不同的 IDE 上运行了这段代码,它成功了。出于某种原因,我在 Xcode 上收到上述错误消息。我想我错过了某种标题,但我不确定是哪一个。

#include <iostream>
#include <limits>
#include <string>
#include <vector>

int main() {
    vector<string> listRestaurants;  // error: Implicit instantiation of undefined template
    return 0;
}

【问题讨论】:

  • std 命名空间包含这两个模板。将vector 更改为std::vector,将string 更改为std::string
  • 我想知道什么损坏的工具链按原样接受此代码。

标签: c++ xcode vector header implicit


【解决方案1】:

Xcode 10.2.1 向我显示错误 Implicit instantiation of undefined template 'std::__1::vector&lt;std::__1::basic_string&lt;char&gt;, std::__1::allocator&lt;std::__1::basic_string&lt;char&gt; &gt; &gt;'.

#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
  std::vector<std::string> listRestaurants;

  ....
  return 0;
}

解决了我的问题。

【讨论】:

  • 只需添加 #include &lt;vector&gt; 即可解决我的问题。
【解决方案2】:

如果添加 std:: 对您来说不是问题,请检查您是否有 #include &lt;vector&gt;。这解决了我的问题。

【讨论】:

    【解决方案3】:

    没有意识到#include &lt;vector&gt; 是必需的。我认为它是标准库模板的一部分;我在 VSCODE IDE 中运行了这段代码,对我来说效果很好

    #include <iostream>
    #include <vector>
    using namespace std;
    int main() 
    {
        uint_least8_t i; // trying to be conscious of the size of the int
        vector<int> vect;
        for(i = 0; i < 5; ++i) 
        {
            vect.push_back(i);
        }
        for(auto i : vect) 
        {
            cout << i << endl;
        }
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      来自cmets:

      std 命名空间包含这两个模板。将vector 更改为std::vector,将string 更改为std::string。 – WhozCraig

      【讨论】:

        【解决方案5】:

        向量和字符串被放置在命名空间std中 使用命名空间标准;

        【讨论】:

        • 他没有说要使用using。你总是可以像 std::vector 或 std::string 这样使用。顺便说一句,@RyanLi 是对的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 2013-08-10
        • 2021-09-02
        • 1970-01-01
        相关资源
        最近更新 更多