Unknown Error

So im trying to write a program that reads the contents of two files into two separate vectors (GirlNames.txt and BoyNames.txt). Basically, I want the user to enter a boy name and a girl name and the code will judge whether or not those names are popular. I don't really understand what the error means or what to do. I was told it might be my compiler but I'm not sure. (I use Dev-C++) I tried Visual Studios but I don't understand how to use it and I think online compilers just run the code without searching through my folders for the txt files.

This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;


vector<string> readNames(string fileName) {
    vector<string> names;
    string line;
    
    ifstream f2(fileName); // This is the line with the error
    
    while (getline (f2, line)) {
        names.push_back(line);
    }
    
    f2.close();  
    
    return names;
}

int isNamePresent(vector<string> names, string nameToCompare) {
    int result = 0;
    
    for(int i = 0; i < names.size(); i++) {
        if(names[i] == nameToCompare) {
            result = 1;
            break;
        }
    }    
    
    return result;
}

void displayResult(vector<string> names, string nameToCompare, string label) {
    if(nameToCompare != "N") {
        if(isNamePresent(names, nameToCompare) == 1)
            cout << nameToCompare << " is one of the most popular " << label << "'s names." << endl;   
        else
            cout << nameToCompare << " is not one of the most popular " << label << "'s names." << endl;    
    }
}

int main()
{
    string fileNameBoys = "BoyNames.txt";
    string fileNameGirls = "GirlNames.txt";
    
    vector<string> boyNames = readNames(fileNameBoys);

    vector<string> girlNames = readNames(fileNameGirls);
    
    string userInputBoyName, userInputGirlName;

    cout << "Enter a boy's name, or N if you do not wish to enter a boy's name: ";
    cin >> userInputBoyName;

    cout << "Enter a girl's name, or N if you do not wish to enter a girl's name: ";
    cin >> userInputGirlName;
    
    displayResult(boyNames, userInputBoyName, "boy");

    displayResult(girlNames, userInputGirlName, "girl");

    return 0;
}


This is the error I'm getting along with some other things:

(In function)
[Error] no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'

[Note] candidates are:

(In file)
[Note] std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]

[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'

[Note] std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]

[Note] candidate expects 0 arguments, 1 provided

[Note] std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)

[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const std::basic_ifstream<char>&'
Last edited on
Hello SweatyCoder,

It is best to copy and paste the complete error message. With out line numbers it makes it hard to figure out where you went wrong.

DEV C++ is old and outdated. Unless you know how to update the IDE to use C++11 standards you are likely to have many problems. If you need help with that I have instructions I can post.

On line 13 you define a file stream and open the file, but how do you know it opened and is usable?

Something to start on while give your code a compile.

Both MSVS 2017 Community and 2019 Community are good IDEs. Code::Blocks is also a good IDE. I use both the MSVS 2017 and 2019 versions. Like any new program it takes time to get use to.

Andy
Your error, simplified:
[Error] no matching function for call to 'std::ifstream::ifstream( std::string& )'
[Note] candidates are:
1. std::ifstream()
2. std::ifstream( const char*, std::ios_base::openmode )

[Note] no known conversion for argument 1 from 'std::string' to 'const char*'

See http://www.cplusplus.com/reference/fstream/ifstream/ifstream/

Before C++11 the std::ifstream had two constructors:
1
2
ifstream();
explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);

Since C++11 there are four that you can use:
1
2
3
4
ifstream();
explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
explicit ifstream (const string& filename, ios_base::openmode mode = ios_base::in);
ifstream (ifstream&& x);


There are now two options:
1. Enable C++11 support in your compiler
2. Get data from string 'fileName' as const char*

C++11 has been around for a decade. I'd say its time to learn and use it.
There are also C++14, C++17, and C++20 so skipping the 11 could be sane.
Staying with pre-C++11 is much less so.
Hello SweatyCoder,

I compiled your code with MSVS 2017 using the C++14 standards and had no errors. Did have 1 warning easily corrected.

Line 27 in the for loop "names.size()" returns a number of type "size_t" an "unsigned integral" type. It helps when the loop iterator matches, but not something that would stop the program from running.

After reading keskiverto's post I would agree that your DEV C++ is using pre 2011 standards. I believe the standard it starts with is 1998.


The DEV C++ that I have is version 5.11 with a build year of 2015.

To adjust the settings:
    •	Under the Tools menu choose “Compiler Options”.
    •	In the window that comes up you will see tabs for “General”, “Settings”, “Directories” and “Programs”.
    •	Choose the settings tab.
    •	In the next set of tabs that come up choose “Code Generation”.
    •	The last line should say “Language Standard (-std).
    •	On the right side of that line click on the down arrow.
    •	In the list box that comes up choose “ISO C++ 11”.
    •	Press “OK”.

This will let the IDE and compiler use the C++11 standards.



In the function "readNames" add this if statement:
1
2
3
4
5
6
7
8
9
ifstream f2(fileName); // This is the line with the error

if (!f2)
{
    //std::cout << "\n     File " << std::quoted(inFileName) << " did not open.\n";  // <--- Requires header file "<iomanip>".
    std::cout << "\n     File \"" << fileName << "\" did not open.\n";

    return 1;
}

The only problem is that the function needs to return an "int" not a vector. Instead you should pass the vector you need by reference, much less work this way, and use what you already have.

I will have to work on that one.

Andy
Hello SweatyCoder,

The changes I made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int readNames(string fileName, vector<string>& names)
{
    //vector<string> names;  // <--- Covered in the parameters.
    string line;

    ifstream f2(fileName); // This is the line with the error. You could try "filename.c_str()" for pre2011 standards.

    if (!f2)
    {
        //std::cout << "\n     File " << std::quoted(inFileName) << " did not open.\n";  // <--- Requires header file "<iomanip>".
        std::cout << "\n     File \"" << fileName << "\" did not open.\n";

        return 1;
    }

    while (getline(f2, line))
    {
        names.push_back(line);
    }

    //f2.close();  // <--- Not required as the dtor will close the file when the function looses scope.

    return 0;  // <--- Means everything worked.
}


Then in "main":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    const string fileNameBoys{ "BoyNames.txt" };
    const string fileNameGirls{ "GirlNames.txt" };

    int result{};
    vector<string> boyNames;
    vector<string> girlNames;

    if(result=readNames(fileNameBoys, boyNames))
       return result;

    if (result = readNames(fileNameGirls, girlNames))
        return result;

Not having any input files to use, it would help if you post the input files or at least a fair sample, so everyone would have the same files to use.

Andy
Thank you, all for the help I will be trying every solution. I also had no idea I was using an old version of C++. I've been using Dev C++ since high school and never had a problem with it until now. We live and we learn. Once again thank you all.
Topic archived. No new replies allowed.