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

谁能帮我改改,好多错误==

Orphan 发布于 2015-06-05 15:07, 1079 次点击
#include<iostream>
using namespace std;
class date
{protected:
        int year;
    int month;
    int day;
 public:
    date(inty,intm,int d)
    {
        year=y;
        month=m;
        day=d;
   
    }
    void display()
    {
    cout<<year<<"/"<<month<<"/"<<day<<endl;
    }

};
class person
{protected:
    string name;
 public:
    person(string n)
    {
        name=n;
    }
    void display()
    {
        cout<<name<<endl;
    }

};
class student : public person
{protected:
    date=birthday;
public:
    student(date d,string n):person(n)
    {
    birthday=d;
    }
    void display()
    {
    cout<<name;
    birthday.display();
    }

};
int main()
{
        int y,m,d;
    cin>>y>>m>>d;
    date d(y,m,d);
    d.display();
    person p("xiaoming",d):student s(d,"xiaoming");
    s.display();
    return 0;
}


//--------------------Configuration: ksd - Win32 Debug--------------------
Compiling...
dzwee.cpp
E:\\ksd\dzwee.cpp(32) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,structstd::char_traits<char>,class std::allocator<char>>' (or there is no acceptable conversion)
E:\\ksd\dzwee.cpp(38) : error C2059: syntax error : '='
E:\\ksd\dzwee.cpp(38) : error C2238: unexpected token(s) preceding ';'
E:\\ksd\dzwee.cpp(55) : error C2371: 'd' : redefinition; different basic types
        E:\\ksd\dzwee.cpp(53) : see declaration of 'd'
E:\\ksd\dzwee.cpp(56) : error C2228: left of '.display' must have class/struct/union type
E:\\ksd\dzwee.cpp(57) : error C2143: syntax error : missing ';' before ':'
E:\\ksd\dzwee.cpp(58) : error C2065: 's' : undeclared identifier
E:\\ksd\dzwee.cpp(58) : error C2228: left of '.display' must have class/struct/union type
执行 cl.exe 时出错.

dzwee.obj - 1 error(s), 0 warning(s)
13 回复
#2
wp2319572015-06-05 15:12
这都是神马  date(inty,intm,int d)
#3
林月儿2015-06-05 15:38
改代码容易,改习惯难啊!
程序代码:
#include<iostream>
using namespace std;
class date{
protected:
    int year;
    int month;
    int day;
public:
    date(){
        year=2015;
        month=6;
        day=1;
    }
    date(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    }
    void display(){
        cout<<year<<"/"<<month<<"/"<<day<<endl;
    }

};
class person{
protected:
    string name;
public:
    person(string n){
        name=n;
    }
    void display(){
        cout<<name<<endl;
    }
};
class student : public person{
protected:
    date birthday;
public:
    student(date d,string n):person(n){
        birthday=d;
    }
    void display(){
      cout<<name;
      birthday.display();
    }
};
int main(){
    int y,m,d;
    cout<<"year:";
    cin>>y;
    cout<<"month:";
    cin>>m;
    cout<<"day:";
    cin>>d;
    date dt(y,m,d);
    dt.display();
    person p("xiaoming");
    student s(dt,"xiaoming");
    p.display();
    s.display();
    return 0;
}
#4
rjsp2015-06-05 15:42
无数的低级错误。
楼主,我猜你从来没上过课或看过书

程序代码:
#include <iostream>
#include <string>

class date
{
public:
    date( unsigned year, unsigned month, unsigned day ) : year_(year), month_(month), day_(day)
    {
    }
protected:
    unsigned year_, month_, day_;

    friend std::ostream& operator<< ( std::ostream& os, const date& d );
};

std::ostream& operator<< ( std::ostream& os, const date& d )
{
    return os << d.year_ << '/' << d.month_ << '/' << d.day_;
}

class person
{
public:
    person( const char* name ) : name_(name)
    {
    }
    virtual ~person()
    {
    }
protected:
    std::string name_;

    friend std::ostream& operator<< ( std::ostream& os, const person& p );
};

std::ostream& operator<< ( std::ostream& os, const person& p )
{
    return os << p.name_;
}

class student : public person
{
public:
    student( const date& birthday, const char* name ) : person(name), birthday_(birthday)
    {
    }
protected:
    date birthday_;

    friend std::ostream& operator<< ( std::ostream& os, const student& s );
};

std::ostream& operator<< ( std::ostream& os, const student& s )
{
    return os << static_cast<const person&>(s) << '\n' << s.birthday_;
}

using namespace std;

int main( void )
{
    unsigned y, m, d;
    cin >> y >> m >> d;

    date birthday( y, m, d );
    cout << birthday << endl;

    student s( birthday, "xiaoming" );
    cout << s << endl;

    return 0;
}

#5
诸葛欧阳2015-06-05 18:18
楼主要学会看编译信息,不懂的单词要查一查,时间长了就能根据编译信息改正大部分错误
#6
yangfrancis2015-06-05 23:00
楼主如果觉得自己错误多了改不过来的话,给你个建议,先写短一些的代码,比如只写一个类,只调用该类的功能摆弄一下。感觉没问题了再新建个工程写另一个类。所有可能存在的问题分别搞好了再把不同的类用在一个工程里。对自己语法还不太自信的情况下最好不要一口吃个胖子。一步步慢慢来更容易解决问题,也有利于提高自己信心。
    再有就是如5楼所说,自己多琢磨一下编译信息。
    其实编程中遇到的最可怕问题根本不是你这些语法错误,而是编译运行顺利通过,却没出现自己想要的结果。
#7
Orphan2015-06-06 18:28
回复 6楼 yangfrancis
谢谢你的建议,可是我看不出来= =||
#8
Orphan2015-06-06 18:34
回复 5楼 诸葛欧阳
可是我看不出来,英文单词一定要正确吗,好忧伤
#9
Orphan2015-06-06 18:35
回复 4楼 rjsp
只是没有听= =
#10
Vsnow2015-06-06 23:37
英文单词的正确那肯定是必须的啊!就连符号都不能有错呐
#11
T_MACC2015-06-11 21:21
回复 3楼 林月儿
搞c++啦 ??
#12
林月儿2015-06-11 21:27
回复 11楼 T_MACC
没有啦,学的java,感觉差不多啦
#13
T_MACC2015-06-11 21:39
回复 12楼 林月儿
高级编程语言啊,可惜啊,我们先学习c c++啥的 都得学习 ,
#14
林月儿2015-06-11 21:43
回复 13楼 T_MACC
高级编程语言啊,可惜啊,我们先学习c c++啥的 都得学习 ,
。。。被你说的好像吃糖果一样容易
1