构造函数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;
}
机器.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)
{ }
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销