| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 552 人关注过本帖
标题:[求助]新人,刚刚学习有问题想请教
只看楼主 加入收藏
ichigo
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2007-5-27
收藏
 问题点数:0 回复次数:5 
[求助]新人,刚刚学习有问题想请教

今天按照书上的程序输入进去可是怎么修改也无法运行,不知道哪里出错了,谁能帮一下,谢谢了

#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 时出错.

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

搜索更多相关主题的帖子: 新人 学习 
2007-05-27 20:02
孤魂居士
Rank: 2
来 自:老A(中国地大)
等 级:论坛游民
威 望:4
帖 子:1142
专家分:18
注 册:2007-5-21
收藏
得分:0 

新手就起步到这地步``
.................
我也是新手  帮不 了你哦

准备用3年做个高级软件工程师 10年也做不成。准备用10年做成高级软件工程师 3年就成了QQ 群 45771086
欢迎版主...欢迎JAVA爱好者...
一起从深夜 到凌晨...
2007-05-28 21:41
yushui
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:1355
专家分:22
注 册:2006-7-19
收藏
得分:0 

#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; }
friend ostream& operator<< (ostream&, Array<T>&);
private:
T *pType;
int itsSize;
};
template <class T>
// 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;
}
还有警告 呵呵


fighting!from now on!
2007-05-28 23:50
ichigo
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2007-5-27
收藏
得分:0 
还是不行...别人说用vc8直接能译过去,没有错误,可是vc8我下了一个安装不了。..总是下载完成就出错...
2007-05-29 09:48
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
没看你代码哈!

我用vc++2005给你编译了下,没有错误,可以运行。

3楼的有错误。

Fight  to win  or  die...
2007-05-29 10:32
ichigo
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2007-5-27
收藏
得分:0 
那能给我发个2005的安装程序吗...我想用那个...
2007-05-29 11:00
快速回复:[求助]新人,刚刚学习有问题想请教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033435 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved