Getting a template error for header file

Main function seems to not connect with header file

Page 1 of 1

11 Replies - 1114 Views - Last Post: 26 December 2009 - 06:13 AM Rate Topic: -----

#1 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Getting a template error for header file

Post icon  Posted 25 December 2009 - 03:14 PM

I am getting an error when I try compile my main function.
As per my understanding the compiler is not able to find the header file even though it is in the same folder as the main function. Please some one help me resolving this error:


//HEADER FILE

#ifndef H_Stack
#define H_Stack

#include <iostream>
#include <stack>
#include <string>
#include <fstream>
using namespace std;

#define Empty -1

class Stack
{
public:
	   
	   
	   int head;
	   Stack <string> pile; //creates a stack
	  // Stack *pointer;//pointer to the stack
	   //*pointer = pile; //pointer now points to an object of Stack
	   string content, term,left_child, right_child;

	string ReadArith();
	void Create_stack(string);
	void remove();	 



//Implementation file


#include <iostream>
#include <fstream>
#include <stack>
#include "Stack.h"


string Stack::ReadArith()
{
	   
string filename;
string arith_step;

ifstream readFrom;

cout<<"Enter filename to open."<<endl;
cin>>filename;

readFrom.open(filename.c_str());
if(readFrom.fail())
{
	cout<<"File failed to open."<<endl;
	exit(1);
}

do
{
do
{

getline(readFrom,arith_step);
	cout<<arith_step<<endl;
}while(arith_step != "; ");
Create_stack(arith_step);
}while(!readFrom.eof());

remove();

return arith_step;
}

void Stack::Create_stack(string Arith_step)
{
	 
	 pile.push(Arith_step);
	 cout<<"stack size: "<<pile.size()<<endl; //checks to see if items are being pushed correctly on stack

}

void Stack::remove()
{
	 
}



//Main function

#include <iostream>
#include <fstream>
#include "Stack.h"

int main()
{
	
	Stack  x;
	
	x.ReadArith();
	
	system("Pause");
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Getting a template error for header file

#2 Guest_c.user*


Reputation:

Re: Getting a template error for header file

Posted 25 December 2009 - 03:42 PM

if the first file is Stack.h where is a close figure bracket with a semicolon after it ?

how do you compile it (which system, which compiler) ?

This post has been edited by c.user: 25 December 2009 - 03:48 PM

Was This Post Helpful? 0

#3 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Getting a template error for header file

Posted 25 December 2009 - 04:01 PM

View Postc.user, on 25 Dec, 2009 - 02:42 PM, said:

if the first file is Stack.h where is a close figure bracket with a semicolon after it ?

how do you compile it (which system, which compiler) ?


I have a closing bracket ( } ;) on the actual file. I just left it by mistake while copying pasting it here.
I also have end of header file indicator (#endif) on the actual file.

As per compiler I was compiling using Bloodshed Dev C++ 4.9.9.2

The program had worked earlier (about 2 or so weeks ago)

This post has been edited by jingoria: 25 December 2009 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Getting a template error for header file

Posted 25 December 2009 - 04:04 PM

What's the actual error? I'd prefer not to copy/paste since it may not be recreating your exact environment.
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Getting a template error for header file

Posted 25 December 2009 - 04:24 PM

Follow KYA's excellent advice and post the actual error.

The most common cause of such errors is that you have not given exactly the right location and name name for the file.
You have assured us you have the location correct so let's look at names.
You have said in your include statement that the file is called "Stack.h".
If the file name is actually "stack.h" or "Stack.h " (note the trailing whitespace) or <some other variant> then it is not an exact match and it may not be found.
Are you absolutely sure you have an exact match to the file name?

Also
You said you have a closing brace at the end of your class definition (in the .h file).
But you didn't say whether you have a closing semicolon as c.user asked.
Do you have that semicolon in place?
Was This Post Helpful? 0
  • +
  • -

#6 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Getting a template error for header file

Posted 25 December 2009 - 04:43 PM

View Postjanotte, on 25 Dec, 2009 - 03:24 PM, said:

Follow KYA's excellent advice and post the actual error.

The most common cause of such errors is that you have not given exactly the right location and name name for the file.
You have assured us you have the location correct so let's look at names.
You have said in your include statement that the file is called "Stack.h".
If the file name is actually "stack.h" or "Stack.h " (note the trailing whitespace) or <some other variant> then it is not an exact match and it may not be found.
Are you absolutely sure you have an exact match to the file name?

Also
You said you have a closing brace at the end of your class definition (in the .h file).
But you didn't say whether you have a closing semicolon as c.user asked.
Do you have that semicolon in place?


For clarity I have attached the print screen of my header and main file with the error screen also.
Please take a look. I hope that will help.

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 Guest_c.user*


Reputation:

Re: Getting a template error for header file

Posted 25 December 2009 - 04:56 PM

you used a template syntax, the header inclusion is right

Attached image(s)

  • Attached Image

This post has been edited by c.user: 25 December 2009 - 05:00 PM

Was This Post Helpful? 0

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Getting a template error for header file

Posted 25 December 2009 - 05:00 PM

Look at what you posted.

The first error is cut off and unreadable.
Does posting part of the error help?
Perhaps it might be better to paste the whole error?
What do you think?

What do you think this line does?
Stack <string> pile;
Talk us through it.

Why post code as a picture?
Are the code tags not a better option?
What do you think?

This post has been edited by janotte: 25 December 2009 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_c.user*


Reputation:

Re: Getting a template error for header file

Posted 25 December 2009 - 05:20 PM

janotte said:

Why post code as a picture?
Are the code tags not a better option?
What do you think?

the picture shows an unique place in his code
he will know where is it exactly in one look

this is a reaction on

jingoria said:

As per my understanding the compiler is not able to find the header file even though it is in the same folder

he incorrectly diagnosed a problem so he could be at so knowledge level, where the picture will be rather
Was This Post Helpful? 0

#10 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Getting a template error for header file

Posted 25 December 2009 - 05:28 PM

View Postjanotte, on 25 Dec, 2009 - 04:00 PM, said:

Look at what you posted.

The first error is cut off and unreadable.
Does posting part of the error help?
Perhaps it might be better to paste the whole error?
What do you think?

What do you think this line does?
Stack <string> pile;
Talk us through it.

Why post code as a picture?
Are the code tags not a better option?
What do you think?


Well I was reading through my book and the only way to implement a stack showed in the book was using a template. I thought including a stack class in the header would let me use the stack template, but I guess I was wrong.
In the above statement (Stack <string> pile) pile is an object of type stack (right?).
I guess my question now is how would I use the push function in my class. That is how would I push an element to top of the stack. (I cannot just simply do a pile.push()).

Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Getting a template error for header file

Posted 26 December 2009 - 01:04 AM

You are in the right ballpark but you need to do a bit more to make templates work.

Have a read here:
http://www.cplusplus...rial/templates/
Was This Post Helpful? 0
  • +
  • -

#12 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Getting a template error for header file

Posted 26 December 2009 - 06:13 AM

C++ is case-sensitive. if you're trying to use the STL stack in your Stack class, then you will need to use stack<string> (Notice the lowercase 's')

This post has been edited by Bench: 26 December 2009 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1