请指点,为什么不能用这个表达式?
一个分子class我定义为://----------------------- Molecule.h --------------------
//
class Molecule//define Molecule class
{
char mstring[11];//molecule structure
char name[21];//molecule name
double weight;//molecule weight
public:
Molecule();
bool read();
void display() const;
};
我要向一个class输入分子数据的函数并输出,请注意看下面红色部分,编译器vs老是提示:Error 1 error C2440: '=' : cannot convert from 'char' to 'char [11]'
//------------------------ Molecule.cpp -----------------
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
#include "Molecule.h"
Molecule::Molecule()
{
mstring[0]='\0';
name[0]='\0';
weight=0.0;
}
bool Molecule::read()//bool read()
{
int value=0;// temporary value holders
bool done;
double wt;
char str1[11];
char str2[21];
done=false;
do
{
cout << "Enter structure : "; // prompts user
cin.get(str1, 11); // extract symbol or "0"
cin.ignore(); // extract newline terminator
if (str='0')//这边要表达的意思是当用户输入数字0时,把目前的object存入一个a safe empty state里,其他方法都试过了 请指点 谢谢
{
mstring[0]='\0';
name[0]='\0';
weight=0.0;
}
else
{
cout << "Enter full name : "; // prompts user
cin.get(str2, 21); // extract symbol or "0"
cin.ignore(); // extract newline terminator
cout << "Enter weight : "; // prompt user
cin >> wt;
cin.ignore(); // extract newline terminator
// store input data in current object
strcpy(mstring, str1);
strcpy(name, str2);
weight = wt;
done=true;
}
} while(value = 0);
return done;
}
void Molecule::display() const
{
if (mstring[0] != '\0')
{
cout.setf(ios::left);
cout.setf(ios::fixed);
cout.precision(3);
cout.width(20);
cout << mstring;
cout << name;
cout.unsetf(ios::left);
cout.width(10);
cout << weight << endl;
}
else
cout << "no data available" << endl;
}
另外两个文件为:
// w4x.h
const int MAX_MOLECULES = 10;
// w4x.cpp
#include <iostream>
using namespace std;
#include "w4x.h"
#include "Molecule.h"
int main() {
int n = MAX_MOLECULES;
Molecule molecule[MAX_MOLECULES];
cout << "Molecular Information\n";
cout << "=====================" << endl;
for (int i = 0; i < MAX_MOLECULES; i++) {
if (!molecule[i].read()) {
n = i;
i = MAX_MOLECULES;
}
cout << endl;
}
cout << "Structure Name Mass\n";
cout << "==================================================" << endl;
for (int i = 0; i < n; i++)
molecule[i].display();
}
输出结果如下:
Molecular Information
=====================
Enter structure : H2O
Enter full name : Water
Enter weight : 18.015
Enter structure : CO2
Enter full name : Carbon Dioxide
Enter weight : 44.010
Enter structure : NaCl
Enter full name : Sodium Chloride
Enter weight : 58.443
Enter structure : 0
Structure Name Mass
-------------------------------------------------
H2O Water 18.015
CO2 Carbon Dioxide 44.010
NaCl Sodium Chloride 58.443