这段代码能编译运行,但是运行后出错,哪的有关问题呀
这段代码能编译运行,但是运行后出错,哪的问题呀?
if(typeid(*zshu[i])==typeid(Inch)) 这个哪有问题?
........
zshu数组里放的是类地址,Inch是类。
运行到这步的时候出错 Debug Error....
------解决方案--------------------
我怎么编译和运行都木有问题捏。。。
------解决方案--------------------
zshu[i] 的指针正确么?
------解决方案--------------------
我也没有问题啊, 请检查指针。。。
#include <iostream>
using namespace std;
class Inch{
private:
int a;
public:
Inch(int x=0):a(x){}
void output(){cout < < "a is " < <a < <endl;}
};
int main()
{
Inch* zshu[2];
zshu[0] = new Inch(100);
zshu[1] = new Inch(101);
if(typeid(*zshu[0])==typeid(Inch))
{zshu[0]-> output();}
delete zshu[0];
delete zshu[1];
return 0;
}
/home/test> g++ zhu.c
/home/test> ./a.out
a is 100
------解决方案--------------------
typeid中只能比较数据类型 *zshu[0]表示一个对象实例
语法上是正确的,因此编译通过
------解决方案--------------------
可能是你的i运行越界了吧?贴出代码看看
------解决方案--------------------
BaseLength* readlength()返回的是BaseLength*
但你函数内操作是new Inch(cd);,new Meters(cd);
这样结果出来指针类型已经混乱了
但我将readlength(),zshu[5]都定义为Inch,切Inch类扩充变量(不知道typeid如何运作,应该是根据指针寻址后判断吧?)后,编译仍然报
used on polymorphic type 'class Inch ' with /GR-; unpredictable behavior may result
运行也出错,好像要是继承类指针就有问题,请高手指教啊。。
------解决方案--------------------
用dynamic_cast,不要用typeid
------解决方案--------------------
typeid Operator
C++ Specific —>
typeid( type-id )
typeid( expression )
The typeid operator allows the type of an object to be determined at run-time.
The result of a typeid expression is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used. See type_info Class for more information.
END C++ Specific
------解决方案--------------------
没太明白楼主的意思。。。
if(typeid(*zshu[i])==typeid(Inch)) 这个哪有问题?
........
zshu数组里放的是类地址,Inch是类。
运行到这步的时候出错 Debug Error....
------解决方案--------------------
我怎么编译和运行都木有问题捏。。。
------解决方案--------------------
zshu[i] 的指针正确么?
------解决方案--------------------
我也没有问题啊, 请检查指针。。。
#include <iostream>
using namespace std;
class Inch{
private:
int a;
public:
Inch(int x=0):a(x){}
void output(){cout < < "a is " < <a < <endl;}
};
int main()
{
Inch* zshu[2];
zshu[0] = new Inch(100);
zshu[1] = new Inch(101);
if(typeid(*zshu[0])==typeid(Inch))
{zshu[0]-> output();}
delete zshu[0];
delete zshu[1];
return 0;
}
/home/test> g++ zhu.c
/home/test> ./a.out
a is 100
------解决方案--------------------
typeid中只能比较数据类型 *zshu[0]表示一个对象实例
语法上是正确的,因此编译通过
------解决方案--------------------
可能是你的i运行越界了吧?贴出代码看看
------解决方案--------------------
BaseLength* readlength()返回的是BaseLength*
但你函数内操作是new Inch(cd);,new Meters(cd);
这样结果出来指针类型已经混乱了
但我将readlength(),zshu[5]都定义为Inch,切Inch类扩充变量(不知道typeid如何运作,应该是根据指针寻址后判断吧?)后,编译仍然报
used on polymorphic type 'class Inch ' with /GR-; unpredictable behavior may result
运行也出错,好像要是继承类指针就有问题,请高手指教啊。。
------解决方案--------------------
用dynamic_cast,不要用typeid
------解决方案--------------------
typeid Operator
C++ Specific —>
typeid( type-id )
typeid( expression )
The typeid operator allows the type of an object to be determined at run-time.
The result of a typeid expression is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used. See type_info Class for more information.
END C++ Specific
------解决方案--------------------
没太明白楼主的意思。。。