2012-01-20 15 views
3

私はすべてのファイルとクラスと関数の定義との間の接続をチェックしましたが、私のプログラムを実行しようとするたびに私を止めて、 "1未解決外部 "と呼ばれる。"1未解決の外部" C++

プログラムは、複数のファイル(「学生」ファイルと「成績」ファイル)を開き、そこから読み込んだ後、「クエリファイル」を使用してデータを見て、 forのクエリファイルを作成し、新しいファイルに出力します。混乱している?はい。

まだ解決策に近いですが、私はVisual Studioを使用しているため、見つからずに「未解決の外部1」を終了するまでプログラムを実行することもできません。エラーがどこにあるのかわからない。私はC++をかなり新しくしています。検索しても、問題を解決できないようです。ここで

は私のメインプログラムです:

#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 
#include <fstream> 

#include "Student.h" 

using namespace std; 

int main(){ 



    //Request a file to read from, and open it (I haven't declared the files yet, don't worry about that) 
    ifstream input_file; 
    string studentfile = ""; 
    string gradefile = ""; 
    string queryfile = ""; 


    //Create a Map for the Students 
    map<string, Student *> student_map; 

    //Open the Student file and load it in 
    cout << "Loading \"" << studentfile << "\"... " <<endl; 
    input_file.open(studentfile); 

    //Look for: 
    string id_number; 
    string name; 
    string address; 
    string phone; 

    //Boolean value to check for duplicate students 
    bool duplicate = false; 

    //Check to see if the Student File is empty 
    if (!input_file.eof()){ 

     while (input_file.good()){ 
      //Get the ID Number 
      input_file.get(); 
      getline (input_file, id_number); 

      //Sort through and make sure there are no duplicate students 
      for (map<string, Student *>::iterator counter = student_map.begin(); counter != student_map.end(); counter ++){ 
       if (counter->first == id_number){ 
        duplicate = true; 
        counter = student_map.end(); 
       } 
      } 

      if (duplicate != true){ 
       //Get the name 
       input_file.get(); 
       getline (input_file, name); 

       //Get the Address 
       input_file.get(); 
       getline (input_file, address); 

       //Get the Phone Number 
       input_file.get(); 
       getline (input_file, phone); 

       //Create a new student               
       Student * newStudent = new Student (id_number, name, address, phone); 

       //Add it to the map (referenced by the ID number) 
       student_map[id_number] = newStudent; 
      } 
     } 
    } 

    else { 
     return 0; 
    } 
    input_file.close(); 

    //Open the Grades file and load it in 
    cout << "Loading \"" << gradefile << "\"... " <<endl; 
    input_file.open(gradefile); 

    //Look for (id_number already defined): 
    string course; 
    string grade; 

    if (!input_file.eof()){ 

     while (input_file >> course >> id_number >> grade){ 
      //Find the student referenced 
      Student* current_student = student_map[id_number]; 
      //Calculate their grade points and add them to their grade point vector 
      current_student ->add_grade_points(current_student ->check_grade_points(grade)); 
     } 
    } 

    else { 
     return 0; 
    } 
    input_file.close(); 

    //Open the Query file and load it in 
    cout << "Loading \"" << queryfile << "\"... " << endl; 
    input_file.open(queryfile); 

    if (!input_file.eof()){ 

     //Write to 
     ofstream output_file ("report.txt"); 

     //No need to "Look for" anything, id_number alread defined 

     //If the file is open, write to it 
     if (output_file.is_open()) 
     { 
      while (input_file >> id_number) 
      { 
       //Print the ID Number (With four spaces) 
       output_file << id_number << " "; 

       //Print out the GPA (With four spaces) 
       Student* current_student = student_map[id_number]; 
       output_file << current_student->get_gpa() << " "; 

       //Print out the student's name (Then end that line) 
       output_file << current_student->get_name() << endl; 
      } 
     } 
     input_file.close(); 
     output_file.close(); 
    } 

    else { 
     return 0; 
    } 


    cout << endl; 
    cout << "File Printed."; 

    return 0; 
} 

