编译 polygone.h
和 polygone.cc
给出错误:
polygone.cc:5:19: error: expected constructor, destructor, or type conversion before ‘(’ token
代码:
//polygone.h
# if !defined(__POLYGONE_H__)
# define __POLYGONE_H__
# include <iostream>
class Polygone {
public:
Polygone(){};
Polygone(std::string fichier);
};
# endif
和
//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)
识别为构造函数,但是,如果确实如此,我不知道为什么。
有什么帮助吗?
原文由 Marconius 发布,翻译遵循 CC BY-SA 4.0 许可协议
标头中的第一个构造函数不应以分号结尾。
#include <string>
标题中缺少。string
std::
的条件。这些都是简单的语法错误。更重要的是:当你应该使用引用时,你没有使用。您使用ifstream
的方式也被破坏了。我建议在尝试使用它之前学习 C++。让我们解决这个问题:
和