| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1746 人关注过本帖
标题:头文件问题
只看楼主 加入收藏
sfwyx
Rank: 2
等 级:论坛游民
帖 子:25
专家分:59
注 册:2011-8-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:14 
头文件问题
先上逻辑有点混乱的代码(程序简单没加注释)
///////////////////main.cpp
#include"man.h"
#include<iostream>
using namespace std;
char ManOrWoman;
void Man();
void Woman();
int main()
{
    cout<<"***********WELCOME***********"<<endl;
    cout<<"Are you a man or a woman?(M/W)"<<endl;
    cin>>ManOrWoman;
    if(ManOrWoman=='m'||ManOrWoman=='M')
        Man();
    else
        Woman();
    return 0;
}
void Woman()
{
    int l, g, ag, wei, hi;
    string jo, nam;
    int car, hou;
    unsigned mo, sala;
    cout<<"Your name:";cin>>nam;cout<<"Your age:";cin>>ag;cout<<"Your weight:";cin>>wei;cout<<"Your job:";cin>>jo;
    cout<<"Leghth of you vagina:";cin>>l;cout<<"Value of your glam:";cin>>g;
    cout<<"Your height::";cin>>hi;cout<<"Number of you cars:";cin>>car;
    cout<<"Number of you houses:";
    cin>>hou;cout<<"How much is your money:";cin>>mo;cout<<"How much is your salary:";cin>>sala;
    human *you(ag,wei,hi,jo,nam,car,hou,mo,sala);
    you=new woman(l,g);
    man SB(180,10,25,57,160,"SB","2B",0,0,10000000,0);
    man    ZHY(175,0,23,55,180,"2b","周鸿祎",5,5,1000000,50000);
    man    MHT(20,-8,2,180,55,"SB","马化腾",50,50,1000000000,0);
    man    WLGQ(189,60,29,55,189,"技术宅","[我嘞个去,名字被屏蔽了]",2,2,10000,1000);
    man    NB(190,87,39,67,189,"工人最伟大","盖茨(艺名)",0,1,100000,5000);
    bool sb=SB.ShowL(you); bool mht=MHT.ShowL(you);
    bool zhy=ZHY.ShowL(you); bool wlgq=WLGQ.ShowL(you); bool nb=NB.ShowL(you);
    if(sb)
        cout<<"Congratulations!"<<endl;
    else
    {
        if(zhy)
            cout<<"Congratulations!"<<endl;
        else{
                if(mht)
                    cout<<"Congratulations!"<<endl;
                else
                {
                        if(wlgq)
                            cout<<"Congratulations!"<<endl;
                        else
                        {
                                if(nb)
                                    cout<<"Congratulations!"<<endl;
                                else
                                    cout<<"没人要的屌丝,滚!"<<endl;
                        }
                }
            }
    }
}
void Man()
{
    int l, g, ag, wei, hi;
    string jo, nam;
    int car, hou;
    unsigned mo, sala;
    cout<<"Your name:";cin>>nam;cout<<"Your age:";cin>>ag;cout<<"Your weight:";cin>>wei;cout<<"Your job:";cin>>jo;
    cout<<"Leghth of you penis:";cin>>l;cout<<"Value of your glam:";cin>>g;
    cout<<"Your height::";cin>>hi;cout<<"Number of you cars:";cin>>car;
    cout<<"Number of you houses:";
    cin>>hou;cout<<"How much is your money:";cin>>mo;cout<<"How much is your salary:";cin>>sala;
    human *you(ag,wei,hi,jo,nam,car,hou,mo,sala);
    you=new man(l,g);
    woman SB(180,10,25,57,160,"SB","2B",0,0,10000000,0);
    woman    ZHY(175,0,23,55,180,"2b","周鸿祎",5,5,1000000,50000);
    woman    MHT(20,-8,2,180,55,"SB","马化腾",50,50,1000000000,0);
    woman    WLGQ(189,60,29,55,189,"技术宅","[我嘞个去,名字被屏蔽了]",2,2,10000,1000);
    woman    NB(190,87,39,67,189,"工人最伟大","盖茨(艺名)",0,1,100000,5000);
    bool sb=SB.ShowL(you); bool mht=MHT.ShowL(you);
    bool zhy=ZHY.ShowL(you); bool wlgq=WLGQ.ShowL(you); bool nb=NB.ShowL(you);
    if(sb)
        cout<<"Congratulations!"<<endl;
    else
    {
        if(zhy)
            cout<<"Congratulations!"<<endl;
        else{
                if(mht)
                    cout<<"Congratulations!"<<endl;
                else
                {
                        if(wlgq)
                            cout<<"Congratulations!"<<endl;
                        else
                        {
                                if(nb)
                                    cout<<"Congratulations!"<<endl;
                                else
                                    cout<<"没人要的屌丝,滚!"<<endl;
                        }
                }
            }
    }
}
/////////////////////////man.h
#include"woman.h"
#include<iostream>
using namespace std;
#ifdef MAN_H
#define MAN_H
class man
{
public:
    man(int l,ine g,int ag,int wei,int hi,string jo,string nam,int car,int hou,unsigned mo,unsigned sala);
    bool ShowL(man me,woman lo);
    void AI(man me,woman lo);
    bool Interest(woman lo);
private:
    int Leghth,glam;
    bool doEsSheLove;
    char yEs;
    string sty;
    int age,weight,height;
    string Name,sex,job;
    int cars,houses;
    unsigned money,salary;
};
man::man(int l,int g,int ag,int wei,int hi,string jo,string nam,int car,int hou,unsigned mo,unsigned sala)
{
    Leghth=l;glam=g;age=ag;        weight=wei;
        height=hi;
        Name=nam;
        cars=car;
        houses=hou;
        money=mo;
        job=jo;
        if(glam>=50 && money>=10000000)
            sty="Gaofushuai";
        else
            sty="Diaosi";
}
bool man::ShowL(man me,woman lo)
{
    cout<<"Darling, come on!T`m "<<me.Name<<"and I`m a "<<sty
        <<endl<<"Value of my glamour:"<<me.glam<<", my height:"<<me.height<<", leighth of my penis"<<me.Leghth<<"mm."<<endl;
    cout<<"I have "<<me.Cars<<"cars and "<<me.Houses<<", and 128√e980!"<<endl;
    cout<<"I cannot wait!!!"<<endl;
    lo.Interest(me);
    if(lo.yEs=='Y'||lo.yEs=='y')
        return true;
    else
        return false;
}
bool man::Interest(woman lo)
{
    cout<<"Are you interted in her?(Y/N)"<<endl;
    cin>>lo.yEs;
    if(lo.yEs=='Y'||lo.yEs=='y')
        return true;
    else
        return false;
}
#endif
////////////////////////////woman.h
#include"man.h"
#include<iostream>
using namespace std;
#ifdef WOMAN_H
#define WOMAN_H
class woman
{
public:
    woman(int l,ine g,int ag,int wei,int hi,string jo,string nam,int car,int hou,unsigned mo,unsigned sala);
    bool ShowL(woman me,man lo);
    bool Interest(woman me,man lo);
private:
    int Leghth,glam;
    bool doEsHeLove;
    char yEs;
    string sty;
    int age,weight,height;
    string Name,sex,job;
    int cars,houses;
    unsigned money,salary;
};
woman::woman(int l,int g,int ag,int wei,int hi,string jo,string nam,int car,int hou,unsigned mo,unsigned sala)
{
    Leghth=l;glam=g;age=ag;        weight=wei;
        height=hi;
        Name=nam;
        sex=se;
        cars=car;
        houses=hou;
        money=mo;
        if(glam>=50 && money>=10000000)
            sty=Baifumei;
        else
            sty=Diaosi;
}
bool woman::ShowL(woman me,man lo)
{
    cout<<"Darling, come on!I`m "<<me.Name<<"and I`m a "<<sty
        <<endl<<"Value of my glamour:"<<me.glam<<", my height:"<<me.height<<", leighth of my vagina"<<me.Leghth<<endl;
    cout<<"I have "<<me.Cars<<"cars and "<<me.Houses<<", and 128√e980!"<<endl;
    cout<<"I cannot wait!!!"<<endl;
    lo.Interest(me);
    if(lo.yEs=='Y'||lo.yEs=='y')
        return true;
    else
        return false;
}
bool woman::Interest(man lo)
{
    cout<<"Are you interted in him?(Y/N)"<<endl;
    cin>>lo.yEs;
    if(lo.yEs=='Y'||lo.yEs=='y')
        return true;
    else
        return false;
}
#endif
有两个问题:
一、编译时提示无限递归(貌似是这问题,机器翻译的,我不知道确切的翻译)
e:man.h(1) : warning C4182: #include nesting level is 363 deep; possible infinite recursion
e:man.h(1) : fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
二、《c++ primer 4》上把头文件保护符说的神乎其神的,但是头文件保护符是不是真的那么重要?上边的程序不加保护符会提示一大堆奇怪的错误,加上保护符就会提示无限递归

