简体   繁体   English

C ++“呼叫没有匹配功能”错误

[英]C++ “No matching function for call” error

I'm using MinGW compiler. 我正在使用MinGW编译器。 I don't understand why I am getting an error. 我不明白为什么会出错。

Error : 错误:

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

Here is the cpp file: 这是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];
}

}

Here is the header file: 这是头文件:

#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

You have one Constructor for InsultGenerator : InsultGenerator(const InsultGenerator &); 您有一个InsultGenerator构造InsultGeneratorInsultGenerator(const InsultGenerator &); , but it has parameters. ,但有参数。 You need a Constructor without parameters, so the statement InsultGenerator t1; 您需要一个没有参数的构造函数,因此语句InsultGenerator t1; can call the corresponding Constructor correctly. 可以正确调用相应的构造方法。

Because you not have contructor for InsultGenerator object. 因为您没有InsultGenerator对象的构造InsultGenerator in header file, you add InsultGenerator(); 在头文件中,添加InsultGenerator(); and in cpp file, you add 然后在cpp文件中添加

InsultGenerator::InsultGenerator() { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM