首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

运行时总是有有关问题。帮忙解决一下

2012-02-27 
运行时总是有问题。帮忙解决一下/*****************baselength.h**********************/#ifndefbaselength

运行时总是有问题。帮忙解决一下
/*****************baselength.h**********************/
#ifndef   baselength_h
#define   baselength_h
class   baselength{
public:
      baselength(){mm=0;}
      baselength(long   themm);
      virtual   double   length()   const;
protected:
      long   mm;
      static   double   inches_number;
      static   double   meters_number;
      static   double   yards_number;
      static   double   perches_number;
};

class   inches:public   baselength{
public:
      inches(){baselength();}
      inches(double   thenumber);
      double   length()   const;
};

class   meters:public   baselength{
public:
      meters(){baselength();}
      meters(double   thenumber);
      double   length()   const;
};

class   yards:public   baselength{
public:
yards(){baselength();}
yards(double   thenumber);
double   length()   const;
};

class   perches:public   baselength{
public:
perches(){baselength();}
perches(double   thenumber);
double   length()   const;
};
#endif
/*****************baselength.h**********************/

/*****************baselength.cpp**********************/
#include   "stdafx.h "
#include   "baselength.h "
#include   <iostream>
using   namespace   std;
double   baselength::inches_number=25.4;
double   baselength::meters_number   =1000;
double   baselength::yards_number=inches_number*36;
double   baselength::perches_number   =yards_number*5.5;

baselength::baselength(long   thenumber)
{
mm=thenumber;
}
double   baselength::length   ()   const{
return   static_cast <double> (mm);
}

inches::inches(double   thenumber)
{
mm=static_cast <long> (thenumber);
}
double   inches::length   ()   const{
return   mm/inches_number;
}

meters::meters(double   thenumber)
{
mm=static_cast <long> (thenumber);
}
double   meters::length   ()   const{
return   mm/meters_number;
}

yards::yards(double   thenumber)
{
mm=static_cast <long> (thenumber);
}
double   yards::length   ()const   {
  return   mm/yards_number;
}

perches::perches(double   thenumber)
{
mm=static_cast <long> (thenumber);
}
double   perches::length   ()   const{
return   mm/perches_number;
}
/*****************baselength.cpp**********************/

/*****************main.cpp**********************/
#include   "stdafx.h "
#include   "baselength.h "
#include   <iostream>
#include   <typeinfo>
#include   <string>
using   namespace   std;

baselength*   read()
{
int   flag;
long   temp;
cout < < "please   switch(inches   1,meters   2,yards   3,perches   4,): ";
cin> > flag;


switch(flag){
case   1:
    cout < < "please   input   number: ";
    cin> > temp;
    return   new   inches(temp);
case   2:
    cout < < "please   input   number: ";
    cin> > temp;
    return   new   meters(temp);
case   3:
    cout < < "please   input   number: ";
    cin> > temp;
    return   new   yards(temp);
case   4:
    cout < < "please   input   number: ";
    cin> > temp;
    return   new   perches(temp);
default:
cout < < "input   error   " < <endl;
break;
}
}
int   main()
{
baselength*   pb[4];
int   i;
string   s;
for(i=0;i <4;i++)
            pb[i]=read();
for(i=0;i <4;i++){
                    if(typeid(*pb[i])==typeid(inches))     s= "inches ";
    else   if(typeid(*pb[i])==typeid(meters))     s= "meters ";
    else   if(typeid(*pb[i])==typeid(yards))       s= "yards ";
    else   if(typeid(*pb[i])==typeid(perches))   s= "perches ";
    cout < <pb[i]-> length < <s < < "       " < <pb[i]-> baselength::length   () < <endl;
}
for(i=0;i <4;i++)
    delete   pb[i];  
return   0;  
}

/*****************main.cpp**********************/
题目的大概意思是:定义一个类baselength,它把长度存储为一个整数值,单位毫米,该类有一个成员函数length(),函数返回一个指定长度的double值。从baselength中派生一些类inches,meters,yards和perches,并重写基类的length   函数,把长度返回为相应单位的double值(1英寸=25.4,1米=1000毫米,1码=36英寸,1杆=5.5码).定义一个main()函数,读取一系列不同单位的长度,创建相应的派生类对象,把它们的地址存储在baselength*类型的数组中,以毫米和原单位输出每个长度.

在运行时输入四个值后,就终止,,,想了好久都不知道是什么问题,希望大家帮一下忙.急......



[解决办法]
cout < <pb[i]-> length

cout < <pb[i]-> length();

这个你能编译过啊?

问题出在typeid上....
[解决办法]
如果你用的VC.net, 在工程属性的C/C++ 命令行 的附加选项加入 /GR即可

主义编译器的警告, 编译器给的每个警告都要仔细看看, 编译的时候最好坐到无错误 无警告

热点排行