Hello,

Iam getting a strange error and I dont know how to solve it. Can you help me?

Iam trying to make a dictionaire using BST.

// Task3.cpp : BST

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#define cabecalho "---------------------------------------------------------\nDICTIONAIRE\n\nEnglish to Portuguese\n---------------------------------------------------------\n"
#define color(y) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), y);

#define LEFT  1
#define RIGHT 2

struct node
{
	char word[20],meaning[100];
	node *left,*right;
};

/*
Prototypes
*/
node *maketree(char[],char[]);
node* treefromfile();
void filefromtree(node*);
void addword(node*,char[],char[]);
void seperateword(char[],char[],char[]);
void displayall(node*);
node* bsearch(node*,char[]);
void showmenu();
FILE *file_ptr;
void clean_screen();


/*
------------
Clear Screen & Main Menu
------------
*/

void clean_screen()
{
     system("cls");
     HANDLE h_stdout;
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     DWORD dwConSize;
     COORD coordScreen = { 0, 0 };    
     DWORD dwCharsWritten;
 
     h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
 
     GetConsoleScreenBufferInfo( h_stdout, &csbi );
     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 
     FillConsoleOutputCharacter( h_stdout, (TCHAR) ' ', dwConSize, coordScreen, &dwCharsWritten );
 
     SetConsoleCursorPosition( h_stdout, coordScreen );
}

int menu()
{
    color(6);
     puts(cabecalho);
     color(7);
     int op_menu;
	 printf("\n\n\t\t----------  MENU  ----------\n");
     printf("\tSelect one of this options:\n\n");
     printf("\t 1 - File.\n");
     printf("\t 2 - Search.\n");
     printf("\t 3 - Insert.\n");
	 printf("\t 4 - View.\n");
     printf("\t 0 - Exit\n\n");
     printf("Option?\n");
     printf(">");
     scanf_s("%d", &op_menu);
     printf("\n");
     return(op_menu);       
}

int main(int argc, _TCHAR* argv[])
{
	int op_menu,op_file;
	char word[20], meaning[100];
	node *temp;
	

	while(1)
	{
		op_menu=menu();

		switch(op_menu)
		{
		case 1: 
			printf("1. Open\n");
			printf("2. Save\n");
			scanf("%d",&op_file);

			if(op_file==1)
			{
				temp=treefromfile();
				if(temp==NULL)
				{
					printf("File does not exist or dictionary is empty.\n");
					system("pause");
				}
			
			} else if(op_file==2){

								filefromtree(temp);
								printf("BST saved to file\n");
								system("pause");
			
								} else { printf("Error, invalid option.\n");}
			break;
		case 2:
			/*Search Word*/
			if(temp==NULL)
			{
				printf("The dictionary is empty\n");

			}else{

					printf("Find meaning of: ");
					flushall();
					gets(word);

					node *t;
					t=bsearch(temp,word);
					if(t==NULL)
					{
						printf("Word not found.\n");
					} else {

							printf("%s : ",t->word);
							puts(t->meaning);
					
					       }
				}

			system("pause");
			break;
		case 3:
			/*Insert Word*/
			printf("Enter word: ");
			scanf("%s",&word);
			printf("Enter meaning: ");
			flushall();
			gets(meaning);

			if(temp==NULL)
				temp=maketree(word,meaning);
			else
				addword(temp,word,meaning);

			system("pause");
			break;
		case 4:

			if(temp==NULL)
			{
				printf("Dictionary is empty.\n");
			
			} else {

					displayall(temp);
			}

			system("pause");
			break;
		default:
			color(4);
            printf("** Error: Your option is invalid!\n\n");
            color(7);
            printf("Try again! Option only 1 to 4.\n\n");
            system("pause");
            clean_screen();
            break;
        case 0: 
			exit(1);
			break;
		
		
		
		}//switch
	
	}//while

	return 0;
}


/*
------------
Make tree from file
------------
*/

