Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chunhohuen

macrumors newbie
Original poster
Apr 18, 2011
11
0
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;
}
 

SidBala

macrumors 6502a
Jun 27, 2010
533
0
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;
}
}
 

Sander

macrumors 6502a
Apr 24, 2008
520
67
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.
 

chunhohuen

macrumors newbie
Original poster
Apr 18, 2011
11
0
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.

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;
}
}


Thank you very much !;)!
It can work now :p!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.