C++ new hand problem 2 please help (about library)

Discussion in 'Mac Programming' started by chunhohuen, Apr 22, 2011.

  1. macrumors newbie

    Joined:
    Apr 18, 2011
    #1
    HI everyone, First I want to thanks all the help in here to solve my previous problem:)! It mains a lot to me!;)

    Now, I would like to use the preivous program to build a static library and try to test it.

    When I finish build the library and apply it for test, there are run times error occured:

    1>fortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall login::find(void)" (?find@login@@QAEXXZ) referenced in function _main

    There are four of them and I know they are the same things.

    I do not know how to fix them....:(


    The code:

    The header file is :

    #include <fstream>
    #include <string>

    class login
    {
    public:
    void setuser();

    void write();

    void read();

    void find();

    private:
    char inputusername[99];
    };

    The code file is :


    void find()

    {
    ifstream file;
    file.open("file.txt");

    string input=inputusername;
    string input_line;
    bool found = false;

    while( file >> input_line ) {
    if( input_line.compare(input) == 0 ) {
    cout << "The name " << input_line << " was found in file.\n";
    found = true;
    break;
    }
    }

    if( found == false ) {
    ofstream fout ("file.txt",ios::app);
    fout<<inputusername<<endl;
    fout.close();
    }

    file.close();

    }

    void setuser() //function of set the username
    {
    cout<<"Enter the username: "<<endl;
    cin>>inputusername;
    int score=0;
    }


    void read() //read the file and show the details
    {
    cout<<endl;
    ifstream fin("file.txt");
    char ch;
    while (fin.get(ch))
    cout<<ch;
    fin.close();
    }



    void write() //read the input to the file
    {
    ofstream fout ("file.txt",ios::app);
    fout<<inputusername<<endl;
    fout.close();
    }


    And the main test cpp file is:

    #include <iostream>
    #include "logininhead.h"
    using namespace std;


    int main()
    {
    login open;

    int choice=0, number=0;

    bool entered=false;
    do {
    cout <<endl;
    cout << "Please choose an option below:\n\n";
    cout << "1. input\n";
    cout << "2. write\n";
    cout << "3. read\n";
    cout << "4. find\n";
    cout << "5. Quit\n";
    cin >> choice;


    switch(choice) {
    case (1) : {open.setuser();break;}


    case (2) : {open.write(); break; }


    case (3) : {open.read(); break; }


    case (4) : {open.find(); break; }


    case (5) : {break; }


    }
    }
    while(choice != 5);

    return 0;
    }
     
  2. macrumors 6502a

    Joined:
    Jun 27, 2010
    #2
    Where you define the code, you need to put in classname::functionname

    Do this for every function you define. This need not be done if you are within the class declaration.

    Code:
    void [SIZE="5"][B]login::[/B][/SIZE]find()
    {
    ifstream file;
    file.open("file.txt");
    
    string input=inputusername;
    string input_line;
    bool found = false;
    
    while( file >> input_line ) {
    if( input_line.compare(input) == 0 ) {
    cout << "The name " << input_line << " was found in file.\n";
    found = true;
    break;
    }
    }
     
  3. macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    I'm surprised this compiles (i.e. that you only get errors when linking). The line

    Code:
    string input=inputusername;
    
    must have thrown an error at you, since "inputusername" is not visible to functions outside the class.
     
  4. chunhohuen, Apr 22, 2011
    Last edited: Apr 22, 2011

    thread starter macrumors newbie

    Joined:
    Apr 18, 2011
    #4

    Thank you very much !;)!
    It can work now :p!
     

Share This Page