node* treefromfile()
{
	node *ptree=NULL;
	int flag=0;
	char word[20],meaning[100],str[120],*i;
	
	file_ptr=fopen("C:\dict.anu","r");
	
	if(file_ptr==NULL)
		ptree=NULL;
	else
	{
		while(!feof(file_ptr))
		{
			i=fgets(str,120,file_ptr);
	
			if(i==NULL)
				break;
	
			seperateword(str,word,meaning);
	
			if(flag==0)
			{
				ptree=maketree(word,meaning);
				flag=1;

			} else
				addword(ptree,word,meaning);
		}//while

	fclose(file_ptr);
	}
	
	
	return ptree;
}

/*
------------
Make tree
------------
*/

node* maketree(char w[],char m[])
{
	node *p;
	p=new node;
	strcpy(p->word,w);
	strcpy(p->meaning,m);
	p->left=NULL;
	p->right=NULL;
	return p;
}

/*
------------
Add word & Separate Word
------------
*/

void seperateword(char str[],char w[],char m[])
{
	int i,j;
 
	for(i=0;str[i]!=' ';i++)
		w[i]=str[i];
 
	w[i++]=NULL;	//Append the null and skip the space.

	
	for(j=0;str[i]!='\0';i++,j++)
	{
		m[j]=str[i];
	}
		m[j]=NULL;
}

void addword(node *tree,char word[],char meaning[])
{
	node *p,*q; 
	p=q=tree;
 
	while(strcmp(word,p->word)!=0 && p!=NULL)
	{
		q=p;
  
		if(strcmp(word,p->word)<0)
			p=p->left;
		else
			p=p->right;
	}

 
	if(strcmp(word,q->word)==0)
	{
		printf("This word already exists...");
		
	} else if(strcmp(word,q->word)<0)
			q->left=maketree(word,meaning);
			else
			q->right=maketree(word,meaning);
}


/*
----------
Binary Search
----------

*/

node* bsearch(node *tree,char word[])
{
	node *q;
	q=tree;
 
	while(q!=NULL)
	{
		//p=q;
		if(strcmp(word,q->word)<0)
			q=q->left;
		else if(strcmp(word,q->word)>0)
				q=q->right;
  
		if(strcmp(word,q->word)==0)
	
		break;
	}
 return q;
}

/*
------------
Display All
------------
*/

void displayall(node *tree)
{
	if(tree!=NULL)
	{
		displayall(tree->left);
		printf("%s : %s\n",tree->word,tree->meaning);
		displayall(tree->right);
	}
}

/*
------------
Save to file
------------
*/

void travandwrite(node *tree)
{
	if(tree!=NULL)
	{
		fprintf(file_ptr,"%s %s\n",tree->word,tree->meaning);
		travandwrite(tree->left);
		travandwrite(tree->right);
	}
 }

Error 11 error LNK2019: unresolved external symbol "void __cdecl filefromtree(struct node *)" (?filefromtree@@YAXPAUnode@@@Z) referenced in function _main C:\Users\rafzk\documents\visual studio 2010\Projects\Task3\Task3\Task3.obj Task3


Error 12 error LNK1120: 1 unresolved externals C:\Users\rafzk\documents\visual studio 2010\Projects\Task3\Debug\Task3.exe 1 1 Task3


This errors, is about what? Iam using the right libraries for this task.

The second error is just a reference back to the first one. Note that you have not implemented the filefromtree(struct node*) function. Since you are calling it, it has to be implemented, otherwise you get this error, "unresolved external symbol".

The second error is just a reference back to the first one. Note that you have not implemented the filefromtree(struct node*) function. Since you are calling it, it has to be implemented, otherwise you get this error, "unresolved external symbol".

Problem solved. It was in comment the code.

If we add:

void filefromtree(node *tree)
{
 void travandwrite(node*);
 file_ptr=fopen("C:\dict.anu","w");
 if(file_ptr==NULL)
 {
  printf("
Cannot open file for writing data...");
 }
 else //if(tree==NULL)
 {
  if(tree!=NULL)
  {
   travandwrite(tree);
  }
  fclose(file_ptr);  //Close the file anyway.
 }
}

Its all working! Thanks! I was looking and looking and when its obvious we think always the worse. Thanks!:)

Sometimes it is difficult to "see the forest for the trees"! :-) Glad you sorted this out. Occasionally, the "obvious" is not so...

This article has been dead for over six months. Start a new discussion instead.