2016-09-15 4 views
-3

私は赤ちゃんの名前のリストを使ってプログラムを作成していますが、ファイルを開くために別の関数を作ることにしました。これはこれまでのところあります。.txt関数がメイン関数にリンクしていません

#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"; 


} 

私は非常に多くのエラーメッセージを取得していますなぜ、誰もが知っている:

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) 

誰もが私が間違っているの何を知っていて、私はそれが比較的近いように私は流れにちょうどかなり新たなんだと感じC++で。

+0

必要な機能を定義してリンクします。 – MikeCAT

答えて

0

示すコードを宣言し、次の関数を呼び出す:

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

残念ながら、図示のコードは、これら2つの関数のいずれかを定義しないと、2つのリンクエラーは、その結果です。

示されたコードは、open_file()とも呼ばれる機能をいくつか定義していますが、異なるパラメータを使用するため、全く異なる機能です。示されたコードはfind_name()と呼ばれる機能を定義していません。

あなたは、単にのような関数を宣言することはできません。

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

そして、この関数のコードが自動的にどこかに現れることを期待しています。この関数の内容を定義して記述する必要があります。この関数のパラメータは、定義するときに、ここで宣言したものと同じでなければなりません。

関連する問題