指针传参的问题,请看代码
// Danjicheng.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
/*首先定义一个动物类作为基类*/
class Dongwu
{
protected:
char* Name;
char* Aihao;
public:
Dongwu();
void SetName(char* name);
void SetAihao(char* aihao);
void xianshi();
};
Dongwu::Dongwu()
{
Name = new char[20];
Aihao = new char[20];
}
void Dongwu::SetName(char* name)
{
strcpy(Name,name);
}
void Dongwu::SetAihao(char* aihao)
{
strcpy(Aihao,aihao);
}
void Dongwu::xianshi()
{
cout<<"基类显示:"<<'\t';
cout<<"动物类中,一个名为:"<<Name<<"的未知的动物,爱好:"<<Aihao<<'\n';
}
/*创建一个继承与动物类的 狗类 Dog类*/
class Dog:public Dongwu
{
protected:
int* Age;
public:
Dog();
void DogAge(int* age);
void Dogxianshi();
~Dog();
};
void Dog::Dogxianshi()
{
cout<<"Dog中显示"<<'\t';
cout<<"狗类中,一个名为:"<<Name<<"的狗,平时喜欢:"<<Aihao<<"今年:"<<Age<<"岁了"<<'\n';
}
void Dog::DogAge(int* age)
{
Age = age;
}
Dog::Dog()
{
Age = new int[1];
Name = new char[20];
Aihao = new char[20];
}
Dog::~Dog()
{
}
int main(int argc, char* argv[])
{
Dog dog;
dog.SetName("皮皮");
dog.SetAihao("咬人");
dog.DogAge(20);//此处有错误,肯定是传参的时候出现了问题,求教帮助!!!!!!!!!!!!!!
dog.xianshi();
dog.Dogxianshi();
}
提示错误如下:
--------------------Configuration: Danjicheng - Win32 Debug--------------------
Compiling...
Danjicheng.cpp
D:\.net程序\Danjicheng\Danjicheng.cpp(75) : error C2664: 'DogAge' : cannot convert parameter 1 from 'const int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
D:\.net程序\Danjicheng\Danjicheng.cpp(78) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
Danjicheng.exe - 1 error(s), 1 warning(s)