Cannot run palindrome program

Apr 11, 2014 at 5:12am
I'm trying to compile a piece of code from a c++ book as an example, but I can't figure why it won't compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "stack.h"
using namespace std;
using namespace stack;

int main()
{
  Stack s;
  char next;

  cout << "Enter a word: ";
  cin.get(next);
  while (next != '\n')
	{
		s.push(next);
		cin.get(next);
	}

	cout << "Written backward that is: ";
	while ( ! s.empty() )
		cout << s.pop();
	cout << endl;

	system ("pause");
	return 0;
  }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef STACK_H
#define STACK_H
namespace stack
{
	struct StackFrame
	{
		char data;
		StackFrame *link;
	};

	typedef StackFrame* StackFramePtr;

	class Stack
	{
	public:
		Stack();
		Stack(const Stack& a_stack);
		~Stack();
		void push (char the_symbol);
		char pop();
		bool empty() const;

	private:
		StackFramePtr top;
	};
}
#endif 
Last edited on Apr 11, 2014 at 3:33pm
Apr 11, 2014 at 5:14am
Maybe the #include <stack> should be #include "Stack.h" ?
Apr 11, 2014 at 3:02pm
When I did, it still said fatal error LNK1120: 10 unresolved externals
Last edited on Apr 11, 2014 at 3:52pm
Apr 11, 2014 at 3:29pm
I suspect you're getting the include file for std::stack rather than your Stack class. When I compiled this without using a separate header, I got no errors.

#include <file> generally means to look in the compiler's include directory first, while #include "file" generally means to look in the users directory first. Try changing the angle brackets to quotes.
If that doesn't work, change the name of the header file to something that doesn't conflict (e.g mystack.h).

p.s. When saying something doesn't compile, please post the exact error messages.
Last edited on Apr 11, 2014 at 3:31pm
Apr 11, 2014 at 4:38pm
Shouldn't there be a Stack.cpp or something that contains the implementation of the Stack class?
Topic archived. No new replies allowed.