[ 本帖最后由 sfwyx 于 2012-6-30 18:05 编辑 ]
搜索更多相关主题的帖子: return include void 
2012-06-30 18:04
lonmaor
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:郑州
等 级:版主
威 望:75
帖 子:2637
专家分:6423
注 册:2007-11-27
收藏
得分:0 
楼主的代码很强大

从不知道到知道,到知道自己不知道,成长的道路上脚步深深浅浅
2012-06-30 18:39
sfwyx
Rank: 2
等 级:论坛游民
帖 子:25
专家分:59
注 册:2011-8-18
收藏
得分:0 
版主别光说笑啊
2012-07-01 10:07
sfwyx
Rank: 2
等 级:论坛游民
帖 子:25
专家分:59
注 册:2011-8-18
收藏
得分:0 
在线等
2012-07-01 11:01
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
把 .h 里的 ifdef 等指令挪到第一行试试。
2012-07-01 11:06
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
刚还没注意看,你这是什么逻辑?
ifdef ... define ...
如果定义过了就再定义一遍?

你随便找一个系统自带的头文件看看是怎么写的就行了。
2012-07-01 11:31
sfwyx
Rank: 2
等 级:论坛游民
帖 子:25
专家分:59
注 册:2011-8-18
收藏
得分:0 
回复 6楼 pangding
啊,少了个n
但是挪到第一行再加个n还是有问题
--------------------Configuration: 5_3_2 - Win32 Debug--------------------
Compiling...
main.cpp
e:\man.h(3) : warning C4182: #include nesting level is 363 deep; possible infinite recursion
e:\man.h(3) : fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
执行 cl.exe 时出错.

