0

Here is Libaray 1 that I would like in the namespace "MyLib"

/**
 * lib1.h
 */
#ifndef LIB1_H
#define LIB1_H

#include <iostream>
#include <fstream>
#include <vector>

namespace MyLib {
    class Lib1;
}

class Lib1 {
public:
    Lib1();
    ~Lib1();

    int some_lib1_function(int foo);

private:
    MyLib::Lib1 *lib1;

};

#endif

/**
 * lib1.cpp
 */
#include "lib1.h"

using namespace std;

Lib1::Lib1() : lib1(new MyLib::Lib1)
{

}

Lib1::~Lib1()
{

}

int Lib1::some_lib1_function(int foo)
{
    // do stuff with foo
    return foo;
}

Now here is Libaray 2 that I would like also like in namespace, however I want this lib to be able to access functions in the first lib, from within functions. See below.

/**
 * lib2.h
 */
#ifndef LIB2_H
#define LIB2_H

#include <iostream>
#include <fstream>
#include <vector>

namespace MyLib {
    class Lib2;
}

class Lib2 {
public:
    Lib2();
    ~Lib2();

    int some_lib2_function(int foo);

private:
    MyLib::Lib2 *lib2;

};

#endif

/**
 * lib2.cpp
 */
#include "lib2.h"

using namespace std;

Lib2::Lib2() : lib2(new MyLib::Lib2)
{

}

Lib2::~Lib2()
{

}

int Lib2::some_lib2_function(int foo)
{
    // do stuff with foo 
    // using function from Lib1 within this Lib2

    return Fallout::lib1->some_lib1_function(foo);
}

How could I go about doing this, the above is obvious not going to work as its just an example i tapped up for this post.

Thanks.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mike_2000_17
0

You just need to enclose everything in the namespace. As so:

/**
 * lib1.h
 */
#ifndef LIB1_H
#define LIB1_H

#include <iostream>
#include <fstream>
#include <vector>

namespace MyLib {

class Lib1 {
public:
    Lib1();
    ~Lib1();

    int some_lib1_function(int foo);

private:
    Lib1 *lib1;

};

}; //end of namespace MyLib

#endif

/**
 * lib1.cpp
 */
#include "lib1.h"

using namespace std;

namespace MyLib {

Lib1::Lib1() : lib1(new Lib1) // this will, btw, cause an infinite loop.
{

}

Lib1::~Lib1()
{

}

int Lib1::some_lib1_function(int foo)
{
    // do stuff with foo
    return foo;
}

}; //end of namespace MyLib
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.