Sign in to follow this  
tstrimp

C++ Classes within a namespace

Recommended Posts

tstrimp    1798
I'm having a problem getting a class to reference another class within a namespace. For example. bar.h
namespace foobar
{
    class bar
    {
        bar();
    };
}


foo.h
#include "bar.h"
namespace foobar
{
    class foo
    {
        foo(foobar::bar someBar); // error: using-declaration for non-member at class scope
    };
}


I've tried it without the namespace declaration as well but then I get "error: `bar' does not name a type". Am I using namespaces incorrectly? Any help would be greatly aprreciated.

Share this post


Link to post
Share on other sites
dragongame    538

namespace foobar
{
class bar
{
public:
bar() {}
};
};

namespace foobar
{
class foo
{
public:
bar b;
};
};

int main()
{
foobar::foo f();
return 0;
};



See the ; at the end of the namespace. Than it should work.

Share this post


Link to post
Share on other sites
Fruny    1658
No need for a ; at the end of a namespace.

tstrimp. foo is in the foobar namespace. Refer to bar as just plain bar (or, alternatively, ::foobar::bar to give an "absolute path"), not foobar::bar.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Try not making the ctors private:

namespace foobar
{
class bar
{
public:
bar();
};
}

namespace foobar
{
class foo
{
public:
foo(foobar::bar someBar); // error: using-declaration for non-member at class scope
};
}

Not sure if that will help though.

Share this post


Link to post
Share on other sites
tstrimp    1798
Quote:
Original post by Drew_Benton
Try not making the ctors private:
*** Source Snippet Removed ***
Not sure if that will help though.


Sorry, that had nothing to do with it. I just forgot to include them in the example. In the code they are public.

Share this post


Link to post
Share on other sites
tstrimp    1798
Okay time for an update. Someday I'll learn to actualy test the examples I post to make sure they demonstrate the problem I'm having. [wink] The foobar stuff above works for me and I've tracked it down to the way I'm including the files. Each of my header files have guard blocks on them to prevent them from being included more then once, then I have all the relevent files included in a general includes.h file which in this case only has 2 lines of code.

includes.h

#include "foo.h"
$include "bar.h"





And each of my classes have #include "includes.h". When I remove it and use #include "bar.h" it works. So any idea what would cause that? To see a tested and broken example here are the files.

Edit: If I remove the .cpp files from the project it compiles. [help]

Share this post


Link to post
Share on other sites
Fruny    1658
Again, the contents of your test.zip compile without any modification in both VC 7.1 and GCC 3.4

What compiler are you using?

Share this post


Link to post
Share on other sites
tstrimp    1798
Yep, I'm dyslexic. [wink] I just upgraded to 3.4.4 and it still doesn't compile. I checked the build options to make sure there wasn't anything unusual in there and it was all set to the defaults.

Here is the zip that contains the project file too. Won't build under a clean install of code::blocks. [wow] Any ideas?

Share this post


Link to post
Share on other sites
Kimeli    139
Quote:
Original post by tstrimp
Doesn't compile with the Visual C++ 2003 toolkit under code::blocks either, unless I remove the .cpp files from the project.


-Remove #include "lwg.h" from lwg_task.h (there is no reason why this should be here)
-Replace #include "lwg.h" from lwg_kernel with #include "lwg_task.h"

Notes!

- Use include guards (in headers) for example:

#ifndef _FOOBAR_H_
#include "foobar.h"
#endif

- Use forward declarations (when possible)

class Foobar;

- Dont include anything useless

Share this post


Link to post
Share on other sites
tstrimp    1798
Quote:
Original post by Kimeli
-Remove #include "lwg.h" from lwg_task.h (there is no reason why this should be here)
-Replace #include "lwg.h" from lwg_kernel with #include "lwg_task.h"


There isn't a reason now. But there might be later. Is there not a way to have an all inclusive includes header for the projet?

Quote:

Notes!

- Use include guards (in headers) for example:

#ifndef _FOOBAR_H_
#include "foobar.h"
#endif


If you look at the source download then you would notice I am using them. Also
Quote:
Original post by tstrimp
Each of my header files have guard blocks on them to prevent them from being included more then once


Quote:

- Use forward declarations (when possible)

class Foobar;

- Dont include anything useless


This has nothing to do with it. The only problem is within the namespace. If I take them out of the namespace there isn't any problems.

Share this post


Link to post
Share on other sites
Kimeli    139
Your problematic file is lwg_task.cpp.
Just follow the includes and You will see why it dont compile.

lwg_task.cpp:
#include "lwg_task.h"

lwg_task.h:
#ifndef _LWG_TASK_H
#define _LWG_TASK_H

#include "lwg.h"

lwg.h:

#include "lwg_task.h"

lwg_task.h:
#ifndef _LWG_TASK_H <<< already defined so it skips Task class declaration etc.

lwg.h:

#include "lwg_kernel.h"

lwg_kernel.h:
[clip]
private:
lwg::Task t; <<< this one is not yet declared
[/clap]

This has nothing to do with namespaces. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this