| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 652 人关注过本帖
标题:请指点,为什么不能用这个表达式?
取消只看楼主 加入收藏
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
收藏
已结贴  问题点数:10 回复次数:0 
请指点,为什么不能用这个表达式?
一个分子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
搜索更多相关主题的帖子: double public display structure convert 
2013-06-12 12:14
快速回复:请指点,为什么不能用这个表达式?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033035 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved