C++ "No matching function for call"错误

标签 c++ c++11 mingw

我正在使用 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

最佳答案

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

关于C++ "No matching function for call"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207543/

相关文章:

c++ - 为什么包含 <utility> 会破坏 GCC 中的结构化绑定(bind)?

c++ - 第二次缩放 mandelbrot 设置不会让它在所需位置放大

c++ - 当从 DirectX BackBuffer 拉伸(stretch)到 WIndow 时,Windows 使用点采样

c++ - 模拟套接字错误

c++ - 如何初始化正则表达式 vector ?

c++ - operator[] const 引用二维指针的重载

c++ - 现在我们有了 std::array C 风格的数组还有什么用途?

c++ - 如何对不同的静态类进行分组?

c - 输出 unicode wchar_t 字符

eclipse - 使用 Eclipse C++ for Win7 设置 OPENCV2.3.1 的问题