2014-09-02 59 views
-4

看起来好像当我试图为freeglut api编写自己的包装时偶然发现了一些奇怪的事情。基本上,我正在编写我自己的小型库,以便更容易地使用freeglut。我正在做的第一件事情之一是试图实现我自己的Color类,它将被馈送到“glClearColor”中。我也拥有它,以便您可以手动输入颜色;这意味着我将拥有多个具有相同名称但参数/参数不同的静态方法。之后我试图编译这个文件,但收到一个错误,让我认为编译器不能决定使用哪种方法 - 考虑到这两种方法仍然不同,这很奇怪。一个采用Color3类,另一个采用Color4。C++具有相同名称但具有不同参数的多个静态函数

下面是一些来源:

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(); 
}; 
+6

它应该是'color.getRed()'而不是'color.getRed'吗? – 2014-09-02 18:59:40

+3

第一个错误消息毫不含糊地告诉你'Color3 *'和'const Color3'是两个不同的东西。 – 2014-09-02 19:06:55

+0

http://postimg.org/image/jgclkszxh/ – Krythic 2014-09-02 19:07:09

回答

2

那么首先,你传递一个COLOR3 指针成重载函数,采用两种不同的引用

你有多种选择:

  • 没有传递指针(你有main()Color3 color,不传&color,通过color
  • 去参考指针传递参考(具有在main()一个Color3* color,通*colorcolor
  • 更改方法或添加一个接受Color3指针。这是愚蠢的,我不建议。但是你可以!

我也知道这是不是问题的一部分,但它似乎getRedgetGreengetBlue,你应该追加()的方法。

+0

这工作。标记为答案;非常感谢你。 – Krythic 2014-09-02 19:24:42

+0

+1实际回答这个*******问题。 – Walter 2014-09-02 19:37:24

相关问题