如何在源文件中定义类并在头文件中声明它(而不必使用`class :: method`语法定义类方法)? [英] How to define a class in a source file and declare it in a header file (without having to define the class methods using `class::method` syntax)?

查看:639
本文介绍了如何在源文件中定义类并在头文件中声明它(而不必使用`class :: method`语法定义类方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 github上的库,其中一个头文件中包含以下内容:

I am looking at a library on github which has the following in one of the header files:

class Util
{
public:
    static void   log( const string& message );
    static void   log( const char* message );
    template<typename T>
    static inline string toString(T t) { stringstream s; s << t; return s.str(); }
};

以及源文件中的以下内容:

and the following in the source file:

void Util::log( const string& message )
{
    const string& logMessage = "[cppWebSockets] " + message;
    syslog( LOG_WARNING, "%s", logMessage.c_str( ) );
}

void Util::log( const char* message )
{
    log( string( message ) );
}

为什么当我将源文件中的上述内容替换为以下内容时,编译器会抱怨重新定义"?我以为头文件只包含声明,而源文件实际上定义了类?

Why is it that when I replace the above contents in the source file with the below the compiler complains of "redefinition"? I thought the header file only held the declaration whereas the source file actually defines the class?

class Util
{
public:
    void Util::log( const string& message )
{
    const string& logMessage = "[cppWebSockets] " + message;
    syslog( LOG_WARNING, "%s", logMessage.c_str( ) );
}

void Util::log( const char* message )
{
    log( string( message ) );
} 
}

如何使用上述样式而不是Util::log来定义类?

How to define the class using the above style instead of Util::log?

推荐答案

如果您有类似的代码块

class Util
{
    ...
}

您正在定义班级.您提供的第一个示例是定义的类和声明的头文件中的函数.源文件正在定义功能.

you are defining the class. The first sample you gave is defining the class and declaring the functions in the header file. The source file is defining the functions.

当您尝试使用括号将class Util行放在源文件中时,编译器认为您正在定义一个新类.这就是为什么您会收到错误消息.

When you tried to put the class Util line in the source file with the braces, the compiler thinks you are defining a new class. That's why you get the error.

如果仅将class Util;放在一行上,那么您将声明该类.类声明告诉您该类已存在,但不告诉您有关该类的任何信息.

If you just put class Util; on a line, then you would be declaring the class. A class declaration tells you that the class exists, but does not tell you anything about the class.

如果删除了头文件并将以下文件放入源文件中,它将进行编译,但是其他源文件将无法使用该类(因为它们将不再具有为它们定义头文件的头文件).

If you removed the header file and put the following in the source file, it would compile, but other source files would not be able to use the class (because they would no longer have the header file defining it for them).

class Util
{
public:
    void log( const string& message )
    {
        const string& logMessage = "[cppWebSockets] " + message;
        syslog( LOG_WARNING, "%s", logMessage.c_str( ) );
    }

    void log( const char* message )
    {
        log( string( message ) );
    } 
}

如果要让多个源文件使用一个类,则需要在标头中定义该类.然后,您也可以在标头中定义函数(通常出于很多原因不建议使用),或者可以使用Util::log语法在源文件中定义函数(推荐做法).

If you want to have a class be used by multiple source files, then you will need to define the class in a header. You could then define the functions inside the header as well (usually discouraged for a lot of reasons), or you could use the Util::log syntax to define the function in a source file (the recommended practice).

头文件(以及它们的使用方式)是有关C和C ++的常见投诉.这就是为什么较新的语言(如Java和C#)倾向于不使用头文件的原因.但是,您引用的库是按照应根据C ++最佳实践定义类的方式来定义此类的.

Header files (and the way they need to be used) are a common complaint about C and C++. That is why newer languages (like Java and C#) tend not to use header files. However, the library you referenced is defining this class the way classes should be defined according to C++ best practices.

这篇关于如何在源文件中定义类并在头文件中声明它(而不必使用`class :: method`语法定义类方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