views:

239

answers:

4
//cstack.h
# ifndef _STACK_H__
#define _STACK_H__

#include<iostream>
#include<vector>

template< class Type ,int Size = 3>
class cStack
{
 Type *m_array;
 int m_Top;
 int m_Size;

public:
    cStack();
    cStack(const Type&);
    cStack(const cStack<Type,Size> &);
    int GetTop()const;
    bool Is_Full()const;
    bool Is_Empty()const;
    void InsertValue(const Type&);
    void RemeoveValue();
    void show();  
    ~cStack();
    friend std::ostream& operator <<(std::ostream &, const cStack<Type,Size> &);
};

// iam writing only one function defination because linking is because of this function
template< class Type,int Size >
std::ostream& operator << ( std::ostream &os, const cStack<Type,Size> &s)
{
 for( int i=0; i<=s.GetTop();i++)
 {
  os << s.m_array[i];
 }
 return os;
}

 

//main.cpp
#include "cStack.h"
#include <string>
#include<iostream>

int main()
{
 cStack<int> sobj(1);
 std::cout << sobj;
}

When I compile I get the following error:

error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class cStack<int,3> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$cStack@H$02@@@Z) referenced in function _main
A: 

It's compiling, it's just not linking. (I know, that's not a big help.) You don't have a place where the function template is being instantiated. Do you have a main() somewhere that tries to use this stream insertion operator?

jwismar
main{cStack<int> sobj(1);std::cout << sobj;}
+2  A: 

I think the linker can't find your function. Place all the code in the the header file.

Charles Beattie
its in header file
Is this the same header as the one containing cStack definition? Probably you need to show us whole .cpp that contains your `main`
iPhone beginner
#include"cstack.h"main(){cStack<int> sobj(1); std::cout << sobj;}
Does "cstack.h" contains both class definition and template method implementation? Does it help if you move your template method implementation just above your `main` in cpp file? Are you sure that code in the question is your actual code (no typos)?
iPhone beginner
+5  A: 

35.16 Why do I get linker errors when I use template friends?

friend std::ostream& operator<< (std::ostream &, const cStack<Type, Size> &);

Mark the function as template functions

friend std::ostream& operator<< <>(std::ostream &, const cStack<Type, Size> &);

And the compiler will be happy. And put the function definition before the class definition.

template< class Type ,int Size>
class cStack;

template< class Type ,int Size >
std::ostream& operator <<(std::ostream &os, const cStack<Type,Size> &s)
{
    for( int i=0; i<=s.GetTop();i++)
    {
        os << s.m_array[i];
    }
    return os;
}


template< class Type ,int Size = 3>
class cStack
{
    Type *m_array;
    int m_Top;
    int m_Size;
public:
    cStack() {}
     //...
    friend std::ostream& operator<< <>(std::ostream &, const cStack<Type, Size> &);
};
TimW
More info: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16 (I'd rather go with the suggestion at the end.)
UncleBens
A: 

[snip]

The following works on windows with Visual Studio 2005

template <class Type, int Size = 3 >
class cStack 
{
    // ....

    template<class Type>
    friend std::ostream& operator<< (std::ostream & os, const cStack<Type> &s);
};

template<class Type>
std::ostream& operator << (std::ostream &os, const cStack<Type> &s)
{
    for( int i=0; i < s.GetTop();i++)
    {
        os << s.m_array[i];
    }
    return os;
}
toby