Xcode C ++向量:未定义模板的隐式实例化

Nat*_*ale 6 c++ xcode header vector implicit

我在不同的IDE上运行了此代码,并获得了成功。由于某种原因,我在Xcode上收到上述错误消息。我认为我缺少某种标题,但是我不确定哪个标题。

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

int main() {
    vector<string> listRestaurants;  //here is where the semantic error appears
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

小智 10

如果添加 std:: 对您来说不是问题,那么检查您是否有#include <vector>. 这为我解决了这个问题。


Sta*_*ver 6

没有意识到这#include <vector>是必需的。我认为它是标准库模板的一部分;我在 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;
}
Run Code Online (Sandbox Code Playgroud)


Sau*_*lui 5

Xcode 10.2.1向我显示了错误 Implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'

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

  ....
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

解决了我的问题。

  • 只需添加“#include &lt;vector&gt;”即可解决我的问题。 (11认同)

Rya*_*nLi 0

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

  • [**永远不要**使用`using namespace std;`](/sf/ask/101690501/)。 (3认同)