使用重载方法时,Visual Studio错误C2664

Visual Studio error C2664 when using overloaded methods

本文关键字:Visual Studio 错误 C2664 重载 方法      更新时间:2023-10-16

我有一个类来管理钢笔,颜色集合和图纸:NRGraphics:

class NRGraphics
{
    ...
    void newPen(std::string name, float epaisseur, Color couleur, DashStyle style);
    void newPen(std::string name, float epaisseur, std::string colorName, DashStyle style);
    ...
}

这些是唯一具有该名称的方法。在某些函数中,我使用的最多的是第二个。

今天我正在写一个新类,我不得不使用第二个方法,所以我写的是:

void NRCell::draw(NRGraphics * drawer)
{
    switch(backgroundStyle)
    {
        case Solid:
            std::string colorName;
            std::string borderPen;
            std::string borderColorName = "pas de couleur";
            try
            {
                colorName = drawer->findColor(color->Red(), color->Green(), color->Blue(), color->Alpha());
            }
            catch(std::exception e)
            {
                colorName  = "R";
                colorName += color->Red();
                colorName += "G";
                colorName += color->Green();
                colorName += "B";
                colorName += color->Blue();
                colorName += "A";
                colorName += color->Alpha();
                drawer->newColor(colorName, color->Red(), color->Green(), color->Blue(), color->Alpha());
            }
            try
            {
                borderColorName = drawer->findColor(
                borderColor->Red(),
                borderColor->Green(),
                borderColor->Blue(),
                borderColor->Alpha());
                borderPen = drawer->findPen(borderColorName, borderSize);
            }
            catch(std::exception e)
            {
                if(borderColorName == "pas de couleur")
                {
                borderColorName  = "R";
                borderColorName += borderColor->Red();
                borderColorName += "G";
                borderColorName += borderColor->Green();
                borderColorName += "B";
                borderColorName += borderColor->Blue();
                borderColorName += "A";
                borderColorName += borderColor->Alpha();
                drawer->newColor(
                  borderColorName,
                  borderColor->Red(),
                  borderColor->Green(),
                  borderColor->Blue(),
                  borderColor->Alpha());
                }
                borderPen  = "Pen_color_";
                borderPen += borderColorName;
                borderPen += "_size_";
                borderPen += borderSize;
                drawer->newPen(borderPen, (float)borderSize, borderColorName, 0);
            }
            drawer->DrawFilledSolidRectangle(
                rect->getPt1()->X(), rect->getPt1()->Y(),
                rect->getPt2()->X(), rect->getPt2()->Y(),
                colorName,
                borderPen,
                borderSize
            );
        break;
    /*case GradientHorizontal:
    break;
    case GradientVertical:
    break;*/
    }
}

下面是Visual Studio编译时输出的内容:

1>c:usersmd2idesktopcurrentbranchdevelsourcesnrgraphicscomponents.cpp(90): error C2664: 'void NRGraphics::newPen(std::string,float,Gdiplus::Color,Gdiplus::DashStyle)' : impossible de convertir le paramètre 3 de 'std::string' en 'Gdiplus::Color'
1>          Aucun opérateur de conversion définie par l'utilisateur disponible qui puisse effectuer cette conversion, ou l'opérateur ne peut pas être appelé

错误信息的自动英文翻译:

1>c:usersmd2idesktopcurrentbranchdevelsourcesnrgraphicscomponents.cpp(90): error C2664: 'void NRGraphics::newPen(std::string,float,Gdiplus::Color,Gdiplus::DashStyle)' : can not convert parameter 3 from 'std::string' to 'Gdiplus::Color'
1>          No conversion operator defined by the user available that can perform this conversion, or the operator can not be called

我有点困惑,因为当我使用Visual Studio自动完成时,它会显示我所有重载的方法。

我不知道我做错了什么。

谢谢你的帮助

好了,我的问题解决了。我把答案贴出来,可能会有帮助。

问题出在最后一个参数。VS编译器不能将其转换为Gdiplus::DashStyle(这是一个枚举),所以它报告一个错误。但不是正确的错误。

所以我只需要将最后一个值转换为Gdiplus::DashStyle,它就可以工作了。


Gdiplus::DashStyle的更多信息:http://msdn.microsoft.com/en-us/library/ms534104%28v=vs.85%29.aspx