push & pop in class definition

Why did the Linker force me to put function inside my header file

Page 1 of 1

6 Replies - 4410 Views - Last Post: 22 March 2007 - 01:30 PM Rate Topic: -----

#1 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

push & pop in class definition

Posted 12 March 2007 - 02:11 PM

For no particular reason I just wrote my own stack template, neat little thing that I did to get some experience in areas that I either have never worked, or have done little work. One of the neat features I added was the ability to push and pop using the << and >> operators. All that aside:


When I went to test the class it compiled fine but then the linker choked with an "undefined external identifier" error on my push and pop routines... Since the "undefined external identifier" error is usually do to scope a went took them out of my .cpp file and put them inside the header file (technically inside the class declaration). To my surprise this fixed the error. None of my other functions need to be in there, just those two.

Question is why?

Here is how they were defined inside the .cpp file:
template <class T>
T ADTStack<T>::pop()
{
	T ret; 
	if (iStackPointer != -1)
	{
		ret=TheStack[iStackPointer--];
	}
	return ret;
}

template <class T>
bool ADTStack<T>::push()
{
	bool ret = false;
	if (iStackPointer + 1 < iStackSize)
	{
		TheStack[++iStackPointer] = oItem;
		ret = true;
	}
	return ret;
}



I ask this because this is only my second or third template class and I wonder if I did something wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: push & pop in class definition

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: push & pop in class definition

Posted 12 March 2007 - 04:34 PM

Good question - do you mind if I ask what compiler you were using? I'm not near one at the moment, so I can't really test...

I have never defined by methods from a Template class in a separate cpp file (although I do with other classes) - but now that I think about it, it's probably due to something I was told, so I'm not sure that I ever experimented with it. It was my understanding that template member functions should be defined and declared in the same file, as per section 3.2.2 here:

http://www.cs.fiu.ed...msvc_stl_3.html
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: push & pop in class definition

Posted 13 March 2007 - 09:26 AM

Although the article makes sence to me, and makes a good case why the functions declarations must be with their definitions... however the compiler (VC++6 in this case) allows other functions to be defined in another file, just required those two.

Thinking back on it though I believe that I too was told (once upon a time) that template classes should be implemented in one file. I even thought of this I was writing the program. I did it this way thinking that it would be nice to have the file for future projects. Shows a fundamental misunderstanding of templates... the compiler CAN'T generate the actual code until it knows WHAT class/datatype will be used.

What do you know, I learned something new.

Thank you very much for the link.
Was This Post Helpful? 0
  • +
  • -

#4 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: push & pop in class definition

Posted 21 March 2007 - 10:37 PM

Are all the components there?

Are you using
#ifndef ADTSTACK_H
#define ADTSTACK_H
....
#endif
in the header file?

are you using
#include "adtstack.h"
in the cpp file?

and are you using a make file?

If you are not using a make file, you will need a pretty fancy set of commands to compile this correctly.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: push & pop in class definition

Posted 21 March 2007 - 11:19 PM

well no, I was not using a make file. Yes I did use a #ifndef-#endif structure to encapsulate my header to prevent duplicates. I really did not have a problem compiling once I moved the push and pop routine definitions into the class declaration. As for a "fancy set of commands to compile this correctly" I only used the standard commands use for any multi-file project.

No, all the components are not there. That was only a snippet of the file. I just did not, and to a certain extent still do not, understand why these two functions gave me trouble.

Although things are working as it is now, I think I may have to put the rest of the functions into the header file as well. I suspect that as I use more and more of them I will find that when I try to use them, if they are not is the defined inside the class declaration, they will generate the same errors.

I wonder why you say that it would take a fancy commands to compile? Perhaps it takes fancy commands to get it to link properly.
Was This Post Helpful? 0
  • +
  • -

#6 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: push & pop in class definition

Posted 22 March 2007 - 01:12 PM

I was wrong about needing a make file or fancy compiling command. You just need to add the include stack.cpp to the bottom of the stack.h file in order that the cpp file determine the type at compile time.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: push & pop in class definition

Posted 22 March 2007 - 01:30 PM

lol... I didn't think about that. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1