首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

!刚刚开始学习有个有关问题请帮忙

2012-02-07 
求助!刚刚开始学习有个问题请帮忙今天按照书上的程序输入进去可是怎么修改也无法运行,不知道哪里出错了,谁

求助!刚刚开始学习有个问题请帮忙
今天按照书上的程序输入进去可是怎么修改也无法运行,不知道哪里出错了,谁能帮一下,谢谢了

#include   <iostream>
using   namespace   std;

const   int   DefaultSize   =   10;

class   Animal
{
public:
Animal(int);
Animal();
~Animal()   {}
int   GetWeight()   const   {   return   itsWeight;   }
void   Display()   const   {   std::cout   < <   itsWeight;   }
private:
int   itsWeight;
};

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

Animal::Animal():
itsWeight(0)
{}

template   <class   T>
class   Array
{
public:
Array(int   itsSize   =   DefaultSize);
Array(const   Array   &rhs);
~Array()   {   delete   []   pType;   }

Array&   operator=(const   Array&);
T&   operator[](int   offSet)   {   return   pType[offSet];   }
const   T&   operator[](int   offSet)   const
{return   pType[offSet];   }
int   GetSize()   const   {   return   itsSize;   }
template   <class   T>
friend   ostream&   operator < <   (ostream&,   Array <T> &);
private:
T   *pType;
int   itsSize;
};

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

template   <class   T>
Array <T> ::Array(int   size):
itsSize(size)
{
pType   =   new   T[size];
for   (int   i=0;   i <size;   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 <T> ::operator=(const   Array   &rhs)
{
if   (this   ==   &rhs)
    return   *this;
delete   []   pType;
itsSize   =   rhs.GetSize();
pType   =   new   T[itsSize];
for   (int   i=0;   i <   itsSize;   i++)
    pType[i]   =   rhs[i];
return   *   this;
}

int   main()
{
bool   Stop   =   false;
int   offset,   value;
Array <int>   theArray;
while   (Stop   ==   false)
{
    cout   < <   "Enter   an   offset   (0-9)   ";
    cout   < <   "and   a   value.   (-1   to   stop):   ";
    cin   > >   offset   > >   value;

    if   (offset <0)
      break;


    if   (offset> 9)
    {
      cout   < <   "***Please   use   values   between     0   and   9.***\n ";
      continue;
    }

    theArray[offset]   =   value;
}

cout   < <   "\nHere 's   the   entire   array:\n ";
cout   < <   theArray   < <   endl;
char   response;
cin   > >   response;
return   0;
}


在108行处
就是cout   < <   theArray   < <   endl;这里编译器提示错误是
D:\C++\第三周\19.3.cpp(108)   :   error   C2563:   mismatch   in   formal   parameter   list
D:\C++\第三周\19.3.cpp(108)   :   error   C2568:   ' < < '   :   unable   to   resolve   function   overload
                could   be   'class   std::basic_ostream <unsigned   short,struct   std::char_traits <unsigned   short>   >   &__cdecl   std::endl(class   std::basic_ostream <unsigned   short,struct   std::char_traits <unsigned   short>   >   &) '
                d:\microsoft   visual   studio\vc98\include\ostream(377)   :   see   declaration   of   'endl '
                or               'class   std::basic_ostream <char,struct   std::char_traits <char>   >   &__cdecl   std::endl(class   std::basic_ostream <char,struct   std::char_traits <char>   >   &) '
                d:\microsoft   visual   studio\vc98\include\ostream(372)   :   see   declaration   of   'endl '
                or               'class   std::basic_ostream <_E,_Tr>   &__cdecl   std::endl(class   std::basic_ostream <_E,_Tr>   &) '
                d:\microsoft   visual   studio\vc98\include\ostream(367)   :   see   declaration   of   'endl '
执行   cl.exe   时出错.

这个问题解决不了下边的知识都有类似的地方...所以希望大家帮忙解决,非常感谢!!

[解决办法]
vc8 直接编译过去,没有错误
[解决办法]
vc 6 的模板语法支持不大好.
好像将
template <class T>
ostream& operator < < (ostream& output, Array <T> & theArray)
内联在类里可以.

[解决办法]
vc8,就是vs2005,很大的,好几个G
不如用devcpp,对标准支持好点,vc6对标准支持不好
[解决办法]
#include <iostream>
using namespace std;

const int DefaultSize = 10;

class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const { return itsWeight; }
void Display() const { std::cout < < itsWeight; }
private:
int itsWeight;
};

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


Animal::Animal():
itsWeight(0)
{}

template <class T>
class Array
{
public:
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array(){ delete [] pType; }

Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet)const
{return pType[offSet]; }
int GetSize() const { return itsSize; }
//template <class T>
friend ostream& operator < < (ostream& output, Array <T> & theArray)
{
for(int i=0; i <theArray.itsSize; i++)
output < < "[ " < < i < < "] " < < theArray[i] < < endl;
return output;
}
private:
T *pType;
int itsSize;
};
//
// template <class T>
// ostream& operator < < (ostream& output, Array <T> & theArray)
// {
// for(int i=0; i <theArray.itsSize; i++)
// output < < "[ " < < i < < "] " < < theArray[i] < < endl;
// return output;
// }

template <class T>
Array <T> ::Array(int size):
itsSize(size)
{
pType = new T[size];
for (int i=0; i <size; 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 <T> ::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete [] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i=0; i < itsSize; i++)
pType[i] = rhs[i];
return * this;
}

int main()
{
bool Stop = false;
int offset, value;
Array <int> theArray;
while (Stop == false)
{
cout < < "Enter an offset (0-9) ";
cout < < "and a value. (-1 to stop): ";
cin > > offset > > value;

if (offset <0)
break;
if (offset> 9)
{
cout < < "***Please use values between 0 and 9.***\n ";
continue;
}

theArray[offset] = value;
}

cout < < "\nHere 's the entire array:\n ";
cout < < theArray < < endl;
char response;
cin > > response;
return 0;
}

这样在vc6下就行了

热点排行