简单代码需要帮助 - 没有与参数列表匹配的构造函数实例

5

我正在学习C++编程的一本书,现在遇到了vector的问题。书上的例子如下:

vector<int> v = {1,2,3};

但是我遇到一个错误:
    1   IntelliSense: no instance of constructor "Vector<T>::Vector [with T=int]" matches the argument list
        argument types are: (int, int, int) ../path

另外,当我创建字符串向量时:
vector<string> v = {"one", "two", "three"}

我得到了这个错误:
    1   IntelliSense: no instance of constructor "Vector<T>::Vector [with T=std::string]" matches the argument list
        argument types are: (const char [4], const char [4], const char [6]) ../path

我正在使用带有2013年11月CTP编译器的VS 2013。我做错了什么吗?


你是否包含了 #include <vector> - Brian Bi
1
“Vector<T>”是什么意思,我指的是大写字母V? - 101010
是的,我确实使用了 #include <vector>。我真的不知道这些错误信息是什么意思,我还是个新手。 - user3650284
尝试使用 std::vector<int> v = {1,2,3};std::vector<std::string> v = {"one", "two", "three"};,然后回报结果。 - 101010
@user3650284 如果你是通过这本书学习C++,那么你可以直接使用新版本的头文件。他在头文件中使用了一些巧妙的构造来简化初学者的学习过程。 - T.C.
显示剩余10条评论
3个回答

5

总结和扩展一下在评论和Bjarne Stroustrup的 "std_lib_facilities.h" 标头中所写的内容:

  • The header contains a trivially range-checked vector class called Vector for teaching purposes;
  • To make Vector a "seamless" replacement for vector in the standard library (again, for teaching purposes), the header contains the following lines:

    // disgusting macro hack to get a range checked vector:
    #define vector Vector
    
  • The OP likely used the header for the first edition of the book (it's the top Google search result for std_lib_facilities.h), whose Vector doesn't have an initializer_list constructor (that edition uses C++98, which doesn't have initializer lists).
  • As a result, the compiler complains that Vector doesn't have a matching constructor when it sees vector<int> v = {1,2,3};, which becomes Vector<int> v = {1,2,3}; after macro replacement.
为了解决问题,请下载并使用正确版本的标题。

1

这可能与您配置的编译环境有关。

以我的为例:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
     vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

     for (const string& word : msg) {
          cout << word << " ";
     }
     cout << endl;
}

如果在在线编译器中编译成功,那么就能成功运行。
eg:programiz:

https://www.programiz.com/cpp-programming/online-compiler/

此外,输出内容为:
Hello C++ World from VS Code and the C++ extension! 

但如果编译器在Visual Studio Code中,那么你将面临相同的问题。

“no instance of constructor "std::__1::vector<_Tp, _Allocator>::vector [with _Tp=std::__1::string, _Allocator=std::__1::allocator<std::__1::string>]" matches the argument list”

怎么会发生这种情况?或者说,到底发生了什么?

我从来没有配置过这些文件,例如:"launch.json""c_cpp_properties.json"

我在下载 VS Code 和 C/C++ 插件后直接编译。

我认为解决问题的关键是确保已经配置好了编译环境。

例如:如果 IDE 出现问题,则可以将其迁移到在线编译器中进行测试是否能够成功编译。

如果和我一样,在没有配置文件的情况下仍然出现相同的问题,那么问题的根本原因就是配置文件配置失败。

最后,Visual Studio Code 在 macOS 中配置 C/C++ 编译环境文件比较困难,但是可以解决。

另外,也许这与您的 C++ 语言版本有关。

Use g++ -std=c++11 <filename> when compiling.

使用这个命令来运行你的代码会更好:

g++ -g -Wall -std=c++11 helloworld.cpp

或者,您可以在VS Code中配置tasks.json:
"args": [
      "-g",
      "-Wall",
      "-std=c++11",
      "-stdlib=libc++",
      "-g",
      "${file}",
      "-o",
      "${fileDirname}/${fileBasenameNoExtension}"
    ],

在任务的"args"中添加"-g""-Wall"

简而言之,

首先,

报告了这个错误是因为C++98不允许您在初始化向量容器时指定初始元素值。

这是在C++11中,您需要配置c++11支持。

  1. 如果您的问题出现在VS Code中,则需要在.vscode/task.json中配置c++11支持,加上-std=c++11-stdlib=libc++配置。

其次,

如果您还在VS代码中使用C/C++ Clang命令适配器插件,

由于C/C++ Clang命令适配器插件默认未配置为支持C++11,

您还需要配置c++11支持。

1.点击Code->Preferences->Settings,搜索Clang: CflagsClang: Cxxflags并添加C++11配置;

2.Clang: Cflags添加项:"-std=c11"Clang: Cxxflags添加项:"-std=c++11"

第三,

您还需要C++11运行环境配置。

  1. 点击Code Runner⚙的齿轮(或直接右键单击)以打开扩展设置;

  2. 找到Code-runner: Executor Map并单击Edit in settings.json

  3. "code-runner.executorMap"中找到"cpp"行;

  4. -std=c++11添加到$fileNameWithoutExt的末尾,并保存以供使用。

或者,

1.编辑您的环境配置./vscode/setting.json/

2.找到"code-runner.executorMap":

3.在"code-runner.executorMap":中找到"cpp"

4.在$fileNameWithoutExt&& $dir$fileNameWithoutExt之间添加-std=c++11

就像这样:

enter image description here


0
尝试使用#include <string>来定义字符串。 还有using namespace std; 最后一行让你可以省略在东西前面加上std::

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接