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

大家帮我看看这个程序。拜托!

songhuirong1 发布于 2010-11-16 17:14, 446 次点击
#include <iostream>

using namespace std;

class Animal
{
private:
    int itsWeight;
public:
    Animal();
    Animal(int);
    ~Animal();

    int GetWeight() const
    {
        return itsWeight;
    }
    void Display() const
    {
        cout << itsWeight;
    }
};

Animal::Animal():
itsWeight(1)
{}

Animal::Animal(int weight):
itsWeight(weight)
{}

Animal::~Animal() {}

template <class T>
class Array
{
private:
    int itsSize;
    T * pType;
public:
    Array(int);
    Array(const Array &);
    ~Array();

    int GetSize() const
    {
        return itsSize;
    }
    T * GetType() const
    {
        return pType;
    }

    Array & operator =(const Array &) const;
    T & operator [](int);
    const T & operator [](int) const;

    friend ostream & operator <<(ostream &,Array<T> &);
};

template <class T>
Array<T>::Array(int size):
itsSize(size)
{
    pType = new T[itsSize];

    for(int i=0;i<itsSize;i++)
        pType[i] = 0;
}

template <class T>
Array<T>::Array(const Array & rhs)
{
    itsSize = rhs.GetSize();
    pType = new T[itsSize];

    for(int i=0;i<itsSize;i++)
        pType[i] = rhs[i];
}

template <class T>
Array<T>::~Array()
{
    delete [] pType;
    pType = 0;

    itsSize = 0;
}

template <class T>
Array<T> & Array<T>::operator =(const Array & rhs) const
{
    if(this == &rhs)
        return * this;

    delete [] pType;
    pType = 0;

    itsSize = rhs.GetSize();
    pType = new T[itsSize];

    for(int i=0;i<itsSize;i++)
        pType[i] = rhs[i];
}

template <class T>
T & Array<T>::operator [](int offset)
{
    if(offset > itsSize)
        return pType[itsSize-1];
    else
        return pType[offset];
}

template <class T>
const T & Array<T>::operator [](int offset) const
{
    if(offset > itsSize)
        return pType[itsSize-1];
    else
        return pType[offset];
}

template <class T>
ostream & operator <<(ostream & output,Array<T> & theArray)
{
    for(int i=0;i<theArray.GetSize();i++)
    {
        output << theArray[i];
    }

    return output;
}

int main()
{
    Array<int> theArray(10);
    int offset,value;
    bool Stop = false;

    while(!Stop)
    {
        cout << "Please input the offset(0-9) and the value:";
        cin >> offset >> value;

        if(offset < 0)
            break;

        if(offset > 9)
        {
            cout << "The offset must be in 0 to 9!" << endl;
            continue;
        }
        theArray[offset] = value;
    }

    cout << "Here is theArray:" << endl;
    cout << theArray << endl;
   
    return 0;
}


我是用vs2008编译的,编译能通过,但是运行却出现了错误。
错误    1    error LNK2019: 无法解析的外部符号 "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> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Array@H@@@Z),该符号在函数 _main 中被引用    ex19-4.obj    Example 19
错误    2    fatal error LNK1120: 1 个无法解析的外部命令    F:\Program Files\source\Example 19\Debug\Example 19.exe    Example 19
5 回复
#2
cccvvv1231232010-11-16 17:26
我没有仔细地看,但是从你给出的错误提示和你编写的上面的语句中,感觉是你写入的程序中有一条语句或分支程序与其冲突造成的!
#3
xiaohui4202010-11-16 19:56
1在你的class Array之前没有对你的friend友元进行声明(注意,声明之前也要声明Array)
2在你的friend ostream & operator <<(ostream &,Array<T> &);中,operator<<后面没有指定<T>
#4
songhuirong12010-11-17 08:29
那具体该怎么改呀?本人初学C++,菜鸟一个,不懂呀。
#5
玩出来的代码2010-11-17 18:04
template<typename T1>
friend ostream & operator <<(ostream &,Array<T1> &);
#6
songhuirong12010-11-18 12:52
成功运行了。哈哈。谢谢lz。
1