I can't get rid this error


IntelliSense: cannot open source file "stdafx.h"

error C1083: Cannot open include file: 'stdafx.h': No such file or directory

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;
//class BinarySearch Tree

class BinarySearchTree
{
private:
	struct BST
	{
		BST* left;
		BST* right;
		int data;
	};
	BST* root;
	//constructor
public:
	BinarySearchTree()
	{
		root =NULL;
	}
	//prototypes
	bool isEmpty() const {return root == NULL;}
	void print_inorder();
	void inorder(BST*);
	void insert(int);
	void search(int);
};

void BinarySearchTree :: search(int d)
{
	bool found = false;
	if(isEmpty())
	{
		cout << "This tree is empty!" << endl;
		return;
	}
	BST* curr;
	BST* parent;
	curr = root;
	while (curr != NULL)
	{
		if(curr -> data ==d)
		{
			found  = true;
			break;
		}
		else
		{
			parent = curr;
			if(d> curr -> data) curr = curr -> right;
			else curr = curr -> left;
		}
	}
	if (!found)
	{
		cout <<"Data not found! " << endl;
	}
	else 
		cout << "Element " << d << " is found " << endl;
}
//place small values in left and larger values in right side
//larger elements go right
void BinarySearchTree :: insert(int d)
{
	BST* t = new BST;
	BST* parent;
	t-> data = d;
	t->left = NULL;
	t->right = NULL;
	//is this a new tree?
	if(isEmpty()) root = t;
	else
	{
		//Note : all insertions are as leaf noods
		BST* curr;
		curr = root;
		//Find the Node's parent
		while(curr)
		{
			parent = curr;
			if(t-> data > curr ->data) curr = curr ->right;
			else curr = curr ->left;
		}
		if(t -> data < parent -> data)
			parent -> left = t;
		else
			parent -> right = t;
		cout << "Element " << d << " is inserted into tree " << endl;
	}
}
//to display inorder
void BinarySearchTree :: inorder(BST* p)
{
	if( p != NULL)
	{
	if(p->left) inorder (p ->left);
	cout << " " << p -> data << "  ";
	if(p->right) inorder (p -> right);
	}
	else return;
}

//main function to drivem the above class
int main ()
{
	BinarySearchTree bst;
	int ch, insert, element;
	while(1)
	{
		//ask user to enter choice
		cout << endl << endl;
		cout << "Simple Binary Search Tree Operations" << endl;
		cout << "------------------------------------ " << endl;
		cout << "1. insertion / Creation " << endl;
		cout << "2. Search " << endl;
		cout << "3. In-Order Traversal" << endl;
		cout << "4. Exit" << endl;
		cout << "Enter your choice: ";
		cin >> ch;
		//based on the chioce control the flow of execution.
		switch (ch)
		{
		case 1 : cout << "Enter  Number to be inserted: ";
			cin >> insert;
			bst.insert(insert);
			break;
		case 3: cout << endl;
			cout << "In-Order Traversal" << endl;
			cout << "--------------------" << endl;
			bst.print_inorder();
			break;
		case 2: cout << endl;
			cout << "Enter Number to be search " << endl;
			cin >> element;
			bst.search(element);
			break;
		case 4: system("pause");
			return 0;
			break;
		}//end of switch
	}//end of while
}//endl of main

You should be able to get rid of it and still have compile. I believe stdafx.h is used to make non console based win32 programs.

I've never actually used the stdafx.h pre-compiled header personally; however, i did a wikipedia search for this header and found some information:

Common Implementations

stdafx.h

stdafx.h is a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever change.

Compatible compilers (for example, Visual C++ 6.0 and newer) will pre-compile this file to reduce overall compile times. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.

The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft Foundation Classes (MFC). Optionally, Visual Studio projects may avoid pre-compiled headers, as well as they may specify an alternative name (though stdafx.h is used by default).

In my opinion, I believe your code will work fine without "stdafx.h"

Error 1 error LNK2019: unresolved external symbol "public: void __thiscall BinarySearchTree::print_inorder(void)" (?print_inorder@BinarySearchTree@@QAEXXZ) referenced in function _main

Error 2 error LNK1120: 1 unresolved externals


Still getting these errors

stdafx.h is a Windows Visual Studio compiler header. Are you building this in a Windows environment?

no in a console in visual studio 2010

no in a console in visual studio 2010

That's a Windows environment, unless Visual Studio was made portable across platforms without my knowledge. :icon_rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.