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
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