![](/img/trans.png)
[英]Call constructor inside another constructor (no matching function to call…) c++
[英]Strange function call in constructor c++
我正在編碼一台謎題機器,並且在為機器類創建構造函數時遇到問題。 盡管必須在構造函數的參數中提供一個插件,但似乎正在調用該插件的構造函數。 這是錯誤
Machine.cpp: In constructor ‘Machine::Machine(std::list<Rotor>, Plugboard)’:
Machine.cpp:6:48: error: no matching function for call to ‘Plugboard::Plugboard()’
Machine::Machine(list<Rotor> rots, Plugboard pb) {
Machine.cpp:
#include "Machine.h"
using namespace std;
Machine::Machine(list<Rotor> rots, Plugboard pb) {
plugboard = pb;
rotors = rots;
}
//give c's alphabet index
int Machine::getPosition(char c) {
if (c >= 'A' && c <= 'Z') {
return c - 'A';
}
else {
cout << "not an accepted character";
return -1;
}
}
//give letter at index i in alphabet
char Machine::atPosition(int i) {
assert(i>=0 && i<=25);
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i];
}
char Machine::encode(char c) {
assert(c >= 'A' && c <= 'Z');
//plugboard
c = plugboard.getMatch(c);
//forward pass through rotors
c = rotors[0].process(c);
//reflector
c = Reflector::reflect(c);
//backwards pass through rotors
c = rotors[0].processInverse(c);
return c;
}
Machine.h:
#ifndef MACHINE_H
#define MACHINE_H
#include <stdexcept>
#include <iostream>
#include <assert.h>
#include <list>
#include "Reflector.h"
#include "Rotor.h"
#include "Plugboard.h"
class Machine{
public:
Machine(std::list<Rotor> rots, Plugboard pb);
static int getPosition(char c);
static char atPosition(int i);
char encode(char c);
private:
std::list<Rotor> rotors;
Plugboard plugboard;
};
#endif
那是因為在您的構造函數中,您首先是默認構造的plugboard
,然后將其復制分配。 只需在初始化列表中構造它即可。 並接受const &
!
Machine(const std::list<Rotor>& rots, const Plugboard& pb)
: rotors(rots)
, plugboard(pb)
{ }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.