类和构造错误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
{
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'