它是指向对象的数组的基本程序。
#include <iostream>
using namespace std;
class city
{
protected:
char *name;
int len;
public:
city()
{
len=0;
name= new char[len+1];
}
void getname(void)
{
char *s;
s= new char[30];
cout<< "enter city name";
cin >> s;
len= strlen(s);
name = new char[len+1];
strcpy(name, s);
}
void printname(void)
{
cout<< name <<"\n";
}
};
编译器说问题出在 "cout<< name <<"\n";"
int main()
{
city *cptr[10];
int n=1;
int option;
do
{
cptr[n]= new city;
cptr[n]->getname();
n++;
cout<< "continue? yes=1, no=0. select now?";
cin>> option;
}
while(option);
cout<< endl<< endl;
for (int i=1;i<=n;i++)
{
cptr[i]-> printname();
}
cin.ignore();
getchar();
return 0;
};
还有一个警告(这个警告不是问题)
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
我试图strcpy_s
删除警告,但这个词无法识别。