(C++) 没有构造函数的实例 ... 与参数列表匹配

(C++) No instance of constructor ... matches the argument list

刚开始学习 C++,我正在做一项在控制台中显示彩色符号和文本的作业。为此,我正在创建一个笔对象,但是我收到 'No instance of constructor matches the argument list' 错误,但我相信我传递了正确的参数。

我已经挠头并为此强调了 2 个多小时,我希望有人能指出我出错的地方并指出正确的方向。

谢谢。

主要方法:

//#include "stdafx.h"
#include <iostream>
#include "tui.h"

using namespace std;
using namespace textUserInterface;

int main()
{

cout << "Testing..." << endl;

byte testColor = color::red;
pen myPen(color::red, color::black, "@", true); //Error on this construction

return 0;
}

Tui头文件:

#ifndef TUI_H
#define TUI_H

typedef unsigned char byte;

namespace textUserInterface {

class tui
{
public:
    tui();
protected:
private:
};

class color
{
public:
    static const byte magenta = 5;
    static const byte yellow = 3;
    static const byte red = 1;
    static const byte white = 7;
    static const byte green = 2;
    static const byte blue = 4;
    static const byte black = 0;
    static const byte system_default = 0;
private:
    color();
};

class pen
{
public:
    pen(byte _foreground, byte _background, char _character, bool _isBright);
private:
    byte foreground;
    byte background;
    char character;
    bool isBright;
};
}
#endif

Tui cpp 文件:

#include <iostream>
#include "tui.h"

using namespace std;
using namespace textUserInterface;

tui::tui()
{

}

pen::pen(byte _foreground, byte _background, char _character, bool _isBright)
: foreground(_foreground), background(_background), character(_character), isBright(_isBright)
{

}

问题是构造函数采用 char 但您传递的是 C 风格的字符串 "@"。您需要使用单引号来获得单个 char,即 '@'.

如果您有一个好的编译器,它应该会按照 "Cannot convert from const char* to char in argument 3."

的行给您一条错误消息
pen myPen(color::red, color::black, "@", true);

"@" 是一个 const char[2],您尝试将其作为 char 参数传入。

你的意思可能是 '@':

pen myPen(color::red, color::black, '@', true);