expected constructor, destructor, or type conversion before ‘(’ token

前端 未结 3 502
抹茶落季
抹茶落季 2020-12-17 16:36

Compiling polygone.h and polygone.cc gives error:

polygone.cc:5:19: error: expected constructor, destructor, or type conversion bef         


        
3条回答
  •  悲&欢浪女
    2020-12-17 17:34

    The first constructor in the header should not end with a semicolon. #include is missing in the header. string is not qualified with std:: in the .cpp file. Those are all simple syntax errors. More importantly: you are not using references, when you should. Also the way you use the ifstream is broken. I suggest learning C++ before trying to use it.

    Let's fix this up:

    //polygone.h
    # if !defined(__POLYGONE_H__)
    # define __POLYGONE_H__
    
    #include 
    #include     
    
    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 
    
    
    Polygone::Polygone(const std::string& nom)
    {
      std::ifstream fichier (nom, ios::in);
    
    
      if (fichier.is_open())
      {
        // keep the scope as tiny 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;
      }
    }
    

提交回复
热议问题