菜鸟上路

时间:2008-05-10 08:59:51   来源:论坛整理  作者:  编辑:chinaitzhe
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <iostream.h>

#include <windows.h>

#include <string.h>

class boy

{

    private:

            char *st;

    public:

            boy(char *p)

            {

                st=new char[strlen(p) 1];

                strcpy(st,p);

            }

            void print()

            {

                cout<<st<<endl;

            }

            ~boy()

            {

                delete st;

            }

};

class girl

{

    private:

            boy name;

            int age;

    public:

        girl(char *p,int a):name(p)

        {

            age=a;

        }

        void print()

        {

            cout<<name.print();

            cout<<age<<endl;

        }

        ~girl()

        {

        }

};

void main()

{

    girl girl001("272",20);

    girl001.print();

}




为什么,他老是报这个错,是因为什么? error C2679: binary ' < <' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
网友回复:name.print()的返回值为空

网友回复:void print()
{
cout < <st < <endl;
}
==================
int print()
{
cout < <st < <endl;
return ;
}
网友回复:他是 void print() 没有返回值呀
网友回复:问题依旧
网友回复:cout < <name.print();

这一句,没有重载 < <运算符,不能这么写
网友回复:那应该怎么写?
网友回复:cout要求必须有值
所以你name.print()就行了
网友回复:void print()
{
cout < <name.print();
cout < <age < <endl;
}
==========
void print()
{
name.print();
cout < <age < <endl;
}

网友回复:书上,怎么是这样写的

void print()
{
cout < <"姓名:" < <name.print();
cout < <"年龄:" < <age < <endl;
}
网友回复:为什么书上会那样写呢? 是书上写错了么?
网友回复:#include <iostream.h>
#include <windows.h>
#include <string.h>
class boy
{
private:
char *st;
public:
boy(char *p)
{
st=new char[strlen(p) 1];
strcpy(st,p);
}
void print()
{
cout < <*st < <endl;
}
~boy()
{
delete st;
}
};
class girl
{
private:
boy name;
int age;
public:
girl(char *p,int a):name(p)
{
age=a;
}
void print()
{
name.boy::print();
cout < <age < <endl;
}
~girl()
{
}
};
void main()
{
girl girl001("272",20);
girl001.print();
}


网友回复:上面有点小错误,改正后如下:

#include <iostream.h>
#include <windows.h>
#include <string.h>
class boy
{
private:
char *st;
public:
boy(char *p)
{
st=new char[strlen(p) 1];
strcpy(st,p);
}
void print()
{
cout < <st < <endl;
}
~boy()
{
delete st;
}
};
class girl
{
private:
boy name;
int age;
public:
girl(char *p,int a):name(p)
{
age=a;
}
void print()
{
name.boy::print();
cout < <age < <endl;
}
~girl()
{
}
};
void main()
{
girl girl001("272",20);
girl001.print();
}

网友回复:void print()
{
cout < <name.print();
cout < <age < <endl;
}
这里是很明显的错误。。 cout < <name.print();应该改为name.print();
网友回复:这个程序的错误不管,既然st=new char[strlen(p) 1]; 那么请delete[] st;
关键字:菜鸟,上路,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面