注册 登录
编程论坛 C++教室

帮我看下这个程序。说链接错误。

songhuirong1 发布于 2010-12-28 16:54, 900 次点击
这是Array.H头文件:
#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
using namespace std;

template <typename elemType>
class Array
{
public:
    Array(elemType *, int);
    ~Array();

    elemType &operator [](int) const;
    void display() const;
    friend ostream &operator <<(ostream &, const Array &);
private:
    int _size;
    elemType *pArray;
};

template <typename elemType>
inline Array<elemType>::Array(elemType *elem, int size):
_size(size)
{
    pArray = new elemType[_size];

    for(int ix=0;ix<_size;++ix)
    {
        pArray[ix] = elem[ix];
    }
}

template <typename elemType>
inline Array<elemType>::~Array()
{
    delete [] pArray;
}

template <typename elemType>
inline elemType &Array<elemType>::operator [](int offset) const
{
    if(offset >= _size)
    {
        cout << "数组越界!" << endl;

        exit(-1);
    }
    else
        return pArray[offset];
}

template <typename elemType>
inline void Array<elemType>::display() const
{
    for(int ix=0;ix<_size;++ix)
        cout << pArray[ix] << " ";

    cout << endl;
}

#endif

这是Array.cpp源文件:
#include "Array.h"
#include <iostream>
using namespace std;

template <typename elemType>
ostream &operator <<(ostream &os, const Array<elemType> &theArray)
{
    for(int ix=0;ix<_size;++ix)
        os << theArray.pArray[ix] << " ";

    return os;
}

int main()
{
    int ia[] = {12,25,62,74,29,38,41,82};
    Array<int> intArray(ia,sizeof(ia)/sizeof(ia[0]));
    intArray.display();

    cout << intArray << endl;
   
    return 0;
}

但是运行程序,说链接错误,请大侠帮忙看看。
12 回复
#2
lintaoyn2010-12-28 17:45
程序代码:
template <typename elemType>
ostream &operator <<(ostream &os, const Array<elemType> &theArray)
{
    for(int ix=0;ix<theArray._size;++ix)//指出_size
        os << theArray.pArray[ix] << " ";

    return os;
}
#3
songhuirong12010-12-28 19:19
回复 楼主 songhuirong1
我试了下,还是一样的错误。
#4
lintaoyn2010-12-28 19:48
贴上编译器报的错误。
#5
songhuirong12010-12-28 20:29
错误    1    error LNK2001: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Array<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Array@H@@@Z)    Array.obj    CP10
错误    2    fatal error LNK1120: 1 个无法解析的外部命令    F:\Program Files\source\CP10\Debug\CP10.exe    CP10
#6
songhuirong12010-12-28 20:29
回复 4楼 lintaoyn
错误    1    error LNK2001: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Array<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Array@H@@@Z)    Array.obj    CP10
错误    2    fatal error LNK1120: 1 个无法解析的外部命令    F:\Program Files\source\CP10\Debug\CP10.exe    CP10
#7
lintaoyn2010-12-28 21:44
程序代码:
template<typename T>class Array;
template<typename T>ostream &operator <<(ostream &, const Array<T>&);
template <typename elemType>
class Array
{
public:
    Array(elemType *, int);
    ~Array();

    elemType &operator [](int) const;
    void display() const;
    friend ostream& operator << <elemType>(ostream& ,const Array<elemType>&);
private:
    int _size;
    elemType *pArray;
};
再改下这个。
在DEV-C++中能够通过编译。
#8
missiyou2010-12-29 10:21
去掉! cout << intArray << endl;

ostream 不能设别这个数组!
#9
missiyou2010-12-29 10:24
class std::basic_ostream<char,struct std::char_traits<char>  这是的std 使用traits 技术! 在traits 里面没有你自己定义的类型!
想看地址的话

cout << &intArray << endl;
#10
songhuirong12010-12-29 11:59
8楼和9楼的,你们都没明白我的意思,我是想重载<<运算符,然后实现直接打印数组对象。
#11
songhuirong12010-12-29 13:02
回复 6楼 songhuirong1
我解决了。太感谢你了。为什么要这么改呢?friend ostream &operator <<<elemType>(ostream &, const Array &);这是声明,ostream &operator <<(ostream &os, const Array<elemType> &theArray)这是定义。我自己又改了下,我如果把定义改成这样就不行,ostream &operator << <elemType>(ostream &os, const Array<elemType> &theArray),编译就报错,这是为什么呀?声明中有<elemType>,但是定义中没有<elemType>?
#12
lintaoyn2010-12-29 17:38
模板也有形参实参的区分。
#13
songhuirong12010-12-30 19:14
回复 2楼 lintaoyn
您一定是C++界的高手了。我现在是一名企业的网络管理员,但是我发现我对C++非常感兴趣,想将来从事C++开发。我现在是C++初学者,不知道这条路怎么走呀,能指点下小弟吗。我现在在看C++ Primer第三版这本书,我想找C++开发方面的工作,那该怎么走能让我尽快找到这方面的工作呢?等进入C++领域,然后我慢慢来研究C++。请高手指点下。
1