はここに私の "学生" クラス(ヘッダファイル)です:

#pragma once 
#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 
#include <fstream> 
#include <map> 

using namespace std; 

class Student 
{ 
public: 
    Student (string _id_number, string _name, string _address, string _phone); 

    void add_grade_points (double grade_points); 
    double check_grade_points (string grade); 

    double get_gpa() const; 
    string get_name() const; 

private: 
    string id_number; 
    string name; 
    string address; 
    string phone; 

    vector <double> grade_points; 
}; 

そして最後に、私のクラスの機能:

#include "Student.h" 

Student::Student (string _id_number, string _name, string _address, string _phone){ 
    id_number = _id_number; 
    name = _name; 
    address = _address; 
    phone = _phone; 
} 

void Student::add_grade_points (double new_grade_point){ 
    grade_points.push_back(new_grade_point); 
} 

double Student::check_grade_points (string grade) { 
    if (grade == "A") 
     return 4.0; 
    else if (grade == "A-") 
     return 3.7; 
    else if (grade == "B+") 
     return 3.4; 
    else if (grade == "B") 
     return 3.0; 
    else if (grade == "B-") 
     return 2.7; 
    else if (grade == "C+") 
     return 2.4; 
    else if (grade == "C") 
     return 2.0; 
    else if (grade == "C-") 
     return 1.7; 
    else if (grade == "D+") 
     return 1.4; 
    else if (grade == "D") 
     return 1.0; 
    else if (grade == "D-") 
     return 0.7; 
    else 
     return 0.0; 
} 

double Student::get_gpa() const{ 
    //Add up all of the grade points 
    double total = 0; 

    for (int i = 0; i < grade_points.size(); i++) { 
     total = total + grade_points[i]; 
    } 

    //Calculate the Grade Point Average 
    double gpa = total/grade_points.size(); 

    return gpa; 
} 

string Student::get_name() const{ 
    return name; 
} 

すべてのヘルプとても素晴らしいだろう!

EDIT:

1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol [email protected] referenced in function ___tmainCRTStartup 
1>C:\Users\Student\Documents\Visual Studio 2010\Projects\CS235 Project 1\Debug\CS235 Project 1.exe : fatal error LNK1120: 1 unresolved externals 
+6

未解決の機能はどれですか?言い換えれば、**あなたが得る正確なエラーメッセージ**は何ですか? –

+2

私はWindowsのGUIアプリケーションとしてコンソールアプリケーションをコンパイルしようとしていると思います。 Linkerは 'main'ではなく' WinMain'を期待しています。プロジェクト設定を確認してください。 – lapk

答えて

7

は、WindowsのGUIアプリケーションとして()のint main()の標準のエントリ・ポイントを使用してプログラムをコンパイルしようとしている、とWindowsのGUIアプリケーションでは、代わりに

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) 

が必要です。

あなたのエントリーポイントをこれに変更することは、コンパイルしようとしている(Visual Studioの世界では何が行われているのか)「コンソールプログラム」には役立ちません。プロジェクトをセットアップすると、コンソールプロジェクトを作成するオプションがあります。あなたはあなたが持っているものをスクラップし、簡単な修正のためにあなたのコードを貼り付ける新しいコンソールプロジェクトを作成することができます。あなたが現在のプロジェクトに本当に付いている場合は、修正することができます。

プロジェクト設定: 構成プロパティ> C/C++>プリプロセッサで、_WINDOWS;を変更します。プリプロセッサ定義の文字列を_CONSOLEに設定します。 で、[設定のプロパティ]> [リンカ]> [システム]で、サブシステムをコンソールに変更します。

2

リンカエラーです:ここまで来るエラーメッセージがあります。関数を呼び出すと、プログラムをリンクするときにリンカーはリンクする関数を見つけることができません。

おそらくライブラリまたはソースファイルがありません。