Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 22, 2011, 02:24 AM   #1
chunhohuen
macrumors newbie
 
Join Date: Apr 2011
C++ new hand problem 2 please help (about library)

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;
}
chunhohuen is offline   0 Reply With Quote
Old Apr 22, 2011, 05:10 AM   #2
SidBala
macrumors 6502a
 
Join Date: Jun 2010
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 login::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;
}
}
SidBala is offline   0 Reply With Quote
Old Apr 22, 2011, 07:32 AM   #3
Sander
macrumors 6502
 
Join Date: Apr 2008
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.
Sander is offline   0 Reply With Quote
Old Apr 22, 2011, 08:05 AM   #4
chunhohuen
Thread Starter
macrumors newbie
 
Join Date: Apr 2011
Quote:
Originally Posted by Sander View Post
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.
Quote:
Originally Posted by SidBala View Post
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 login::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 !

Last edited by chunhohuen; Apr 22, 2011 at 09:34 AM.
chunhohuen is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iTunes Library problem! Help please! Mr. RPG Mac Applications and Mac App Store 2 May 26, 2014 06:53 PM
Second hand Macbook aluminum ML problem LimeLizard MacBook 7 Apr 27, 2013 12:25 PM
General: Photo Library Problem! Zmanbaseball2 Jailbreaks and iOS Hacks 0 Mar 18, 2013 04:27 AM

Forum Jump

All times are GMT -5. The time now is 06:09 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC