2010-04-27 169 views
0
//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; 
} 

我编译时出现以下错误:

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 &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected][email protected]@@Z) referenced in function _main 
+0

你能从'主提供的代码()'(或者'_main()'),其中函数被调用? – 2010-04-27 13:12:49

+0

main() { cStack sobj(1); } – Suri 2010-04-27 13:15:50

+0

对不起,这里是我的主 主 { cStack sobj(1); std :: cout << sobj; – Suri 2010-04-27 13:19:50

回答

0

它编译,它只是不链接。 (我知道,这不是一个很大的帮助。)你没有一个地方正在实例化函数模板。你有一个main()的地方试图使用这个流插入操作符吗?

+0

main { cStack sobj(1); std :: cout << sobj; } – Suri 2010-04-27 13:19:32

2

我认为链接器找不到你的函数。将所有代码放在头文件中。

+0

它在头文件 – Suri 2010-04-27 13:17:57

+0

这是一个包含cStack定义的头文件吗?可能您需要向我们展示整个.cpp文件,其中包含您的'main' – SergGr 2010-04-27 13:23:08

+0

#include“cstack.h” main() {cStack sobj(1); \t \t std :: cout << sobj; } – Suri 2010-04-27 13:27:21

6

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

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

标记功能模板功能

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

,编译器会很高兴。 并将该函数定义放在类定义之前。

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> &); 
}; 
+2

更多信息:http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16(我宁愿随着最后的建议去。) – UncleBens 2010-04-27 13:49:56

1

[剪断]

在Windows下面的工作与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; 
}