namespace clarification

J

John Ratliff

I have a program, the classes that belong to it all belong to a special
namespace I created for them.

This had lead to a minor issue I don't quite understand. For some
reason, when I return a reference class in this namespace from a method
in a class in the same namespace, the compiler makes me fully qualify
the return type.

It may have something to do with forward declarations or header
inclusion order, because the small example I've made to demonstrate the
problem disappears if you put things into a single file.

Sorry I have to define multiple files, but the error only exists when I
do. There are four files.

main.cpp (the main method)
foo.h (the foo class header)
bar.h (the bar class header
bar.cpp (the bar class implementation)

---- main.cpp ----
#include "bar.h"

using namespace ns;

int main(int, char **) {
bar().method();

return 0;
}
------------------

----- foo.h ------
#ifndef _FOO_H_
#define _FOO_H_

#include <iostream>

namespace ns {
class foo {
public:
void method() { std::cout << "foo::method()\n"; }
};
}

#endif
------------------

----- bar.h ------
#ifndef _BAR_H_
#define _BAR_H_

class foo;

namespace ns {
class bar {
private:
foo *f;
foo &getFoo();
public:
bar() : f(0) {}
void method();
};
}

#endif
------------------

---- bar.cpp -----
#include "foo.h"
#include "bar.h"

using namespace ns;

foo &bar::getFoo() {
if (!f) {
f = new foo;
}

return *f;
}

void bar::method() {
getFoo().method();
}
------------------

When I compile with mingw/g++ 3.4.2, I get the following error:

$ g++ -W -Wall bar.cpp main.cpp
bar.cpp:6: error: expected constructor, destructor, or type conversion
before '&' token
bar.cpp:6: error: expected `,' or `;' before '&' token

If you change
"foo &bar::getFoo() {" to
"ns::foo &bar::getFoo() {"

The error is gone. Why?

Thanks,

--John Ratliff
 
G

Gianni Mariani

John Ratliff wrote:
....
---- bar.cpp -----
#include "foo.h"
#include "bar.h"

using namespace ns;

Can't use "use" here - bar::getFoo must be declared IN the namespace:

namespace ns {
foo &bar::getFoo() {
if (!f) {
f = new foo;
}

return *f;
}

void bar::method() {
getFoo().method();
}

} // end namespace
 
M

Marc Mutz

John said:
----- bar.h ------
#ifndef _BAR_H_
#define _BAR_H_

class foo;

namespace ns { class foo; }
namespace ns {
class bar {
private:
foo *f;
foo &getFoo();
public:
bar() : f(0) {}
void method();
};
}

#endif
------------------

Marc
 
J

John Ratliff

Marc said:
John Ratliff wrote:




namespace ns { class foo; }




Marc

Yeah, I tried this last night and it worked great. I thought it was
something to do with my forward decls, but couldn't pinpoint it.

Thanks,

--John Ratliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top