main.obj - 1 error(s), 0 warning(s)
2012-07-01 11:43
sfwyx
Rank: 2
等 级:论坛游民
帖 子:25
专家分:59
注 册:2011-8-18
收藏
得分:0 
回复 6楼 pangding
预处理器变量有两种状态:已定义或未定义。定义预处理器变量和检测其状态所用的预处理器指示不同。#define 指示接受一个名字并定义该名字为预处理器变量。#ifndef 指示检测指定的预处理器变量是否未定义。如果预处理器变量未定义,那么跟在其后的所有指示都被处理,直到出现 #endif。


可以使用这些设施来预防多次包含同一头文件:

     #ifndef SALESITEM_H
     #define SALESITEM_H
     // Definition of Sales_itemclass and related functions goes here
     #endif

条件指示

     #ifndef SALESITEM_H

测试 SALESITEM_H 预处理器变量是否未定义。如果 SALESITEM_H 未定义,那么 #ifndef 测试成功,跟在 #ifndef 后面的所有行都被执行,直到发现 #endif。相反,如果 SALESITEM_H 已定义,那么 #ifndef 指示测试为假,该指示和 #endif 指示间的代码都被忽略。


为了保证头文件在给定的源文件中只处理过一次,我们首先检测 #ifndef。第一次处理头文件时,测试会成功,因为 SALESITEM_H 还未定义。下一条语句定义了 SALESITEM_H。那样的话,如果我们编译的文件恰好又一次包含了该头文件。#ifndef 指示会发现 SALESITEM_H 已经定义,并且忽略该头文件的剩余部分。

头文件应该含有保护符,即使这些头文件不会被其他头文件包含。编写头文件保护符并不困难,而且如果头文件被包含多次,它可以避免难以理解的编译错误。

红字是《c++ primer 4》上的。开始我的头文件里用的是ifdef,现在改成ifndef还是不行啊
编译器带的头文件里太复杂了。《primer》的Sales_item.h里的和我的差不多,下边是Sales_item.h的代码
程序代码:
#ifndef SALESITEM_H
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here


#include <iostream>
#include <string>

class Sales_item {
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
    // added constructors to initialize from a string or an istream
    Sales_item(const std::string &book):
              isbn(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    // other members as before
   
public:
    // operations on Sales_item objects
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
        { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
// private members as before
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;

};


 //...
#endif
2012-07-01 11:51
jiantiewen
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:61
专家分:307
注 册:2012-6-22
收藏
得分:0 
回复 楼主 sfwyx
在定义man类前,先声明woman;定义woman类前,先声明man。再把man中的成员woman类对象换成指针形式的;同样,woman中的man类对象也改为指针。

[ 本帖最后由 jiantiewen 于 2012-7-1 13:19 编辑 ]
2012-07-01 12:41
sfwyx
Rank: 2
等 级:论坛游民
帖 子:25
专家分:59
注 册:2011-8-18
收藏
得分:0 
回复 9楼 jiantiewen
有不用指针的方法吗?
2012-07-01 15:02
快速回复:头文件问题
数据加载中...
 
   



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

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