2010-04-27 108 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; 
}