看板Programming
标 题Re: 请教C++与结构
发信站无名小站 (Sun Apr 22 16:30:36 2007)
转信站ptt!ctu-reader!ctu-peer!news.nctu!netnews.csie.nctu!wretch
※ 引述《[email protected] ( )》之铭言:
> 请教一下,为何
> person1.name2="Mary";
> 会有问题呢?
> 谢谢
> 附上原始程式码:
> #include <iostream>
> using namespace std;
> main( )
> {
> struct company //结构
> {
> char name2[20];
> int salary;
> int prize;
> int total;
> };
> struct company person1;
> person1.name2="Mary";
> person1.salary=20000;
> person1.prize=1500;
> person1.total=person1.salary + person1.prize;
> cout << "姓名:" << person1.name2 <<endl;
> cout << "薪资:" << person1.salary<<endl;
> cout << "奖金:" << person1.prize<<endl;
> cout << "总共:" << person1.total<<endl;
> // system("PAUSE");
> return 0; }
g++後的错误讯息
error: incompatible types in assignment of `const char[5]' to `char[20]'
person1.name2本身是阵列起始位址,没法做指定运算
试看看
strcpy(person1.name2,"Mary"); // #include <cstring>
--
夫兵者不祥之器物或恶之故有道者不处君子居则贵左用兵则贵右兵者不祥之器非君子
之器不得已而用之恬淡为上胜而不美而美之者是乐杀人夫乐杀人者则不可得志於天下
矣吉事尚左凶事尚右偏将军居左上将军居右言以丧礼处之杀人之众以哀悲泣之战胜以
丧礼处之道常无名朴虽小天下莫能臣侯王若能守之万物将自宾天地相合以降甘露民莫
之令而自均始制有名名亦既有夫亦将知止知止可以不殆 Kino.Dorm10.NCTU.edu.tw海
1F:推 williamyu:受教 61.216.177.204 04/24 10:17