0

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');
}
Ramy Sameh
  • 271
  • 4
  • 13
  • 3
    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](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Aug 10 '20 at 10:40
  • 2
    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 Aug 10 '20 at 11:06
  • 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 Aug 10 '20 at 11:10
  • 1
    @RamySameh, Ok then, note that there are all kinds of bad code online, try to get your examples from authoritative sources. – anastaciu Aug 10 '20 at 11:15
  • After reading more about my problem online, I discovered that I am REALLY new to C++ :( There is so much to learn. – Ramy Sameh Aug 10 '20 at 11:18

1 Answers1

0

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

string filename(MAX_PATH, '\0');
Ramy Sameh
  • 271
  • 4
  • 13