【问题标题】:Compilation error - more than one instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string编译错误 - 多个构造函数实例 "std::basic_string<_Elem, _Traits, _Alloc>::basic_string
【发布时间】:2020-08-10 10:37:13
【问题描述】:

我是 C++ 语言的新手,我正在尝试按照我在网上找到的示例进行操作。 我正在逐行复制粘贴在线代码并在添加每一行后尝试构建(因为我确信如果我复制粘贴整个代码它永远不会编译:)

我面临以下错误:

E0309 多个构造函数实例 "std::basic_string<_elem _traits _alloc>::basic_string [with _Elem=char, _Traits=std::char_traits, _Alloc=std::allocator]" 匹配参数列表

C2668 'std::basic_string::basic_string': 对重载函数的模糊调用

我的代码是:

#include <iostream>
#include <limits>
#include <fstream>
#include <string>

#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

int main()
{
    using namespace std;
    string filename(0, MAX_PATH);  // <------- This is the line which throws the error

    std::cout << "Hello World!\n";

    std::cout << "Press ENTER to continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

【问题讨论】:

  • 你打算让那行代码做什么?无论如何,“尝试遵循我在网上找到的示例”并不是学习 C++ 的一种非常有效的方法。好多了to use a good C++ textbook.
  • 可以使用您提供的参数调用多个构造函数,您想在那里做什么? MAX_PATH 大小的字符串用'\0' 填充?没有进一步的解释,这没有多大意义。
  • 谢谢,这似乎解释了它。要回答您的问题,就像我在问题中提到的那样,只需遵循代码 sn-p,并尝试了解它的作用。
  • @RamySameh,好吧,注意网上有各种不好的代码,尽量从权威来源获取你的例子。
  • 在线阅读有关我的问题的更多信息后,我发现我对 C++ 真的很陌生 :( 有很多东西要学。

标签: c++


【解决方案1】:

以防其他人遇到同样的问题(极不可能),此行的正确语法是:

string filename(MAX_PATH, '\0');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-14
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多