类和构造错误C

Class and construct Error C++

本文关键字:错误      更新时间:2023-10-16

我需要此错误的帮助?

molecule.cpp:31:7:错误:对'mole'的合格引用是构造函数名称,而不是可以声明构造函数的类型mole :: mole(atom1((,atom2((({

class mole {
private:
        string name;
        int proton;
        int neutron;
        int electron;
        int valence;
public:
        int mass();
        mole();
        mole(Atom, Atom);
        mole(string);
        mole(string,int,int,int);
};

mole::mole()
  {
        name="hydrogen";
        proton=1;
        neutron=0;
        electron=1;
        valence=1;
}
mole::mole(Atom1(), Atom2() ){
        proton= Atom1.p + Atom2.p;
        neutron=Atom1.n + Atom2.n;
        electron=Atom1.e + Atom2.e;
}

在另一个文件中:

#include<iostream>
using namespace std;
class Atom {
private:
        string name;
        int proton;
        int neutron;
        int electron;
        int valence;
public:
        int mass();
        Atom();
        Atom(int,int,int);
        Atom(string);
        Atom(string,int,int,int);
};

Atom::Atom(){
        name="hydrogen";
        proton=1;
        neutron=0;
        electron=1;
        valence=1;
}
Atom::Atom(int p, int n, int e){
        proton=p;
        neutron=n;
        electron=e;
}
Atom::Atom(string n){
        name=n;
}
Atom::Atom(string nm, int p, int n, int e){
        name = nm;
        proton=p;
        neutron=n;
        electron=e;
}

int Atom::mass(){
        int mass = proton+neutron;
        return mass;
}

我假设Atom是在其他地方声明的类?如果您想能够接受非默认构造器中Atom类型的两个参数,则应像这样声明它:

mole(Atom atom1, Atom atom2);
...
mole::mole(Atom atom1, Atom atom2) {
    proton = atom1.p + atom2.p
    ....
}

很多错误。让我们整理一下,使其成为真正的C 。

mole::mole( const Atom & a, const Atom & b )
    : proton( a.p + b.p )
    , neutron( a.n + b.n )
    , electron( a.e + b.e )
    , valence{}  // or whatever calculation you intended  
{
}