Skip to main content
deleted 5 characters in body
Source Link
timemage
  • 5.7k
  • 1
  • 15
  • 27

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

Making a function inline has a second effect beyond implying that you want a function's code to be generated at the call site. That is to informsite; it informs the compiler (or more accurately the linker) to expect to see multiple definitions (that are required to be identical) of the code for the function. Without that advanced noticed, you will get the multiple definition of error when a function definition is inserted into multiple .cpp file by way of an #include.

As a side note: #include is an automated form of copy-and-paste. It logically plants the contents of another file (and all of its reachable #include uses transitively) into place where the #include directive appeared. It does not behave the same way that many similar looking directives in other languages do. E.g. #include does not behave like the import keyword does in say Java or python for example.

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

Making a function inline has a second effect beyond implying that you want a function's code to be generated at the call site. That is to inform the compiler (or more accurately the linker) to expect to see multiple definitions (that are required to be identical) of the code for the function. Without that advanced noticed, you will get the multiple definition of error when a function definition is inserted into multiple .cpp file by way of an #include.

As a side note: #include is an automated form of copy-and-paste. It logically plants the contents of another file (and all of its reachable #include uses transitively) into place where the #include directive appeared. It does not behave the same way that many similar looking directives other languages do. E.g. #include does not behave like the import keyword does in say Java or python for example.

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

Making a function inline has a second effect beyond implying that you want a function's code to be generated at the call site; it informs the compiler (or more accurately the linker) to expect to see multiple definitions (that are required to be identical) of the code for the function. Without that advanced noticed, you will get the multiple definition of error when a function definition is inserted into multiple .cpp file by way of an #include.

As a side note: #include is an automated form of copy-and-paste. It logically plants the contents of another file (and all of its reachable #include uses transitively) into place where the #include directive appeared. It does not behave the same way that many similar looking directives in other languages do. E.g. #include does not behave like the import keyword does in say Java or python for example.

Expanded explanation regarding the practical effects of inline functions and the nature of #include vs similar looking non-macro-based derectives in other languages.
Source Link
timemage
  • 5.7k
  • 1
  • 15
  • 27

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

Making a function inline has a second effect beyond implying that you want a function's code to be generated at the call site. That is to inform the compiler (or more accurately the linker) to expect to see multiple definitions (that are required to be identical) of the code for the function. Without that advanced noticed, you will get the multiple definition of error when a function definition is inserted into multiple .cpp file by way of an #include.

As a side note: #include is an automated form of copy-and-paste. It logically plants the contents of another file (and all of its reachable #include uses transitively) into place where the #include directive appeared. It does not behave the same way that many similar looking directives other languages do. E.g. #include does not behave like the import keyword does in say Java or python for example.

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

Making a function inline has a second effect beyond implying that you want a function's code to be generated at the call site. That is to inform the compiler (or more accurately the linker) to expect to see multiple definitions (that are required to be identical) of the code for the function. Without that advanced noticed, you will get the multiple definition of error when a function definition is inserted into multiple .cpp file by way of an #include.

As a side note: #include is an automated form of copy-and-paste. It logically plants the contents of another file (and all of its reachable #include uses transitively) into place where the #include directive appeared. It does not behave the same way that many similar looking directives other languages do. E.g. #include does not behave like the import keyword does in say Java or python for example.

Clarification with regarding inline function defined within the class definition. Fixed case of Fooa to match with code.
Source Link
timemage
  • 5.7k
  • 1
  • 15
  • 27

If you want fooaFooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition right in thedirectly inside class definitiondefinition; this also makes the function inline despite not requiring the inline keyword. Or

Or if you want to keep itFooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with foobFoob().

If you want fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately provide the definition right in the class definition. Or if you want to keep it as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with foob().

If you want Fooa() to be defined in a header that can be included in more than one .cpp file, you would need to declare it as an inline function like so:

#ifndef Foo_H
#define Foo_H

class Foo
{
    public:
    Foo();

    public:
    inline bool Fooa();
    bool Foob();
};
#endif

Alternately, you can provide the function definition directly inside class definition; this also makes the function inline despite not requiring the inline keyword.

Or if you want to keep Fooa as a non-inline function, you'd need to move the definition out of the header and into a .cpp file as you did with Foob().

Source Link
timemage
  • 5.7k
  • 1
  • 15
  • 27
Loading