C++ 编译器错误:“构造函数的返回类型规范无效”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14885819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ compiler error: "return type specification for constructor invalid"
提问by Sunny
Here's my code. When compiling all the files I get this error, I am not sure what I am doing wrong. Please advise.
这是我的代码。编译所有文件时出现此错误,我不确定我做错了什么。请指教。
Molecule.cpp:7:34: error: return type specification for constructor invalid
Molecule.cpp:7:34: 错误:构造函数的返回类型规范无效
//Sunny Pathak
//Molecule.cpp
#include <iostream>
#include "Molecule.h"
using namespace std;
inline void Molecule::Molecule(){
int count;
count = 0;
}//end function
bool Molecule::read(){
cout << "Enter structure: %c\n" << structure << endl;
cout << "Enter full name: %c\n" << name << endl;
cout << "Enter weight : %f\n" << weight << endl;
}//end function
void Molecule::display() const{
cout << structure << ' ' << name << ' ' << weight << ' ' << endl;
}//end function
回答by juanchopanza
A constructor has no return type:
构造函数没有返回类型:
class Molecule
{
public:
Molecule(); // constructor. No return type.
bool read();
void display() const;
};
Molecule::Molecule(){
int count;
count = 0;
}//end constructor
Also note that count
is local to the body of the constructor, and you are not using it for anything.
另请注意,它count
是构造函数主体的局部变量,并且您没有将它用于任何用途。
回答by Andy Prowl
You're writing a constructor with a return type. Constructors have no return type. Just change your constructor definition into:
您正在编写具有返回类型的构造函数。构造函数没有返回类型。只需将您的构造函数定义更改为:
/* void */ Molecule::Molecule()
// ^^^^ Remove this
{
int count;
count = 0;
}
回答by billz
Constructor can not have return type.
构造函数不能有返回类型。
update:
更新:
inline void Molecule::Molecule(){
^^^
int count;
count = 0;
}//end function
to:
到:
Molecule::Molecule(){
int count;
count = 0;
}//end function