我寫了下面employee
類:C++數組,數組賦值
#include<iostream>
#include<string>
using namespace std;
class employee
{
private:
int id;
int salaries[12];
int annualS;
string name;
public:
employee(int id2, string name2, int array[12])
{
id = id2;
name=name2;
salaries = array; //here where the error occurred.
}
~employee()
{
cout<<"Object Destructed";
}
employee()
{
id = 0;
name="Mhammad";
}
int annulalSalary()
{
for(int i=0; i<12; i++)
{
annualS+=salaries[i];
}
return annualS;
}
int tax()
{
return (annualS*10/100);
}
};
void main()
{
int salaries[12];
for(int i=0; i<12; i++)
{
cin>>salaries[i];
}
employee Mohammad(10,"Mohammad",salaries);
cout<< Mohammad.annulalSalary();
cout<< Mohammad.tax();
}
...但是當我編譯它,編譯器返回以下錯誤:
cannot convert from 'int []' to 'int [12]'
誰能幫我解決這個問題?
使用'std :: array'或'std :: vector ',它們有'operator ='重載(等等),這意味着你不必自己寫分配代碼。 –
Borgleader
2014-10-17 19:57:57
問題是你的構造函數中的參數。工資不能被聲明爲12號。相反,使用'int * salaries'。但是,是的,你應該使用矢量,更好,更安全 –
WindowsMaker
2014-10-17 20:06:29