C++中结构体数组
如何给结构体数组赋值,按照书上写下来,报一大堆错误,谁能帮我把这段代码调通,IDE是VC6.0:如下代码:#include<iostream>
#include<string>
using namespace std;
struct Person
{
string name;
int count;
};
void mian()
{
Person loader[3];//{"wang",0,"zhang",0,"li",0};书上的
//loader[0].name="zhang";
//loader[0].count=0;
loader[0]={"zhang",0};
loader[1]={"wang",0};
loader[2]={"li",0};
//loader[1].name="wang";
//loader[1].count=0;
//loader[2].name="lili";
//loader[2].count=0;
int i,j;
string loader_name;
for(i=0;i<10;i++)
{
cin>>loader_name;
}
cout<<endl;
for(j=0;j<3;j++)
{
if(loader_name==loader[j].name)
loader[j].count++;
}
cout<<endl;
for(i=0;i<3;i++)
{
cout<<loader[i].name<<":"<<loader[i].count<<endl;
}
}
才接触C++,很多的不懂, 谢谢啊
追问:Person loader[3]={"wang",0,"zhang",0,"li",0}; 但是在这里还是报错的,D:\C\Tools\VCINstalled\MSDev98\MyProjects\Study_EX_05\C++_结构体_01.CPP(11) : error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Person'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C\Tools\VCINstalled\MSDev98\MyProjects\Study_EX_05\C++_结构体_01.CPP(11) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Person'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C\Tools\VCINstalled\MSDev98\MyProjects\Study_EX_05\C++_结构体_01.CPP(11) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe. 3个错误就定位到这里? 是怎么回事 呢, 谢谢
答案:因为结构体数组中包含string类型,所以字符串数组赋值不能直接使用,应该要用字符串函数。建议改成字符数组保存字符串
代码段:
#include<iostream>
#include<string>
using namespace std;
struct Person
{
char name[20];//改动
int count;
};
void main()
{
Person loader[3]={"wang",0,"zhang",0,"li",0};
int i,j;
char loader_name[10][20];//改动
for(i=0;i<10;i++)
{
cin>>loader_name[i];
}
cout<<endl;
for(i=0;i<10;i++)
{
for(j=0;j<3;j++)
{
if(strcmp(loader_name[i],loader[j].name)==0)
loader[j].count++;
}
}
cout<<endl;
for(i=0;i<3;i++)
{
cout<<loader[i].name<<":"<<loader[i].count<<endl;
}
}
#include<iostream>
#include<string>
using namespace std;
struct Person
{
char name[10];
int count;
};
void main()
{
Person loader[3]={{"wang",0},{"zhang",0},{"li",0}};//书上的
//loader[0].name="zhang";
//loader[0].count=0;
/*loader[0]={"zhang",0};
loader[1]={"wang",0};
loader[2]={"li",0};
*/
//loader[1].name="wang";
//loader[1].count=0;
//loader[2].name="lili";
//loader[2].count=0;
int i,j;
char loader_name[10];
for(i=0;i<10;i++)
{
cin>>loader_name;
}
cout<<endl;
for(j=0;j<3;j++)
{
if(strcmp(loader_name,loader[j].name))
loader[j].count++;
}
cout<<endl;
for(i=0;i<3;i++)
{
cout<<loader[i].name<<":"<<loader[i].count<<endl;
}
}错误1:main写错了
错误2:loader[0]={"zhang",0};
loader[1]={"wang",0};
loader[2]={"li",0};
这是什么?左边是结构体,右边?
ke yi zhe yang gai :
#include<iostream>
#include<string>
using namespace std;
struct Person
{
string name;
int count;
Person(string str="",int n=0)
{
name=str;
count=0;
}
};
void main()
{
Person loader[3]/*={"wang",0,"zhang",0,"li",0}*/;//书上的
//loader[0].name="zhang";
//loader[0].count=0;
// loader[0]=p;//{"zhang",0};
loader[1]=Person("wang",0);
loader[2]=Person("li",0);
//loader[1].name="wang";
//loader[1].count=0;
//loader[2].name="lili";
//loader[2].count=0;
int i,j;
string loader_name;
for(i=0;i<10;i++)
{
cin>>loader_name;
}
cout<<endl;
for(j=0;j<3;j++)
{
if(loader_name==loader[j].name)
loader[j].count++;
}
cout<<endl;
for(i=0;i<3;i++)
{
cout<<loader[i].name<<":"<<loader[i].count<<endl;
}
}
书上的貌似不太规范,我没这样写过,可能是一种默认的写法吧
一般这样写Person loader[3]={Person("wang",0),Person("zhang",0),Person("li",0)};//书上的
当然一般写结构体得写构造函数
上一个:C++是什么?
下一个:c++ 编写dll 示例