编译polygone.h
and polygone.cc
给出错误:
polygone.cc:5:19: error: expected constructor, destructor, or type conversion before ‘(’ token
Code:
//polygone.h
# if !defined(__POLYGONE_H__)
# define __POLYGONE_H__
# include <iostream>
class Polygone {
public:
Polygone(){};
Polygone(std::string fichier);
};
# endif
and
//polygone.cc
# include <iostream>
# include <fstream>
# include "polygone.h"
Polygone::Polygone(string nom)
{
std::ifstream fichier (nom, ios::in);
std::string line;
if (fichier.is_open())
{
while ( fichier.good() )
{
getline (fichier, line);
std::cout << line << std::endl;
}
}
else
{
std::cerr << "Erreur a l'ouverture du fichier" << std::endl;
}
}
//ifstream fich1 (argv[1], ios::in);
我的猜测是编译器无法识别Polygone::Polygone(string nom)
作为构造函数,但是,如果确实如此,我不知道为什么。
有什么帮助吗?
这不仅仅是一个“新手”场景。我刚刚在重构类以删除一些构造函数参数时遇到了此编译器消息(GCC 5.4)。我忘记更新声明和定义,编译器吐出了这个不直观的错误。
底线似乎是这样的:如果编译器无法将定义的签名与声明的签名匹配,它会认为定义不是构造函数,然后不知道如何解析代码并显示此错误。这也是 OP 发生的事情:std::string
与以下类型不同string
因此声明的签名与定义的签名不同,并且该消息被吐出。
附带说明一下,如果编译器寻找几乎匹配的构造函数签名,并且在发现签名时建议参数不匹配而不是给出此消息,那就太好了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)