I got some linker error, couldnt find the mistake. What is wrong?

#include "myStack.h" //Imports the Stack header file containing the declaration of the member functions

myStack::myStack()
/*The stack is initialized as empty */
{
	count=0;
} //end of Stack::Stack constructor

Error_code myStack::push(const Stack_entry &item)
/*If the stack is full then the error code overflow is returned
else the item is pushed to the top of the stack and return error code success */
{
	Error_code outcome=success; //default value of outcome is set to success

	if(count>=maxstack) //the overflow condition is tested
		outcome=overflow;
	
	else //the success condition will be implemented
		entry[count++]=item;

	return outcome; //returns the Error_code type outcome

} //end of Stack::push function

Error_code myStack::pop()
/*If the stack is empty then return error code underflow and do nothing
else if the stack is not emtpy then pop the top of the stack */
{
	Error_code outcome=success; //the default value of outcome is set to success

	if(count==0) //stack is empty case is handled
		outcome=underflow;

	else //stack is not empty case is handled and pop is executed
		count--;

	return outcome;

} //end of Stack::pop function

Error_code myStack::top(Stack_entry &item) const
/*If the stack is empty then an error of underflow is returned.
Else if the stack is not empty then the top of the stack is assigned to the item */
{
	Error_code outcome=success; //the default value of outcome is set to success

	if(count==0) //the stack if empty case is handled and returned the underflow error code
		outcome=underflow;

	else /*the stack is not empty case is handled and the value of the top of the stack is assigned
		 to the parameter item */

		item=entry[count-1]; //count-1 represents the top of the stack

	return outcome;

} //end of Stack::top function

bool myStack::empty() const
/* If the stack is empty then the function returns true
else if the stack is not empty then the function returns false */
{
	bool outcome=false; //the default value of returning value outcome is set to false
	
	if(count==0) //the case that the stack is empty is handled
		outcome=true;

	return outcome; //outcome is returned

} //end of Stack::empty function
#ifndef STACK_H
#define STACK_H

typedef char Stack_entry;
enum Error_code{success,underflow,overflow};


/*Manual implementation of Stack class.
  By this way it will be possible to learn the advantages of Stack 
  data structure over others. This is the header file of class Stack */

const int maxstack=10; //A small value to be tested as a limit of the stack

class myStack
{
public:
	myStack(); //Constructor
	bool empty() const; //constant function returns true if the stack is empty otherwise returns false
	
	Error_code pop(); /*Function tries to pop the value from the stack if the stack is not empty
					  and returns Error_code*/

	Error_code push(const Stack_entry &item); /*function tries to push the value of constant item to 
											  the top of the stack and returns Error_code*/

	Error_code top(Stack_entry &item) const; /*function tries to copy the value of the top of the
											 stack into the item and returns Error_code */

private:
	int count; // counts the number of values existing in the stack

	Stack_entry entry[maxstack]; //the decleration of the stack that will hold the values.
};

#endif
#include <iostream>
using namespace std;

#include "myStack.h"

int balanced( );


int main()
{
	int state; //true=1 false=0
	state = balanced(); //function that will decide if the brackets are balanced returns 1 or 0
	
	return 0 ;
}

int balanced( )
{
	char chr ; //character that will be tested one by one
	char match ; //character that will be tested against the closing bracket as opening bracket
	myStack stack ; //stack that will hold the brackets

	chr = cin.get() ; //gets the first character
	while ( (int) chr != 10 ) //continue if character is not empty
	{
		if ( chr == '(' || chr == '{' ) //if opening brackets then add stack
		{
			 stack.push( chr ) ;
		}
		else if ( chr == ')' || chr == '}' ) //if closing brackets then look for the match in the stack
		{
			if ( stack.empty() == true )
			{
				cout << "Stack is empty. Brackets not matched!" << endl ;
				return 0 ;
			}
			else
			{
				stack.top ( match ) ;
				if( match == chr )
					stack.pop() ;
				else
				{
					cout << "The brackets did not match!" << endl ;
					return 0 ;
				}
			}
		}
		chr = cin.get() ; //update the character
	}

	return 1 ;
}
1>------ Build started: Project: Manual Stack Implementation, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: enum Error_code __thiscall myStack::top(char &)const " (?top@myStack@@QBE?AW4Error_code@@AAD@Z) referenced in function "int __cdecl balanced(void)" (?balanced@@YAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: enum Error_code __thiscall myStack::push(char const &)" (?push@myStack@@QAE?AW4Error_code@@ABD@Z) referenced in function "int __cdecl balanced(void)" (?balanced@@YAHXZ)
1>C:\Users\Meltabi\Documents\Visual Studio 2008\Projects\Manual Stack Implementation\Debug\Manual Stack Implementation.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\Users\Meltabi\Documents\Visual Studio 2008\Projects\Manual Stack Implementation\Manual Stack Implementation\Debug\BuildLog.htm"
1>Manual Stack Implementation - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What command are you using to compile/link? What are the names of those files posted above? Is this the absolutely smallest code that will demonstrate your problem?

Dave

I am using VS 2008. The error happens when the balanced() function calls stack.push and stack.top. I dont know what is the problem so i posted the whole code.

If the error happens at runtime (as you say "when the balanced() function calls stack.push"), then it is not a linker error. But the errors you posted ARE linker errors. I don't use visual studio, so unfortunately I can't tell you how to tell it that you want to link to all of the object files generated by all of your source files. Maybe someone else can.

Good luck.
Dave

I mean if you comment the push and top there are no errors. I said they are linker errors in thread name.

Thanks anyway.

1>------ Build started: Project: Manual Stack Implementation, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
[B]1>stack.cpp[/B]
1>Linking...

Have you ever seen it compile stack.cpp ?

If not, you need to add it to the project.

commented: solved. +9
commented: Salem FTW! +2

myStack:top and myStack:push take a Stack_entry argument, not a char which you are calling them with. Given that Stack_entry is typedefed to char that links fine in my VS2005 environment, but VS2008 may have tightened up on that. Try calling those methods with true Stack_entry types.

@Salem
Stack.cpp is included in the myStack.h header, but i tried including it in the main too, then it gave me the error "already included".

@MrSpigot

I tried what you said too but the errors remained.

Thank you both for your replies.

What Salem meant was, have you added mystack.cpp to the project tree in your solution? I'm with Salem here, I strongly suspect you have not!

In Visual Studio, take a look at the solution explorer window and see what files are listed in there. You should have myStack.h listed under "Header Files" and main.cpp and myStack.cpp listed under "Source Files".

If you don't see mystack.cpp listed, right-click the "Source Files" folder in the solution explorer and select "add->existing item", then navigate to the location of mystack.cpp, select the file and hit ok. That will add mystack.cpp to your solution.

Also if you haven't added myStack.h to the list of header files in the project tree of the solution explorer either, you should add that too by right clicking on the "Header Files" folder, select "add->existing item", navigate to the location of the header, select the header file and click OK and that file will also be added to your solution.

Once you've correctly added all of the files to the solution, try re-compiling your project and it should compile without any further linker errors.

Basically the problem was the compiler found the myStack header file, but it didn't know where to look for the actual implementation, i.e. the myStack.cpp file. And it was this that caused the linker errors you were getting.

Cheers for now,
Jas.

@JasonHippy ;
Thanks for your comment. I have the Stack.cpp in the solution tree but your comment made me find my mistake X)

The mistake was that I did not add "myStack::" to the data members names in the Stack.cpp funcitons. Now i added "myStack::" to the begining of the data members and it works !

Thank you all .

Edit: Nice drum kit .

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.