| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 431 人关注过本帖
标题:俺刚学的友元函数怎么老编译通不过呢
只看楼主 加入收藏
pengle_008
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-7-1
收藏
 问题点数:0 回复次数:2 
俺刚学的友元函数怎么老编译通不过呢
红色的是问题语句,蓝色的是提示的错误

___________________________________________________________________________
date1.h

  #ifndef DATE1_H
  #define DATE1_H

class Date {
public:
    Date ( int = 1, int = 1, int = 1900 );
    void print() const;
    ~Date();
private:
    int month;
    int day;
    int year;
    int checkDay ( int );
};

#endif

_______________________________________________________________________________


emply1.h


#ifndef EMPLY1_H
#define EMPLY1_H

#include "date1.h"
class Employee {
public:
    Employee ( char *, char *, int, int, int, int, int, int );
    void print() const;
    ~Employee();
private:
    char firstName[ 25 ];
    char lastName[ 25 ];
    const Date birthDate;
    const Date hireDate;
}

#endif

________________________________________________________________________


test.cpp


#include <iostream.h>
#include <string.h>
#include "date1.h"

Date::Date ( int mn, int dy, int yr)
{
    if ( mn > 0 && mn <= 12 )
      month = mn;
    else {
        month = 1;
        cout < < "Month " < < mn < < " invalid. Set to month 1.\n";
  }
    year = yr;
    day = checkDay ( dy );
   
    cout < < "Date object consturctor for date ";
    print();
    cout < < endl;
}

void Date::print() const
  { cout < < month < < '/' < < day < < '/' < < year; }
  
Date::~Date()
{
    cout < < "Date object destructor for date ";
    print();
    cout < < endl;
}

int Date::checkDay ( int testDay )
{
    static const int daysPerMonth[ 13 ] =
      { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      
    if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
        return testDay;
      if ( month == 2 && testDay == 29 &&
            ( year % 400 == 0 ||
              ( year % 4 == 0 && year % 100 != 0 ) ) )
        return testDay;
        
      cout < < "Day " < < testDay < < " invalid. Set to day 1.\n";
      
      return 1;
}


// Fig. 7.4: emply1.cpp
// Member function definition for Employee class.
#include <iostream.h>
#include <string.h>
#include "emply1.h"
#include "date1.h"

Employee::Employee( char *fname, char *lname,
                    int bmonth, int bday, int byear,
                    int hmonth, int hday, int hyear )
:  birthDate ( bmonth, bday, byear ),
    hireDate ( hmonth, hday, hyear )

{
    int length = strlen ( fname );
    length = ( length < 25 ? length : 24 );
    strncpy ( firstName, fname, length );
    firstName[ length ] = '\0';
   
    length = strlen ( lname );
    length = ( length < 25 ? length : 24 );
    strncpy ( lastName, lname, length );
    lastName[ length ] = '\0';
   
    cout < < "Employee object constructor: "
        < < firstName < < ' ' < < lastName < < endl;
}

void Employee::print() const
{
    cout < < lastName < < ", " < < firstName < < "\nHired: ";
    hireDate.print();
    cout < < " Birth date: ";
    birthDate.print();
    cout < < endl;
}

Employee::~Employee()
{
    cout < < "Employee object destructor: "
        < < lastName < < ", " < < firstName < < endl;
}


// Fig. 7.4: fig07_04.cpp
// Demonstrating composition: an object with member objects.
#include <iostream.h>
#include <conio.h>
#include "emply1.h"

int main()
{
    Employee e ( "bob", "Jones", 7, 24, 1949, 3, 12, 1988 );
   
    cout < < '\n';
    e.print();
   
    cout < < "\nTest Date constructor with invalid values:\n";
    Date d ( 14, 35, 1994 );
    cout < < endl;
    getch();
    return 0;
}              

_________________________________________________________________________________

ISO C++ forbids defining types within return type
return type specification for constructor invalid.
搜索更多相关主题的帖子: 编译 函数 
2009-10-11 18:56
newCpp
Rank: 5Rank: 5
来 自:火星
等 级:职业侠客
威 望:3
帖 子:256
专家分:375
注 册:2009-8-17
收藏
得分:0 
这代码太长了点儿吧!
看着晕,O(∩_∩)O~,我还没学到友元函数呢!
帮不了你啥忙了!
不过你如果将你的代码缩短点儿在写友元函数会不会更容易找出来错误??

编程语言视频教程在线播放学习
2009-10-11 20:20
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:0 
birthDate ( bmonth, bday, byear ),
    hireDate ( hmonth, hday, hyear )
这个两个是什么类?

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-10-12 11:37
快速回复:俺刚学的友元函数怎么老编译通不过呢
数据加载中...
 
   



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

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