The special rules for inline functions require that they be defined in each file in which
they are used.The easiest way to make sure that inline definitions are available to all files
in a multifile program is to include the inline definition in the same header file in which
the corresponding class is defined.

please write a sample code to make me understand the above point.
regards,
ravi

You may also like to see this:

http://www.parashift.com/c++-faq/inline-functions.html

Here is an example that you can see where 'inline' might be appropriately placed and used ...

file: nvector.h

// file: nvector.h //

#ifndef NVECTOR_H
#define NVECTOR_H

#include <iostream>
#include <vector>

class Nvector
{
private:
    std::vector < double > vec;
public:
    Nvector( size_t n=0 ) ;
    Nvector( const std::vector< double >& ) ;

    void resize( size_t n ) { vec.resize(n) ; } // inline //
    void reserve( size_t n ) { vec.reserve(n) ; }

    double& operator [] ( size_t i ) { return vec[i]; }
    const double& operator [] ( size_t i ) const { return vec[i]; }

    typedef std::vector< double >::iterator iterator;
    typedef std::vector< double >::const_iterator const_iterator;

    iterator begin() { return vec.begin(); }
    iterator end() { return vec.end(); }
    const_iterator begin() const { return vec.begin(); }
    const_iterator end() const { return vec.end(); }

    size_t size() const  { return vec.size(); } // inline //

    Nvector& reverse();

    Nvector& operator += ( const Nvector& ) ;
    Nvector operator + ( const Nvector& ) const ;

    Nvector operator - () const;

    Nvector& operator *= ( double scale ) ;
    Nvector operator * ( double scale ) const ;

    double operator * ( const Nvector& ) const ;

    bool operator == ( const Nvector& ) const ;

    void print( std::ostream& ) const ;
} ;

inline
Nvector operator * ( double scale, const Nvector& nv )
{
    return nv * scale;
}

std::ostream& operator << ( std::ostream& os, const Nvector& nv );

#endif

file: nvector.cpp

// file: nvector.cpp //

#include "nvector.h"

std::ostream& operator << ( std::ostream& os, const Nvector& nv )
{
    nv.print( os );
    return os;
}

Nvector::Nvector( size_t n ) { vec.resize(n); }
Nvector::Nvector( const std::vector< double >& v ) : vec(v) {}

Nvector& Nvector::operator += ( const Nvector& nv )
{
    if( vec.size() == nv.size() )
    {
        for( size_t i = 0; i < vec.size(); ++ i )
             vec[i] += nv.vec[i];
    }
    else
        std::cerr << "\nError different sizes ..."
                  <<"NOT added!\n";
    return *this;
}

Nvector Nvector::operator + ( const Nvector& nv ) const
{
    Nvector tmp(vec);
    return tmp += nv;
}

Nvector& Nvector::operator *= ( double scale )
{
    for( size_t i = 0; i < vec.size(); ++ i )
         vec[i] *= scale;
    return *this;
}

Nvector Nvector::operator - () const
{
    Nvector tmp(vec);
    return tmp * -1;
}

Nvector Nvector::operator * ( double scale ) const
{
    Nvector tmp(vec);
    return tmp *= scale;
}

double Nvector::operator * ( const Nvector& nv ) const
{
    double sum = 0;
    if( vec.size() == nv.size() )
    {
        for( size_t i = 0; i < vec.size(); ++ i )
             sum += vec[i] * nv[i];
    }
    else
        std::cerr << "\nError! No size match ... "
                  << "NO dot product ... returning 0 ...\n";
    return sum;
}

bool Nvector::operator == ( const Nvector& nv ) const
{
    if( vec.size() != nv.size() ) return false;
    return vec == nv.vec;
}

void Nvector::print( std::ostream& os ) const
{
    if( vec.size() )
    {
        os << '(' << vec[0];
    }
    for( size_t i = 1; i < vec.size(); ++ i )
         os << ", " << vec[i];
    if( vec.size() )
    {
        os << ')';
    }
}

Nvector& Nvector::reverse()
{
    int bot = 0, top = vec.size() - 1;
    double sav;
    while( top > bot )
    {
        sav =  vec[bot];
        vec[bot] = vec[top];
        vec[top] = sav;
        --top, ++bot;
    }
    return *this;
}

file: test_Nvector.cpp

// file: test_Nvector_addition.cpp //

#include "nvector.h"

int main()
{
    using std::vector; using std::cout; using std::endl;
    using std::flush; using std::cin;

    double dary1[4] = { 1,2,3, 4 }, dary2[] = { 4, 3, 2, 1 };

    Nvector nv1( vector< double >( dary1, dary1+4 ));
    Nvector nv2( vector< double >( dary2, dary2+4 ));

    cout << "nv1 + nv2         = "
         << nv1 << " + " << nv2 <<  " = "
         << nv1+nv2 << endl;
    cout << nv1 << " *= " << "2 = ";
    cout << (nv1 *= 2) << endl;
    cout << nv1 << " *  2" << " = " << nv1*2 << endl;
    cout << " 2 * " << nv1 << " = " << 2*nv1 << endl << endl;

    cout << "(v1               = " << nv1 << endl;
    cout << "nv1.reverse()     = " << nv1.reverse()
         << endl << endl;

    cout << "(-nv1 * (-nv2))   = "
         << -nv1 << " * " << -nv2
         << " = " << -nv1*(-nv2)
         << endl << endl;

    cout << "(nv1 == nv2)      = "
         << ( nv1 == nv2 ? "true" : "false" ) << endl;

    cout << "(nv1 == nv1)      = "
         << ( nv1 == nv1 ? "true" : "false" ) << endl;

    cout << "\nPress 'Enter' to continue/exit " << flush;
    cin.get();
}

@David W: The code you provided doesn't address the OPs concerns and the relative use of inline compared to the example size is very small. It might be helpful if you condensed the example to a very small class and taylored the code to address the 'multiple files' problem the OP is trying to understand.

@L7Sqr I appreciate your concern ... the example I provided was an example of a 'real' use ... in a typical student type coding problem / solution. The example only used one case of 'inline' ... and this also illustrated the relative raity of its (possibly appropriate?) use ... so now you may be able to see more of the 'intent' ...

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.