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

基于 c++ 中 strcmp 的用法错误分析

七夜之华 发布于 2014-09-07 14:23, 567 次点击
#include<iostream>
原始代码(出错代码)为:
#include<string>
using namespace std;
class Point
{
      protected:
              int x,y;
      public:
             void draw(int a,int b)
             {
                  x=a;
                  y=b;
             }
             void show()
             {
                  cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
             }
};
class colour_Point : public Point
{
      private:
              char colour;
      public:
              virtual void draw(int m,int n,char &q)
             {
                  x=m;
                  y=n;
                  colour=q;
             }
             virtual void show()
             {
                  cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
                  cout<<"And the Point's colour is   "<<colour<<endl;
             }
};


int main()
{
    Point point;
    point.draw(3,4);
    point.show();
    colour_Point colour_point;
    colour_point.draw(4,5,"紫色");
    colour_point.show();
    system("pause");
    return 0;
}
    //以上代码编译出错,出错原因:字符串字长限制。
   
   修改方案:将地址引用,换成指针传递变量。

修改成功的代码为:
#include<iostream>
#include<string>
using namespace std;
class Point
{
      protected:
              int x,y;
      public:
             void draw(int a,int b)
             {
                  x=a;
                  y=b;
             }
             void show()
             {
                  cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
             }
};
class colour_Point : public Point
{
      private:
              char *colour;
      public:
              virtual void draw(int m,int n,char *q)
             {
                  x=m;
                  y=n;
                  colour=q;
             }
             virtual void show()
             {
                  cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
                  cout<<"And the Point's colour is   "<<colour<<endl;
             }
};


int main()
{
    Point point;
    point.draw(3,4);
    point.show();
    colour_Point colour_point;
    colour_point.draw(4,5,"紫色");
    colour_point.show();
    system("pause");
    return 0;
}
   编译结果为:    This point is the abscissa for  3 , y coordinate for 4   
                   This point is the abscissa for  4 ,y coordinate for 5,and the Point's colour is 紫色
4 回复
#2
zklhp2014-09-07 14:48
说实话 没看懂
#3
stop12042014-09-08 08:37
关字长什么事.. 我觉得你应该去看下如何使用引用.

数组类的数据只能用指针引用.
#4
七夜之华2014-09-08 16:14
回复 2 楼 zklhp
地址引用与指针传递的区别吧。程序运行前后有明显的修改之处。
#5
七夜之华2014-09-08 16:15
回复 3 楼 stop1204
字长限制是程序运行后指定的错误,不过你的建议我会好好看看的呢。
1