C++ C++;在构造函数中相互需要的两个类?

C++ C++;在构造函数中相互需要的两个类?,c++,class,constructor,C++,Class,Constructor,我在一个.h文件中有两个非常相似的类,它们在构造函数中需要彼此。这是一个颜色类,一个将使用无符号字符0到255作为RGB,另一个将使用浮点0.0到1.0作为RGB,我需要能够在构造函数和赋值运算符以及其他成员函数中从一个和另一个转换 Color3.h: class Color3 { public: unsigned char R, G, B; Color3() : R(0), G(0), B(0) { } Color3(unsigned cha

我在一个.h文件中有两个非常相似的类,它们在构造函数中需要彼此。这是一个颜色类,一个将使用无符号字符0到255作为RGB,另一个将使用浮点0.0到1.0作为RGB,我需要能够在构造函数和赋值运算符以及其他成员函数中从一个和另一个转换

Color3.h:

class Color3 {
    public:
    unsigned char R, G, B;
    Color3()
    : R(0), G(0), B(0) {

    }

    Color3(unsigned char r, unsigned char g, unsigned char b)
    : R(r), G(g), B(b) {

    }

    Color3(const Color3f& other)
    : R(other.R*255), G(other.G*255), B(other.B*255) {

    }
};


class Color3f {
    public:
    float R, G, B;
    Color3f()
    : R(0), G(0), B(0) {

    }

    Color3f(float r, float g, float b)
    : R(r), G(g), B(b) {

    }

    Color3f(const Color3& other)
    : R(other.R/255), G(other.G/255), B(other.B/255) {

    }
};

我可以把它们放在单独的文件中,而不需要进入一个循环(我相信这就是它的名称)包括吗?我想我知道这个问题的答案,但我想知道还有什么其他的解决方案。我希望它们在同一个文件中,但如果没有其他方法,我会将它们分开。

这些情况就是“”的用途。只需将其添加到.h文件的顶部,它将只包含一次:

#pragma once
但是,请确保它只是一个“包含”循环,而不是实际的功能循环。看起来您的构造函数在实例化时可能会导致I循环

是,如果您使用转发声明。例如:

class Color3f; // <--- Forward declaration

class Color3
{
public:
    unsigned char R, G, B;
    Color3()
        : R(0), G(0), B(0)
    {

    }
    Color3(unsigned char r, unsigned char g, unsigned char b)
        : R(r), G(g), B(b)
    {

    }

    // Can't define this yet with only an incomplete type.
    inline Color3(const Color3f& other);
};


class Color3f
{
public:
    float R, G, B;
    Color3f()
        : R(0), G(0), B(0)
    {

    }
    Color3f(float r, float g, float b)
        : R(r), G(g), B(b)
    {

    }
    Color3f(const Color3& other)
        : R(other.R/255), G(other.G/255), B(other.B/255)
    {

    }
};

// Color3f is now a complete type, so define the conversion ctor.
Color3::Color3(const Color3f& other)
        : R(other.R*255), G(other.G*255), B(other.B*255)
    {

    }
class Color3;

class Color3f;// 您可以在头文件的顶部添加另一个类的声明,以避免循环依赖问题。例如:

class Color3f; // <--- Forward declaration

class Color3
{
public:
    unsigned char R, G, B;
    Color3()
        : R(0), G(0), B(0)
    {

    }
    Color3(unsigned char r, unsigned char g, unsigned char b)
        : R(r), G(g), B(b)
    {

    }

    // Can't define this yet with only an incomplete type.
    inline Color3(const Color3f& other);
};


class Color3f
{
public:
    float R, G, B;
    Color3f()
        : R(0), G(0), B(0)
    {

    }
    Color3f(float r, float g, float b)
        : R(r), G(g), B(b)
    {

    }
    Color3f(const Color3& other)
        : R(other.R/255), G(other.G/255), B(other.B/255)
    {

    }
};

// Color3f is now a complete type, so define the conversion ctor.
Color3::Color3(const Color3f& other)
        : R(other.R*255), G(other.G*255), B(other.B*255)
    {

    }
class Color3;
Color3f.h
的顶部,对于其他类也是如此。结合
#pragma once
#ifndef
include guard,该程序可以使用拆分文件进行良好编译


但是这种类似的类实际上应该声明为使用不同模板参数专门化的同一类。例如,
Color3
Color3

请编写没有明显错误的代码,如果您自己尝试编译您编写的代码会更好。在你的类中,Color3f构造函数名为Color3sorry我修复了代码(复制>粘贴从来没有给我带来好东西)谢谢你,这太完美了:)我会在15分钟的时间限制一过就投票解决它…咳嗽。。内联。。。(如果这些都在标题中)。还有+1顺便说一句@WhozCraig:明白了。已编辑。@WhozCraig为什么内联必须存在?@SanduLiviuCatalin,因为如果它都在一个头中,并且您在类def之外实现构造函数,并将头包含在多个.cpp文件中,您的链接器将对定义的重复符号发出吠叫。如果构造函数是在.cpp文件中实现的,则不应将其标记为内联。模板化此类类会使方法混乱,因为无符号字符RGB操作与浮点RGB操作和。我曾想过这样做,但这意味着我应该为无符号字符和浮点值编写重载方法。