我正在編碼一個謎機器,並有問題爲我的機器類構造函數。儘管實際上必須在構造函數的參數中提供插件板,但是看起來插件板的構造函數正在被調用。以下是錯誤在構造函數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
複製轉子列表兩次。爲什麼? – 2014-10-26 13:47:03
我不知道你指的是 – 2014-10-26 13:49:05
您的構造函數按值傳遞列表,這會導致副本。然後它將副本複製到成員變量中。爲什麼不通過const引用並防止複製? – 2014-10-26 15:59:38