0
votes

I am new in C++ language, and I am trying to follow an example I found online. I am copying-pasting the online code line by line and trying to build after adding each line (because I am sure that it would never compile if I copy-pasted the whole code :)

I am facing the below errors:

E0309 more than one instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=char, _Traits=std::char_traits, _Alloc=std::allocator]" matches the argument list

C2668 'std::basic_string<char,std::char_traits,std::allocator>::basic_string': ambiguous call to overloaded function

My code is :

#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');
}
1
What do you intend for that line of code to do? In any case "trying to follow an example I found online" is not a very efficient way to learn C++. It's much better to use a good C++ textbook. - Sam Varshavchik
More than one constructor can be called with the arguments you provide, what do you want to do there? A string with MAX_PATH size filled with '\0'? It does not make much sense without further explanation. - anastaciu
Thank you, this seems to explain it. To answer your question, it is like I mentioned in my question, just following a code snippet, and trying to understand what it does. - Ramy Sameh
@RamySameh, Ok then, note that there are all kinds of bad code online, try to get your examples from authoritative sources. - anastaciu
After reading more about my problem online, I discovered that I am REALLY new to C++ :( There is so much to learn. - Ramy Sameh

1 Answers

0
votes

Just in case someone else faced the same issue (highly unlikely), the correct syntax for this line is:

string filename(MAX_PATH, '\0');