The expected constructor, destructor, or type conversion before '(token

Compiling polygone.h and polygone.cc gives an error:

 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); 

I assume that the compiler does not recognize Polygone::Polygone(string nom) as a constructor, but if that is true, I have no idea why.

Any help?

+11
source share
3 answers

The first constructor in the header should not end with a semicolon. #include <string> missing in the header. string does not match std:: in the .cpp file. These are all simple syntax errors. More importantly: you don't use links whenever you want. The way to use ifstream also violated. I suggest learning C ++ before trying to use it.

Let's fix this:

 //polygone.h # if !defined(__POLYGONE_H__) # define __POLYGONE_H__ #include <iostream> #include <string> class Polygone { public: // declarations have to end with a semicolon, definitions do not Polygone(){} // why would we needs this? Polygone(const std::string& fichier); }; # endif 

and

 //polygone.cc // no need to include things twice #include "polygone.h" #include <fstream> Polygone::Polygone(const std::string& nom) { std::ifstream fichier (nom, ios::in); if (fichier.is_open()) { // keep the scope as tidy as possible std::string line; // getline returns the stream and streams convert to booleans while ( std::getline(fichier, line) ) { std::cout << line << std::endl; } } else { std::cerr << "Erreur a l'ouverture du fichier" << std::endl; } } 
+6
source

This is not just a newbie scenario. I just stumbled upon this compiler post (GCC 5.4) while refactoring a class to remove some constructor parameters. I forgot to update both the declaration and the definition, and the compiler posted this unintuitive error.

The bottom line is: if the compiler cannot match the signature of the definition with the signature of the declaration, it thinks that the definition is not a constructor, and then does not know how to parse the code, and displays this error. What also happened with OP: std::string not of the same type as string so the signature of the declaration was different from the definition, and this message was posted.

As a note, it would be nice if the compiler looked for almost matching constructor signatures and, finding one, suggested that the parameters did not match, and did not produce this message.

+3
source

You are missing the std namespace reference in the cc file. You should also call nom.c_str() because there is no implicit conversion from std::string to const char * expected by the ifstream constructor.

 Polygone::Polygone(std::string nom) { std::ifstream fichier (nom.c_str(), std::ifstream::in); // ... } 
+2
source

Source: https://habr.com/ru/post/1392275/


All Articles