header.h
class Foo{
public:
void fooFunction(std::map <std::string, Bar> &);
}
class Bar{
public:
void barFunction(std::map <std::string, Foo> &, Foo &);
}
When I try to compile this, I get an error saying Bar is not declared in the scope of fooFunction, and when I switch the order of the declarations, I get an error saying Foo is not in the scope of barFunction.
How can I make them be in the scope of each other? If I need multiple header files, how should I set that up in my makefile and #include s?
You can simply forward-declare the other class in the header that requires it:
class Bar;
class Foo
{
public:
void fooFunction(std::map <std::string, Bar>&);
};
class Bar
{
public:
void barFunction(std::map<std::string, Foo>&, Foo &);
};
This works provided that Foo does not use Bar by value anywhere until after Bar has been defined. That means storing it as a member, passing as parameter, returning value of that type. In this case you're fine, since the Foo parameters are passed by reference.
Note that even though your std::map stores type Bar by value, this is still okay because it's a template and is not resolved until you use it, at which time Bar will be defined.
Related
I'm getting this strange problem which I don't know why happens. The first and second of the following code snippets compile, while the third does not:
Compiles:
class Foo {
public:
Foo() { Bar(); }
private:
class Bar {};
};
Compiles:
class Foo {
class Bar {}; // Or only forward declare here and define later
public:
Foo(Bar) {}
}
Does not compile:
class Foo {
public:
Foo(Bar) {}
private:
class Bar {};
};
What makes the third fail to compile while the first can?
Normally, in C++, you can only reference declarations that were previously made in the translation unit. However, within a class definition, the definition of member functions are allowed to reference declarations which are made later in the class. Basically, the compiler restructures your in-class definitions so that they work as though they were written just after the class.
But this is only true of the function definitions. The declaration of the function (including parameter types) isn't allowed to do this. They can only reference declarations that have already been made in file order.
So you can do this:
class Test
{
public:
void Func(int x) {Inner foo;}
private:
class Inner {};
};
But not this:
class Test
{
public:
void Func(Inner x) {}
private:
class Inner {};
};
First example does not expose anything about private Bar to the outside, while third does.
Third example is pretty much saying, that there exist some class Foo, which has constructor with single argument of type Bar. But Bar is unknown to the outside. Imagine calling such constructor.
Foo f{Foo::Bar{}};
Will result probably in something like Foo::Bar is inaccessible.
So I have these two classes: Foo and Bar.
//forward declaration for Bar
class Bar;
typedef enum Type { TYPE1, TYPE2, TYPE3 };
// My class Foo
class Foo
{
public:
explicit Foo(Type fooType)
:mType(fooType)
{}
Type getFooType() const
{
return mType;
}
inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'
private:
Type mType;
};
// My class Bar
class Bar
{
public:
explicit Bar(Type barType)
:mType(barType)
{}
Type getBarType() const
{
return mType;
}
private:
Type mType;
};
Now somewhere in my code, I want to compare two instances (one from the Foo and other from Bar class).
Like this:
if(myBar->getBarType() == myFoo->getFooType())
{
//...
}
QUESTION:
I know that I need to implement operator== to allow this comparation.
So I've done as seen above...
And I get that error, despite the fact that I've made the forward declaration.
What am I missing here that allows me to compare using the operator== on both classes?
You need to define your operator== after the Bar class definition, rather than in the Foo class. Declare it in the class, but define it outside.
inline Foo::operator==(const Bar &bar) const { ... }
This won't quite help with your test above, since you have the Bar element on the left and the Foo on the right, so the operator== in Foo won't be considered. Defining symetric global operator== functions would be useful.
inline bool operator==(const Bar &bar, const Foo &foo) { return foo == bar; }
You say you want this comparison to work...
if(myBar->getBarType() == myFoo->getFooType())
That's comparing the enum Type values returned by the getXType functions, not the Foo and Bar objects themselves, and enums are comparable by default so you don't need to provide your own operator==.
Never-the-less, you have tried to do so, and in...
inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'
...the problem is that Bar is not defined at the place this function definition appears in the translation unit. You could just declare the function...
inline bool operator==(const Bar& bar);
...then define it later in the translation unit, after the definition of class Bar.
This would still only allow you to omit the explicit getFooType() call when comparing with a Foo as the left-hand-side argument and a Bar at right. To support the other ordering you asked for, you'd need a similar operator inside Bar that worked with a const Foo&.
One more detail... you say...
And I get that error, despite the fact that I've made the forward declaration.
A forward declaration just lets the compile know that Bar is a class, it doesn't tell the compiler that Bar contains a Type getBarType() member function, and that knowledge is needed to compile your operator==.
The more I program, the more I like the snake_case and the less the CamelCase. There is one particular inconvenience irritating me a bit: I do not like getters start from the get_ prefix, but that is not always possible without some extra work when using the snake_case. Consider, for instance, a getter for a member of enum type defined inside a class,
class bar
{
public:
enum class foo { a, b };
foo foo() const { return m_foo; }
void set_foo(foo new_foo) { m_foo = new_foo; }
private:
foo m_foo;
};
This won't compile since (if I understand correctly) enum foo is hidden in both the setter new_foo parameter type and the private m_foo member declaration by the prior foo() getter declaration. The obvious fix is to name the getter as get_foo, but as I said, I don't like it.
When I compile with Clang it suggests another "workaround":
error: must use 'enum' tag to refer to type 'foo' in this scope
void set_foo(foo new_foo) { m_foo = new_foo; }
^
enum
and the same for m_foo member. The following code indeed compiles fine
class bar
{
public:
enum class foo { a, b };
foo foo() const { return m_foo; }
void set_foo(enum foo new_foo) { m_foo = new_foo; }
private:
enum foo m_foo;
};
It looks like the elaborated type specifiers are even supposed to solve such kind of problems:
Elaborated type specifiers may be used to refer to a
previously-declared class name (class, struct, or union) or to a
previously-declared enum name even if the name was hidden by a
non-type declaration.
But is not that a misuse of a language feature? Have you ever seen anything like that in libraries that use snake_case, such as, for instance, the Standard Library or Boost, or write it yourself?
I have a class defined as follows:
class Foo {
private:
boolean feature;
public:
Foo(boolean feature) : feature(feature) {}
// ...
};
I'm trying to construct an instance, as a private property of another class:
class Bar {
private:
Foo foo(true);
// ...
};
This doesn't work. I get expected identifier before numeric constant on the line with the declaration. When I remove the parameter from Foo's constructor definition simply and ask for a Foo foo;, it works.
Why?
How do I define and declare an instance of Foo that takes a boolean parameter?
You can't use that initialisation syntax in a class member declaration; you can only initialise members with {} or =. The following should work (assuming support for C++11 or later):
Foo foo{true};
Foo foo = Foo(true);
The pre-C++11 way to do this is:
class Bar {
public:
Bar() : foo(true){} //initialization
private:
Foo foo; //no parameter
};
Bonus:
class Bar {
private:
Foo foo(); //<- This is a function declaration for a function
//named foo that takes no parameters returning a Foo.
//There is no Foo object declared here!
};
I am working on a project and I am facing a little bit strange code which I cannot understand why and how can this happen !
I have a class Foo and Baz, and Foo has a non-static method that is called from Baz class without instantiating Foo:
class Foo {
public:
void qux(int a, int b);
};
class Baz {
public:
void bar(void);
};
void Baz::bar(void){
Foo::qux(2,3); // This should not happen as qux is not a static method !!
}
The only way that would work is if Baz was derived from Foo at some level.
Or, of course, Foo bears a different meaning in that scope (via a using, typedef, define or other).
If neither apply, your compiler is seriously broken.
This can happen in case of Baz is inherited from Foo.
In this case you can call method of the base class directly in the form that you mentioned:
void Baz::bar(void){
Foo::qux(2,3);
}