简体   繁体   English

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

[英](C++) No instance of constructor … matches the argument list

Just began learning C++ and i'm working on an assignment to display colored symbols and text in the console. 刚开始学习C ++,我正在做一个作业,以便在控制台中显示彩色符号和文本。 For this I am creating a pen object, however I am getting an error of 'No instance of constructor matches the argument list' however I believe I am passing the correct parameters. 为此,我正在创建一个笔对象,但是却收到错误消息“没有构造函数实例与参数列表匹配”,但是我相信我传递了正确的参数。

I've been scratching my head and stressing about this for over 2 hours now, I am hoping that someone could pick up where I have gone wrong and point me in the right direction. 我已经花了2个小时以上的时间挠挠自己的头,我希望有人能从我做错的地方找回正确的方向。

Thanks. 谢谢。

Main method: 主要方法:

//#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 header file: 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 file: 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)
{

}

The problem is the constructor takes a char but you are passing a C-style string "@" . 问题在于构造函数采用了char但是您正在传递C样式字符串"@" You need to use single-quotes to get a single char , ie '@' . 您需要使用单引号来获取单个char ,即'@'

If you have a good compiler, it should have given you an error message along the lines of "Cannot convert from const char* to char in argument 3." 如果您有一个好的编译器,它应该为您提供一条错误消息,内容为“无法在参数3中将const char *转换为char”。

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

"@" is a const char[2] which you try to pass in as a char argument. "@"const char[2] ,您尝试将其作为char参数传递。

You probably meant '@' : 您可能的意思是'@'

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

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

相关问题 C++ 没有构造函数的实例与参数列表匹配 - C++ no instance of constructor matches the argument list 如何修复 C++ 中的“没有构造函数的实例匹配参数列表”错误? - How to fix an "no instance of constructor matches argument list" error in C++? C ++模板类:没有构造函数的实例与参数列表匹配 - C++ template class: No instance of constructor matches the argument list C++ 没有构造函数实例匹配参数列表 e0289 - C++ no instance of constructor matches the argument list e0289 C++ 没有构造函数的实例匹配参数列表,使用模板 - C++ no instance of constructor matches the argument list, using templates C ++没有构造函数“ Card :: Card”的实例与参数列表匹配 - C++ No instance of constructor “Card::Card” matches the argument list C ++没有构造函数的实例与参数列表匹配。 - C++ no instance of constructor matches the argument list. 没有构造函数的实例与参数列表匹配 - No instance of constructor matches argument list 错误 E0289:没有构造函数“Movie::Movie”的实例与基本程序 (C++) 的参数列表匹配 - Error E0289: No instance of constructor "Movie::Movie" matches the argument list for a Basic Program (C++) 具有新线程的C ++调用函数,没有构造函数的实例与参数列表匹配 - C++ calling function with new thread, no instance of constructor matches argument list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM