求妖孽大神求圈养解答

&& 查看话题
求大神帮忙解答!
困扰了蛮久了....求大神帮忙解答程序错在哪里....疑问都在程序旁边注释出来了。
新手贴,没什么金币,抱歉....今后努力赚金币。
非常感谢.....:arm:
源程序如下:
#include&iostream&
class Port
& & & & char*
& & & & Port(const char* br=&none&, const char* st=&none&, int b=0);
& & & & Port(const Port& p);
& & & & virtual ~Port() {delete&&}
& & & & Port& operator =(const Port& p);
& & & & Port& operator+=(int b);
& & & & Port& operator-=(int b);
& & & & int BottleCount() const {}
& & & & virtual void Show()
& & & & friend std::ostream& operator&& (std::ostream& os, const Port& p);
class VintagePort: public Port
& & & & char*
& & & & VintagePort();
& & & & VintagePort(const char* br, int b, const char* nn, int y);
& & & & VintagePort(const VintagePort& vp);
& & & & ~VintagePort() {delete&&}
& & & & VintagePort& operator= (const VintagePort& vp);
& & & & virtual void Show()
& & & & friend std::ostream& operator&& (std::ostream& os, const VintagePort& vp);
//实现文件
#include&iostream&
#include&cstring&
#include&Port.h&
Port::Port(const char* br, const char* st, int b)
& &strcpy(brand, br);
& &strncpy(style, st, 20);
& &bottles=b;
Port::Port(const Port& p)
& &strcpy(brand, p.brand);
& &strncpy(style, p.style, 20);
& &bottles=p.
Port& Port::operator = (const Port& p)
& &if (this==&p)
& & & && & return *
& &delete&&
& &strcpy(brand, p.brand);
& &strncpy(style, p.style, 20);
& &bottles=p.
& &return *
Port& Port::operator += (int b)
& &bottles+=b;
& &return *
Port& Port::operator -=(int b)
& &if (b&=bottles)
& & & && & bottles-=b;
& & & && & std::cout&& &You can't take more bottles than we have!\n&
& & & && && && && & && &Operation cancelled!\n&;
& &return *
void Port::Show ()const
& & & & std::cout&& &Brand: &&& brand
& & & & & & & && && &&& &\nKind: &&& style
& & & & & & & & & & & &&&&& &\nBottles: &&& bottles
& & & & & & & & & & & &&&&&std::
std::ostream& operator&& (std::ostream& os, const Port& p)
& &os && p.brand&&&& &, & && p.style && &, & && p.
VintagePort::VintagePort()
& &nickname=
& &strcpy(nickname, &none&);
& &year=0;
VintagePort::VintagePort(const char* br, int b, const char* nn, int y)
:Port(br, &vintage&, b)
& &nickname=
& &strcpy(nickname, nn) ;
& &year=y;
VintagePort::VintagePort(const VintagePort& vp)
& &nickname=
& &strcpy(nickname, vp.nickname);
& &year=vp.
VintagePort& VintagePort::operator = (const VintagePort& vp)
& &Port::operator = (vp);
& &delete&&
& &nickname=
& &strcpy(nickname, vp.nickname);
& &year=vp.
& &return *
void VintagePort::Show () const
& & & & Port::Show ();
& & & & std::cout&& &Nickname: & && nickname
& & & & & & & && && &&& &\nYear: &&&&& year
& & & & & & & & & & & &&&&& std::
std::ostream& operator&& (std::ostream& os, const VintagePort& vp)
& &os && (const Port&)& && && &
& &os && &, & && vp.nickname && &, & && vp.
#include&iostream&
#include&Port.h&
int main()
& & & & using std::
& & & & using std::
& & & & using std::
& & & & Port*
& & & & pointer=&Port(&Lafite&, &tawny&, 27);
& & & & pointer=&Port(&Latour&, &ruby&, 23);
& & & & pointer=&VintagePort(&Haut-Brion&, 35, &The Noble&, 1875);
& & & & pointer=&VintagePort(&Margaux&, 56, &Old Velvet&, 1898);
& & & & for (int k=0;k&4;k++)& && & //这个不是主程序的部分,我是想用来测试,我用基类指针调用Show()函数的结果,
& & & & & & & && && && && && && && && & //结果与我预想的不一样,我的Show()函数是虚拟的,我用基类指针(Port*)指向派类对象
& & & && & pointer-&Show ();& &&&//用pointer-&Show()的时候,应该是调用Port::Show(),这个调用是正确的,但是我的brand成员显示却出现了乱码,这个不明白是怎么回事。
& & & && & cout &&& && && && &//用pointer-&Show()的时候,应该是调用VintagePort::Show(),这个调用是错的,结果调用的是Port::Show(),而且与上面一样brand成员显示却出现了乱码。
& & & & Port& b=Port(&Lafite&, &tawny&, 27);& &//我这里用基类引用指向基类对象,用引用调用的时候,就不出现乱码,所以我觉得应该问题出现在指针上
& & & & b.Show ();& && && && && && && && && &&&//但是,实在想不到是为什么?&&求解答。
& & & & cout && b &&
& & & & Port& a=VintagePort(&Margaux&, 56, &Old Velvet&, 1898);
& & a.Show();& && && && && && &//还有这里,我用基类引用指向派生对象,在调用Show()的时候,就调用的是VintagePort::Show(),是正确的。
& & & & cout && a &&& && && & //但是这里又出现了问题,我重载的cout && a,应该需要显示nickname和year的,但是却与cout && b是一样的,是我的重载出现问题了么??
& & & & int choice,i=4;
& & & & cout && &Do you want to take or save port(s)? (1 to take/2 to save/q to quit)&;
& & & & while (cin && choice)
& & & && & if (choice==1)
& & & && & {
& & & && && & int kind=0,num=0;
& & & & & & & && &cout && &Which brand do you need?(1:Lafite, 2:Latour, 3:Haut-Brion, 4:Margaux)&;
& & & & & & & && &cin &&
& & & & & & & && &cin.get();
& & & & & & & && &switch (kind)
& & & & & & & && &{
& & & & & & & && &case 1:
& & & & & & & & & & & && &{
& & & & & & & & & & & && && &cout && &How many bottles do you need?\n&;
& & & & & & & & & & & & & & & &&&cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & &&&cin &&
& & & & & & & & & & & & & & & &&&*pointer-=
& & & & & & & & & & & & & & & &&&pointer-&Show();
& & & & & & & & & & & & & & & &&&
& & & & & & & & & & & && &}
& & & & & & & && &case 2:
& & & & & & & & & & & && &{
& & & & & & & & & & & && && &cout && &How many bottles do you need?\n&;
& & & & & & & & & & & & & & & &&&cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & &&&cin &&
& & & & & & & & & & & & & & & &&&*pointer-=
& & & & & & & & & & & & & & & &&&pointer-&Show();
& & & & & & & & & & & & & & & &&&
& & & & & & & & & & & && &}
& & & & & & & && &case 3:
& & & & & & & & & & & && &{
& & & & & & & & & & & && && &cout && &How many bottles do you need?&;
& & & & & & & & & & & & & & & &&&cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & &&&cin &&
& & & & & & & & & & & & & & & &&&(Port&)*pointer-=& & //或者直接用*pointer-=因为-=的函参为Port&,可以指向VintagePort。
& & & & & & & & & & & & & & & &&&pointer-&Show();
& & & & & & & & & & & & & & & &&&
& & & & & & & & & & & && &}
& & & & & & & && &case 4:
& & & & & & & & & & & && &{
& & & & & & & & & & & && && &cout && &How many bottles do you need?&;
& & & & & & & & & & & & & & & &&&cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & &&&cin &&
& & & & & & & & & & & & & & & &&&*pointer-=
& & & & & & & & & & & & & & & &&&pointer-&Show();
& & & & & & & & & & & & & & & &&&
& & & & & & & & & & & && &}
& & & & & & & && &default: cout && &That's not a choice!\n&;
& & & & & & & && &}
& & & && & }
& & & && & else
& & & && & {
& & & & & & & && &int num=0;
& & & && && & cout && &Please enter the brand:&;& & //这里我无法进行输入。
& & & & & & & && &
& & & & & & & && &cin.getline(temp, 50);
& & & & & & & && &cin.get();
& && && & if (temp==&Lafite&)& &&&//我这里的字符串的比较应该是有问题的,不能用==判断。
& & & & & & & && &{
& & & & & & & && && &cout && &Save bottles: &;
& & & & & & & & & & & &&&cin &&
& && && && & *pointer+=
& & & & & & & & & & & &&&pointer-&Show();
& & & & & & & && &}
& & & & & & & && &else if (temp==&Latour&)
& & & & & & & && &{
& & & & & & & && && &cout && &Save bottles: &;
& & & & & & & & & & & &&&cin &&
& && && && & *pointer+=
& & & & & & & & & & & &&&pointer-&Show();&&
& & & & & & & && &}
& & & & & & & && &else if (temp==&Haut-Brion&)
& & & & & & & && &{
& & & & & & & && && &cout && &Save bottles: &;
& & & & & & & & & & & &&&cin &&
& && && && & *pointer+=
& & & & & & & & & & & &&&pointer-&Show();
& & & & & & & && &}
& & & & & & & && &else if (temp==&Margaux&)
& & & & & & & && &{
& & & & & & & && && &cout && &Save bottles: &;
& & & & & & & & & & & &&&cin &&
& && && && & *pointer+=
& & & & & & & & & & & &&&pointer-&Show();
& & & & & & & && &}
& & & & & & & && &else
& & & & & & & && &{
& & & & & & & && && &cout && &Enter the style: &;
& & & & & & & & & & & &&&char s_
& & & & & & & & & & & &&&cin.getline(s_temp, 10);
& & & & & & & & & & & &&&if (s_temp==&vintage&)& && & //这里的判断也有问题。
& & & & & & & & & & & &&&{
& & & & & & & & & & & && &&&cout && &Enter the nickname: &;
& & & & & & & & & & & & & & & & char n_
& & & & & & & & & & & & & & & & cin.getline(n_temp, 20);
& & & & & & & & & & & & & & & & cout && &Enter the year: &;
& & & & & & & & & & & & & & & & int y_
& & & & & & & & & & & & & & & & cin && y_
& & & & & & & & & & & & & & & & cout && &Enter the bottles: &;
& & & & & & & & & & & & & & & & int b_
& & & & & & & & & & & & & & & & cin && b_
& & & & & & & & & & & & & & & & pointer=&VintagePort(temp, b_temp, n_temp, y_temp);
& & & & & & & & & & & &&&}
& & & & & & & & & & & &&&else
& & & & & & & & & & & &&&{
& & & & & & & & & & & & & & & & cout && &Enter the bottles: &;
& && && && && & int b_
& & & & & & & & & & & & & & & & cin && b_
& & & & & & & & & & & && & pointer=&Port(temp, s_temp, b_temp);
& & & & & & & & & & & &&&}
& & & & & & & && &}
& & & && & }
& & & && & ++i;
& & & && & cout && &Do you want to take or save port(s)? (1 to take/2 to save/q to quit)&;
& & & & cin.get();
& & & & cout && &Do you want to see all the Port's information?(y/n) &;&&//最后还有这里,无法进行输入....
& & & & cin.get(ch);
& & & & if (ch=='y'||ch=='Y')
& & & && & for (int c=0;c&i;++c)
& & & & & & & && & cout && *pointer &&
& & cout && &Bye!\n&;
& & & & return 0;& &
里面的符号有的变成了表情....汗....
& &但是直接复制的话,应该是可以变回来的吧!?:cry: #include&iostream&
class Port
& & & & char*
& & & & Port(const char* br = &none&, const char* st = &none&, int b = 0);
& & & & Port(const Port& p);
& & & & virtual ~Port() { }
& & & & Port& operator =(const Port& p);
& & & & Port& operator+=(int b);
& & & & Port& operator-=(int b);
& & & & int BottleCount() const { }
& & & & virtual void Show()
& & & & friend std::ostream& operator&& (std ::ostream& os, const Port& p);
class VintagePort : public Port
& & & & char*
& & & & VintagePort();
& & & & VintagePort(const char* br, int b, const char* nn, int y);
& & & & VintagePort(const VintagePort& vp);
& & & & ~VintagePort() { }
& & & & VintagePort& operator= (const VintagePort& vp);
& & & & virtual void Show()
& & & & friend std::ostream& operator&& (std::ostream& os, const VintagePort& vp);
//实现文件
#include&iostream&
#include&cstring&
#include&Port.h&
Port::Port(const char* br, const char* st, int b)
& & & & brand =
& & & & strcpy(brand, br);
& & & & strncpy(style, st, 20);
& & & & bottles =
Port::Port(const Port& p)
& & & & brand =
& & & & strcpy(brand, p.brand);
& & & & strncpy(style, p.style, 20);
& & & & bottles = p.
Port& Port::operator = (const Port& p)
& & & & if (this == &p)
& & & & & & & & return *
& & & & delete&&
& & & & brand =
& & & & strcpy(brand, p.brand);
& & & & strncpy(style, p.style, 20);
& & & & bottles = p.
& & & & return *
Port& Port::operator += (int b)
& & & & bottles +=
& & & & return *
Port& Port::operator -= (int b)
& & & & if (b &= bottles)
& & & & & & & & bottles -=
& & & & else
& & & & & & & & std::cout && &You can't take more bottles than we have!\n&
& & & & & & & & && &Operation cancelled!\n&;
& & & & return *
void Port::Show()const
& & & & std::cout && &Brand: & && brand
& & & & & & & & && &\nKind: & && style
& & & & & & & & && &\nBottles: & && bottles
& & & & & & & & && std::
std::ostream& operator&& (std::ostream& os, const Port& p)
& & & & os && p.brand && &, & && p.style && &, & && p.
VintagePort::VintagePort()
& & & & nickname =
& & & & strcpy(nickname, &none&);
& & & & year = 0;
VintagePort::VintagePort(const char* br, int b, const char* nn, int y)
:Port(br, &vintage&, b)
& & & & nickname =
& & & & strcpy(nickname, nn);
& & & & year =
VintagePort::VintagePort(const VintagePort& vp)
& & & & nickname =
& & & & strcpy(nickname, vp.nickname);
& & & & year = vp.
VintagePort& VintagePort::operator = (const VintagePort& vp)
& & & & Port::operator = (vp);
& & & & delete&&
& & & & nickname =
& & & & strcpy(nickname, vp.nickname);
& & & & year = vp.
& & & & return *
void VintagePort::Show() const
& & & & Port::Show();
& & & & std::cout && &Nickname: & && nickname
& & & & & & & & && &\nYear: & && year
& & & & & & & & && std::
std::ostream& operator&& (std::ostream& os, const VintagePort& vp)
& & & & os && (const Port&)
& & & & os && &, & && vp.nickname && &, & && vp.
#include&iostream&
#include&Port.h&
int main()
& & & & using std::
& & & & using std::
& & & & using std::
& & & & Port*
& & & & //pointer = &Port(&Lafite&, &tawny&, 27);
& & & & //pointer = &Port(&Latour&, &ruby&, 23);
& & & & //pointer = &VintagePort(&Haut-Brion&, 35, &The Noble&, 1875);
& & & & //pointer = &VintagePort(&Margaux&, 56, &Old Velvet&, 1898);
& & & & Port pa = Port(&Lafite&, &tawny&, 27);
& & & & Port pb = Port(&Latour&, &ruby&, 23);
& & & & VintagePort va = VintagePort(&Haut-Brion&, 35, &The Noble&, 1875);
& & & & VintagePort vb = VintagePort(&Margaux&, 56, &Old Velvet&, 1898);
& & & & pointer = & // 只能对左值取地址,像你那样取地址是非法的,其实Port的析构函数都已经调用
& & & & pointer = &
& & & & pointer = &
& & & & pointer = &
& & & & for (int k = 0; k&4; k++)
& & & & & & & & pointer-&Show();
& & & & & & & & cout &&
& & & & //Port& b = Port(&Lafite&, &tawny&, 27);
& & & & Port prefb = Port(&Lafite&, &tawny&, 27);
& & & & Port &b =
& & & & // 或者你可以直接用Port &&b = Port(&Lafite&, &tawny&, 27);
& & & & // 或者const Port &b = Port(&Lafite&, &tawny&, 27);
& & & & // 只有常量引用和右值引用可以绑定在一个临时变量上,你看到没给你报错不代表你的写法
& & & & // 就真的没有错误,只是微软的编译器没有支持标准而已
& & & & b.Show();
& & & & cout && b &&
& & & & Port&& a = VintagePort(&Margaux&, 56, &Old Velvet&, 1898);//同上
& & & & a.Show();
& & & & cout && a &&// 没明白你这怎么重载了,operator(ostream, const Port&)直接从非const到const转换的匹配,没有任何问题
& & & & int choice, i = 4;
& & & & cout && &Do you want to take or save port(s)? (1 to take/2 to save/q to quit)&;
& & & & while (cin && choice)
& & & & & & & & if (choice == 1)
& & & & & & & & {
& & & & & & & & & & & & int kind = 0, num = 0;
& & & & & & & & & & & & cout && &Which brand do you need?(1:Lafite, 2:Latour, 3:Haut-Brion, 4:Margaux)&;
& & & & & & & & & & & & cin &&
& & & & & & & & & & & & cin.get();
& & & & & & & & & & & & switch (kind)
& & & & & & & & & & & & {
& & & & & & & & & & & & case 1:
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & && &cout && &How many bottles do you need?\n&;
& & & & & & & & & & & & & & & & & & & && &cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & & & & & && &cin &&
& & & & & & & & & & & & & & & & & & & && &*pointer -=
& & & & & & & & & & & & & & & & & & & && &pointer-&Show();
& & & & & & & & & & & & & & & & & & & && &
& & & & & & & & & & & & }
& & & & & & & & & & & & case 2:
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & && &cout && &How many bottles do you need?\n&;
& & & & & & & & & & & & & & & & & & & && &cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & & & & & && &cin &&
& & & & & & & & & & & & & & & & & & & && &*pointer -=
& & & & & & & & & & & & & & & & & & & && &pointer-&Show();
& & & & & & & & & & & & & & & & & & & && &
& & & & & & & & & & & & }
& & & & & & & & & & & & case 3:
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & && &cout && &How many bottles do you need?&;
& & & & & & & & & & & & & & & & & & & && &cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & & & & & && &cin &&
& & & & & & & & & & & & & & & & & & & && &(Port*)pointer -=& & //或者直接用*pointer-=因为-=的函参为Port&,可以指向VintagePort。
& & & & & & & & & & & & & & & & & & & && &pointer-&Show();
& & & & & & & & & & & & & & & & & & & && &
& & & & & & & & & & & & }
& & & & & & & & & & & & case 4:
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & && &cout && &How many bottles do you need?&;
& & & & & & & & & & & & & & & & & & & && &cout && &There're & && pointer-&BottleCount() && & saved.\n&;
& & & & & & & & & & & & & & & & & & & && &cin &&
& & & & & & & & & & & & & & & & & & & && &*pointer -=
& & & & & & & & & & & & & & & & & & & && &pointer-&Show();
& & & & & & & & & & & & & & & & & & & && &
& & & & & & & & & & & & }
& & & & & & & & & & & & default: cout && &That's not a choice!\n&;
& & & & & & & & & & & & }
& & & & & & & & }
& & & & & & & & else
& & & & & & & & {
& & & & & & & & & & & & int num = 0;
& & & & & & & & & & & & cout && &Please enter the brand:&;// &&& & //这里我无法进行输入。
& & & & & & & & & & & &
& & & & & & & & & & & & cin.get();
& & & & & & & & & & & & cin.getline(temp, 50);// 你原始的做法是直接获取新的一行,当然得到的就是'\n',没有什么不能输入的
& & & & & & & & & & & & //cin.get();
& & & & & & & & & & & & if (strcmp(temp,&Lafite&) == 0)& &&&//我这里的字符串的比较应该是有问题的,不能用==判断。
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & cout && &Save bottles: &;
& & & & & & & & & & & & & & & & cin &&
& & & & & & & & & & & & & & & & *pointer +=
& & & & & & & & & & & & & & & & pointer-&Show();
& & & & & & & & & & & & }
& & & & & & & & & & & & else if (strcmp(temp,&Latour&) == 0)
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & cout && &Save bottles: &;
& & & & & & & & & & & & & & & & cin &&
& & & & & & & & & & & & & & & & *pointer +=
& & & & & & & & & & & & & & & & pointer-&Show();
& & & & & & & & & & & & }
& & & & & & & & & & & & else if ( strcmp(temp,&Haut-Brion&) == 0)
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & cout && &Save bottles: &;
& & & & & & & & & & & & & & & & cin &&
& & & & & & & & & & & & & & & & *pointer +=
& & & & & & & & & & & & & & & & pointer-&Show();
& & & & & & & & & & & & }
& & & & & & & & & & & & else if ( strcmp(temp,&Margaux&) == 0)
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & cout && &Save bottles: &;
& & & & & & & & & & & & & & & & cin &&
& & & & & & & & & & & & & & & & *pointer +=
& & & & & & & & & & & & & & & & pointer-&Show();
& & & & & & & & & & & & }
& & & & & & & & & & & & else
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & cout && &Enter the style: &;
& & & & & & & & & & & & & & & & char s_
& & & & & & & & & & & & & & & & cin.getline(s_temp, 10);
& & & & & & & & & & & & & & & & if ( strcmp(s_temp,&vintage&) == 0)& && & //这里的判断也有问题。
& & & & & & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & & cout && &Enter the nickname: &;
& & & & & & & & & & & & & & & & & & & & char n_
& & & & & & & & & & & & & & & & & & & & cin.getline(n_temp, 20);
& & & & & & & & & & & & & & & & & & & & cout && &Enter the year: &;
& & & & & & & & & & & & & & & & & & & & int y_
& & & & & & & & & & & & & & & & & & & & cin && y_
& & & & & & & & & & & & & & & & & & & & cout && &Enter the bottles: &;
& & & & & & & & & & & & & & & & & & & & int b_
& & & & & & & & & & & & & & & & & & & & cin && b_
& & & & & & & & & & & & & & & & & & & & pointer =&&new VintagePort(temp, b_temp, n_temp, y_temp);//不知道你想用哪一个pointer,暂时用5,自己改
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & else
& & & & & & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & & cout && &Enter the bottles: &;
& & & & & & & & & & & & & & & & & & & & int b_
& & & & & & & & & & & & & & & & & & & & cin && b_
& & & & & & & & & & & & & & & & & & & & pointer = new Port(temp, s_temp, b_temp);//不知道你想用哪一个pointer
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & }
& & & & & & & & }
& & & & & & & & ++i;
& & & & & & & & cout && &Do you want to take or save port(s)? (1 to take/2 to save/q to quit)&;
& & & & cin.get();
& & & & cout && &Do you want to see all the Port's information?(y/n) &;&&//最后还有这里,无法进行输入....
& & & & cin.get(ch);
& & & & if (ch == 'y' || ch == 'Y')
& & & & for (int c = 0; c&i; ++c)
& & & & & & & & cout && *pointer &&
& & & & cout && &Bye!\n&;
& & & & return 0;
} : Originally posted by 木叶清风 at
#include&iostream&
class Port
& & & & char*
& & & & Port(const char* br = &none&, const char* st = &none&, int b = 0);
& & & & Por ... 非常感谢,问题已经解决了绝大部分....
这里还有一些疑问,希望能得到解决:
1.如何发代码帖子,我的符号变成表情了,注意到您的代码外面穿了马甲:code,请问怎么做到的....我是新人,嘿嘿。
2.Port &&b = Port(&Lafite&, &tawny&, 27);& & 这里好像有错误,报错 引用无法赋值给引用.....
您说,只有常量引用和右值引用可以绑定在一个临时变量上,我写的是Port& b = Port(&Lafite&, &tawny&, 27);我把左值引用绑定在了临时变量上(请问,您说是不规范的,这里是为什么不规范?),const Port &b = Port(&Lafite&, &tawny&, 27);这个是把常量引用绑定在临时变量上,但是请问一下把右值引用绑定在临时变量上是什么例子?
3. pointer =&&new VintagePort(temp, b_temp, n_temp, y_temp);&&这里为什么要new一个VintagePort? 还是因为只能对左值进行取地址操作?如果仅仅是 Port(&Lafite&, &tawny&, 27);这里创建的是临时变量么??
4.& & & & cout && &Do you want to see all the Port's information?(y/n) &;&&//最后还是无法输入。
& & & & cin.get();//我在这里已经用这个把cin &&留下的\n给销毁了,但是下面一个读取还是不能工作。
& & & & cin.get(infor);
& & & & if (infor=='y'||infor=='Y')
& & & && & for (int c=0;c&i;++c)
& & & & & & & && & cout && *pointer &&
& & & & else
& && &&&cout && &Bye!\n&;
十分感谢。 : Originally posted by lhy at
非常感谢,问题已经解决了绝大部分....
这里还有一些疑问,希望能得到解决:
1.如何发代码帖子,我的符号变成表情了,注意到您的代码外面穿了马甲:code,请问怎么做到的....我是新人,嘿嘿。
2.Port && ... 呃...另外.....最后一个输出所有信息,我发觉似乎有问题...我用的是
&&*pointer ,这里所有的都应该调用的是&& (Port),所以我的VintagePort的信息估计会显示不完全...这里有没有办法能够解决??:P : Originally posted by lhy at
非常感谢,问题已经解决了绝大部分....
这里还有一些疑问,希望能得到解决:
1.如何发代码帖子,我的符号变成表情了,注意到您的代码外面穿了马甲:code,请问怎么做到的....我是新人,嘿嘿。
2.Port && ... 1 高级回复可以插入代码
2 右值引用需要c++11支持,编译的时候带上选项,vs都是直接编译就行了,要是没有这么高版本的编译器又不想用g++,那直接改成const Port&就行了;至于临时变量的问题我只能说好好的找本c++的书看看;
3 我只能说那确实是个临时变量,我代码里面没有写释放内存的部分,因为我不知道你到地是想用5还是多少,需要自己把这部分代码补上;
4 把quit的部分直接改成3或者其它数字,cin && choice, 你输入一个字母算什么呀;
5 把输出的部分写成虚函数,或者用RTTI查看是不是派生类,是的话进行转换再输出。 : Originally posted by 木叶清风 at
1 高级回复可以插入代码
2 右值引用需要c++11支持,编译的时候带上选项,vs都是直接编译就行了,要是没有这么高版本的编译器又不想用g++,那直接改成const Port&就行了;至于临时变量的问题我只能说 ... 嗯....我自己用程序测试了一下,这个是个临时变量,创建后马上调用了析构函数......
呃,我这里用cin && choice是想把cin的返回值当作判断条件,因为当我输入数字的时候cin返回1进入循环,输入非数字的时候就读取失败cin返回0,退出循环。& &我按着您的提示修改了程序,用break来跳出循环....程序确实是可以运行了,最后也可以进行输入了...但是我不明白用cin && choice来进行输入,为什么会影响到后面一个字符的输入,我觉得这里应该是最后一个输入读取了前方的q或者\n,所以直接判断为非y所以无法显示所有信息.....
然后,只有成员函数才可以写成虚函数,如果我这里写成虚函数了就只能有一个函参,这么写:virtual std::ostream& operator && (std::ostream & os) {os && *(ps:原来的&&没变,这里调用原来的友元)},所以我在输出的时候这么调用*pointer&& cout &&看起来蛮别扭,但是结果确实是正确的....请问,这个方法能不能改进使得用 cout && *opinter;来调用??
还有您说的RTTI自己还没有涉及到,但是我在想如果检查出是派生对象,把基类*pointer进行转换了输出,这个岂不是向下转换,理论上是不允许的,因为可能丢失数据,难道这里的可以么??
最后,我目前才学习C++不久,使用的教材是《C++ primer Plus(第五版)》,觉得还不错,即将把这本书看完,这里希望您能给点指导,想知道看完这本书后可以继续看看别的什么比较好的教材来深入学习....
非常感谢您不厌其烦的指导.... : Originally posted by lhy at
嗯....我自己用程序测试了一下,这个是个临时变量,创建后马上调用了析构函数......
呃,我这里用cin && choice是想把cin的返回值当作判断条件,因为当我输入数字的时候cin返回1进入循环,输入非数字的时候 ... 写虚函数,然后在重载的输出操作符中调用;
知道正确的类型情况下向下转换指针没有什么问题的,理论上怎么不会不允许;
好好的看&C++ Primer, 5th& 和新版的《C++ Programming Language》,看懂这两本对于语言特性来说足矣。再往后就是设计模式,专业库的使用这些提高的部分了。我还是那句话,一群人放着这两本最经典的不看,到处说不知道怎么学C++。任何语言都一样,要想学,先把经典看了再说。 : Originally posted by 木叶清风 at
写虚函数,然后在重载的输出操作符中调用;
知道正确的类型情况下向下转换指针没有什么问题的,理论上怎么不会不允许;
好好的看&C++ Primer, 5th& 和新版的《C++ Programming Language》,看懂这两本对于 ... Okay....谢谢指导...看来我滴学习方向还是对滴!&&呵呵:P
var cpro_id = 'u1216994';
欢迎监督和反馈:本帖内容由
提供,小木虫仅提供交流平台,不对该内容负责。欢迎协助我们监督管理,共同维护互联网健康,如果您对该内容有异议,请立即发邮件到
联系通知管理员,也可以通过QQ周知,我们的QQ号为:8835100
我们保证在1个工作日内给予处理和答复,谢谢您的监督。
小木虫,学术科研第一站,为中国学术科研研究提供免费动力
广告投放请联系QQ: &
违规贴举报删除请联系邮箱: 或者 QQ:8835100
Copyright &
eMuch.net, All Rights Reserved. 小木虫 版权所有

我要回帖

更多关于 求大神 的文章

 

随机推荐