-3

I am making a program with a list of baby names but I've decided to make a seperate function to open the file, this is what I have got so far.

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

using namespace std;


void open_file(ifstream& in, char fileName[]);
void find_name(ifstream& in, string name, int numNames);



int main() {

    const int NUMNAMES = 1000;
    ifstream inStream;
    char fileName[30];
    string name;
    cout << "Enter the name of the file that contains the names: " << endl;
    open_file(inStream, fileName);
    cout << "Enter the name to search for (capitalize first letter): " << endl;
    cin >> name;
    find_name(inStream, name, NUMNAMES);
    inStream.close();

}


void open_file(ifstream& ) {

    string line;
    ifstream myfile ("babyNames.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    }

    else cout << "I/O failure opening file babyNames";


}

Does anyone know why I am getting so many error messages:

Undefined symbols for architecture x86_64:
  "find_name(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
      _main in Untitled-1b6d2e.o
  "open_file(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, char*)", referenced from:
      _main in Untitled-1b6d2e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Does anyone know what I am doing wrong, I feel like it is relatively close I'm just fairly new to streams in c++.

1 Answers1

0

The shown code declares and calls the following functions:

void open_file(ifstream& in, char fileName[]);
void find_name(ifstream& in, string name, int numNames);

Unfortunately, the shown code does not define any of these two functions, and the two linking errors are the result of that.

The shown code does define some function that's also called open_file(), but it's a completely different function because it takes different parameters. The shown code does not define any function called find_name().

You cannot simply declare a function like:

void open_file(ifstream& in, char fileName[]);

And then expect the code for this function to automatically appear somewhere. You have to define, and write the contents of this function. The parameters in this function, when you define it, must be the same as what you declared here.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148