Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


C++ Classes within a namespace


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 05:40 PM

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.

Sponsor:

#2 dragongame   Members   -  Reputation: 538

Like
0Likes
Like

Posted 18 September 2005 - 06:16 PM


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.

#3 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 06:20 PM

Same problem. And all the examples/documentation I see shows namespaces without the semi-colon at the end.

#4 Fruny   Moderators   -  Reputation: 1653

Like
0Likes
Like

Posted 18 September 2005 - 06:23 PM

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.

#5 Drew_Benton   Crossbones+   -  Reputation: 1719

Like
0Likes
Like

Posted 18 September 2005 - 06:24 PM

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.

#6 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 06:26 PM

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.

#7 Fruny   Moderators   -  Reputation: 1653

Like
0Likes
Like

Posted 18 September 2005 - 06:28 PM

Actually both VC 7.1 and GCC 3.4 accept that code...

What compiler are you using?

#8 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 07:00 PM

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]

#9 Fruny   Moderators   -  Reputation: 1653

Like
0Likes
Like

Posted 18 September 2005 - 07:08 PM

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?

#10 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 07:11 PM

I'm using GCC 4.3.2 that comes with Code::Blocks.

#11 Fruny   Moderators   -  Reputation: 1653

Like
0Likes
Like

Posted 18 September 2005 - 07:14 PM

You probably meant 3.4.2 - there is no 4.3.

I'm using 3.4.4 (with code::blocks, too).

#12 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 07:24 PM

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?

#13 Fruny   Moderators   -  Reputation: 1653

Like
0Likes
Like

Posted 18 September 2005 - 07:32 PM

I'm just creating an ordinary C++ console project and adding your files to it... and it compiles.

#14 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 18 September 2005 - 07:43 PM

Doesn't compile with the Visual C++ 2003 toolkit under code::blocks either, unless I remove the .cpp files from the project.

#15 Kimeli   Members   -  Reputation: 139

Like
0Likes
Like

Posted 18 September 2005 - 10:56 PM

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


#16 tstrimple   Prime Members   -  Reputation: 1724

Like
0Likes
Like

Posted 19 September 2005 - 04:36 AM

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.


#17 Kimeli   Members   -  Reputation: 139

Like
0Likes
Like

Posted 19 September 2005 - 10:39 PM

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. :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS