【问题标题】:C++ "No matching function for call" errorC++“没有匹配的调用函数”错误
【发布时间】:2015-01-29 05:24:25
【问题描述】:

我正在使用 MinGW 编译器。我不明白为什么我会出错。

错误:

Multiple markers at this line
- candidate is:
- no matching function for call to 'InsultGenerator::InsultGenerator()'
- Line breakpoint: Insultgenerator_0hl14.cpp [line: 22]

这是cpp文件:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Insultgenerator_0hl14.h"

using namespace std;

FileException::FileException(const string& m) : message(m){}

string& FileException::what(){ return message;}

NumInsultsOutOfBounds::NumInsultsOutOfBounds(const string& m) : message(m){}

string& NumInsultsOutOfBounds::what(){ return message;}

InsultGenerator::InsultGenerator(const InsultGenerator& ) {}

void InsultGenerator::initialize() const{
    int cols(0);
    InsultGenerator t1;
    string insults;

    string filename("InsultsSource.txt");
    ifstream file(filename.c_str());

    if(file.fail()){
        throw FileException("File not read.");
    }
     while(file >> insults){
         if(cols==0){
            t1.colA.push_back(insults);
            cols++;
         } else if(cols==1){
             t1.colB.push_back(insults);
             cols++;
         }else{
             t1.colC.push_back(insults);
             cols= cols -2;
         }


     }
for (int i=0;i<50;i++){
    cout << i << t1.colA[i];
}

}

这是头文件:

#ifndef INSULTGENERATOR_0HL14_H_
#define INSULTGENERATOR_0HL14_H_

#include <string>
#include <vector>

using namespace std;

class InsultGenerator{
public:
//  InsultGenerator(vector<string>);
    InsultGenerator(const InsultGenerator &);
    void initialize() const;
    string talkToMe() const;
    vector<string> generate(const int) const;
    int generateAndSave (const string, const int) const;

private:
    vector<string> colA;
    vector<string> colB;
    vector<string> colC;
};

class FileException{
public:
    FileException(const string&);
    string& what();
private:
    string message;
};

class NumInsultsOutOfBounds{
public:
    NumInsultsOutOfBounds(const string &);
    string& what();
private:
    string message;
};

#endif

【问题讨论】:

  • 此代码看起来与Implicitly declared function error C++ 非常相似,尽管可能不是同一个问题。
  • 是的,那是我朋友的档案。我们正在做同一个项目。不过,我们目前都有这个错误。

标签: c++ c++11 mingw


【解决方案1】:

您有一个InsultGenerator 的构造函数:InsultGenerator(const InsultGenerator &amp;);,但它有参数。你需要一个没有参数的Constructor,所以InsultGenerator t1;这个语句可以正确调用对应的Constructor。

【讨论】:

    【解决方案2】:

    因为您没有 InsultGenerator 对象的构造函数。 在头文件中添加 InsultGenerator(); 在 cpp 文件中添加

    InsultGenerator::InsultGenerator() { }
    

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多