C ++具有相同名称但不同参数的多个静态函数

时间:2014-09-02 18:57:34

标签: c++

在尝试为freeglut api编写自己的包装时,似乎偶然发现了一些奇怪的东西。基本上,我正在编写自己的小库,以便更轻松地使用freeglut。我正在做的第一件事就是尝试实现我自己的Color类,它将被输入" glClearColor"。我也有它,所以你可以手动输入颜色;这意味着我将拥有多个具有相同名称但不同参数/参数的静态方法。我之后试图编译它,但收到一个错误,让我认为编译器无法决定使用哪种方法 - 考虑到两种方法仍然不同,这是奇怪的。一个是Color3类,另一个是Color4。

以下是一些消息来源:

GL.H

#pragma once
#include "Vector3.h"
#include "Color3.h"
#include "Color4.h"
#include <string>
class GL
{
public:
    static void initializeGL(int argc, char* argv);
    static void initializeDisplayMode(unsigned int displayMode);
    static void initializeWindowSize(int width, int height);
    static void createWindow(std::string title);
    static void mainLoop();
    static void translate(const Vector3 &location);
    static void translate(float x, float y, float z);
    static void rotate(double rotation, float x, float y, float z);
    static void rotate(double rotation, const Vector3& axis);
    static void color3(const Color3 &color);
    static void color4(const Color4 &color);
    static void begin();
    static void end();
    static void pushMatrix();
    static void popMatrix();
    static void enable(int enableCap);
    static void viewport();
    static void polygonMode();
    static void matrixMode();
    static void clearColor(const Color3 &color);
    static void clearColor(float red, float green, float blue);
    static void clearColor(float red, float green, float blue, float alpha);
    static void clearColor(const Color4 &color);
    static void vertex3(const Vector3 &location);
    static void vertex3(float x, float y, float z);
    static void loadIdentity();
    static void perspective();
    static void depthFunction();
};

GL.cpp

#include "GL.h"
#include "freeglut.h"



void GL::clearColor(const Color3 &color)
{
    glClearColor(color.getRed,color.getGreen,color.getBlue, 1.0f);
}

void GL::clearColor(float red, float green, float blue)
{
    glClearColor(red, green, blue, 1.0f);
}

void GL::clearColor(float red, float green, float blue, float alpha)
{

}

void GL::clearColor(const Color4 &color)
{

}

这是我的编译器错误:

1>------ Build started: Project: GameEngineToolkit, Configuration: Debug Win32 ------
1>  Main.cpp
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\main.cpp(47): error C2665: 'GL::clearColor' : none of the 4 overloads could convert all the argument types
1>          c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.h(610): could be 'void GL::clearColor(const Color4 &)'
1>          c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.h(607): or       'void GL::clearColor(const Color3 &)'
1>          while trying to match the argument list '(Color3 *)'
1>  GL.cpp
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.cpp(8): error C3867: 'Color3::getRed': function call missing argument list; use '&Color3::getRed' to create a pointer to member
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.cpp(8): error C3867: 'Color3::getGreen': function call missing argument list; use '&Color3::getGreen' to create a pointer to member
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.cpp(8): error C3867: 'Color3::getBlue': function call missing argument list; use '&Color3::getBlue' to create a pointer to member
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

正如您所看到的,似乎编译器无法决定使用Color3函数还是使用color4函数;我不明白为什么,因为它应该是明显的选择(Color3是我在我的主要使用的那个)。

根据要求,这是我的Color3类:

Color3.h

#pragma once
class Color3
{
public:
    Color3();
    Color3(float red, float green, float blue);
    void setRed(float red);
    void setGreen(float green);
    void setBlue(float blue);
    float getRed();
    float getGreen();
    float getBlue();
    Color3 getColor();
    ~Color3();
private:
    float red;
    float green;
    float blue;
};

Color3.cpp

#include "Color3.h"


Color3::Color3()
{
}

Color3::Color3(float red, float green, float blue)
{
    setRed(red);
    setGreen(green);
    setBlue(blue);
}


Color3::~Color3()
{
}

float Color3::getRed()
{
    return red;
}

float Color3::getGreen()
{
    return green;
}

float Color3::getBlue()
{
    return blue;
}

void Color3::setBlue(float blue)
{
    this->blue = blue;
}

void Color3::setGreen(float green)
{
    this->green = green;
}

void Color3::setRed(float red)
{
    this->red = red;
}

Color3 Color3::getColor()
{
    return *this;
}

解决方案:

使用指针。

GL.cpp

#include "GL.h"
#include "freeglut.h"



void GL::clearColor(Color3* color)
{
    glClearColor(color->getRed(),color->getGreen(),color->getBlue(), 1.0f);
}

void GL::clearColor(float red, float green, float blue)
{
    glClearColor(red, green, blue, 1.0f);
}

void GL::clearColor(float red, float green, float blue, float alpha)
{

}

void GL::clearColor(Color4* color)
{

}

GL.H

#pragma once
#include "Vector3.h"
#include "Color3.h"
#include "Color4.h"
#include <string>

class GL
{
public:
    static void initializeGL(int argc, char* argv);
    static void initializeDisplayMode(unsigned int displayMode);
    static void initializeWindowSize(int width, int height);
    static void createWindow(std::string title);
    static void mainLoop();
    static void translate(const Vector3 &location);
    static void translate(float x, float y, float z);
    static void rotate(double rotation, float x, float y, float z);
    static void rotate(double rotation, const Vector3& axis);
    static void color3(const Color3 &color);
    static void color4(const Color4 &color);
    static void begin();
    static void end();
    static void pushMatrix();
    static void popMatrix();
    static void enable(int enableCap);
    static void viewport();
    static void polygonMode();
    static void matrixMode();
    static void clearColor(Color3* color); // Use pointers instead
    static void clearColor(float red, float green, float blue);
    static void clearColor(float red, float green, float blue, float alpha);
    static void clearColor(Color4* color); // Same thing; no error. =P
    static void vertex3(const Vector3 &location);
    static void vertex3(float x, float y, float z);
    static void loadIdentity();
    static void perspective();
    static void depthFunction();
};

1 个答案:

答案 0 :(得分:2)

首先,您将Color3 指针传递给一个重载函数,该函数需要两个不同的引用

您有多种选择:

  • 不要传递指针(Color3 color中有main(),不传递&color,传递color
  • 取消引用指针以传递引用(Color3* color中有main(),传递*color color
  • 更改方法或添加一个以接受Color3指针。这是愚蠢的,我不建议。但你可以!

此外,我知道这不是问题的一部分,但它显示为getRedgetGreengetBlue是您应该()追加的方法。