C++ '(' 标记之前的预期构造函数、析构函数或类型转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8958044/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:23:08  来源:igfitidea点击:

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

c++classconstructor

提问by Marconius

Compiling polygone.hand polygone.ccgives error:

编译polygone.hpolygone.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);

My guess is that the compiler is not recognising Polygone::Polygone(string nom)as a constructor, but, if this actually is the case, I have no idea why.

我的猜测是编译器没有识别Polygone::Polygone(string nom)为构造函数,但是,如果确实如此,我不知道为什么。

Any help?

有什么帮助吗?

采纳答案by pmr

The first constructor in the header should not end with a semicolon. #include <string>is missing in the header. stringis 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 ifstreamis broken. I suggest learning C++ before trying to use it.

标题中的第一个构造函数不应以分号结尾。#include <string>标题中缺少。在 .cpp 文件中string没有限定std::。这些都是简单的语法错误。更重要的是:您没有使用引用,而您应该使用。你使用的方式也ifstream被打破了。我建议在尝试使用 C++ 之前先学习它。

Let's fix this up:

让我们解决这个问题:

//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;
  }
}

回答by Bob Kocisko

This is not only a 'newbie' scenario. I just ran across this compiler message (GCC 5.4) when refactoring a class to remove some constructor parameters. I forgot to update both the declaration and definition, and the compiler spit out this unintuitive error.

这不仅仅是一个“新手”场景。在重构类以删除一些构造函数参数时,我刚刚遇到了这个编译器消息(GCC 5.4)。我忘记更新声明和定义,编译器吐出这个不直观的错误。

The bottom line seems to be this: If the compiler can't match the definition's signature to the declaration's signature it thinks the definition is not a constructor and then doesn't know how to parse the code and displays this error. Which is also what happened for the OP: std::stringis not the same type as stringso the declaration's signature differed from the definition's and this message was spit out.

底线似乎是这样的:如果编译器无法将定义的签名与声明的签名相匹配,它会认为该定义不是构造函数,然后不知道如何解析代码并显示此错误。这也是 OP 发生的情况: std::string与类型不同,string因此声明的签名与定义的签名不同,并且此消息被吐出。

As a side note, it would be nice if the compiler looked for almost-matching constructor signatures and upon finding one suggested that the parameters didn't match rather than giving this message.

作为旁注,如果编译器寻找几乎匹配的构造函数签名并且在发现一个建议参数不匹配而不是给出此消息时,那就太好了。

回答by dasblinkenlight

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::stringto const char *expected by ifstream's constructor.

您在 cc 文件中缺少 std 命名空间引用。您还应该调用,nom.c_str()因为没有从std::stringconst char *expected byifstream的构造函数的隐式转换。

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