Multiple definition of variable - c++

Here's my code:
main.cpp
#include "foo.h"
int main()
{
return 0;
}
foo.h
#ifndef FOO_H
#define FOO_H
class Foo
{
public:
Foo();
int bar;
}
#endif
foo.cpp
#include "foo.h"
Foo::Foo()
{
bar = 3;
}
Compiling this gives me the following error:
multiple definition of 'bar'
But I have include guards around the header file which defines bar, therefore how can it be defined more than once?

It's down to the missing semicolon at the end of the class declaration foo in foo.h.
That's confusing the compiler (it seems to be attempting to parse your constructor definition as the name of an object of type foo).
C++ ain't Java you know!

Related

C++ tamplate class instances which reference to each other [duplicate]

I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to the other class type? Thanks!
Here's what I have:
File: bar.h
#ifndef BAR_H
#define BAR_H
#include "foo.h"
class bar {
public:
foo getFoo();
protected:
foo f;
};
#endif
File: foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class foo {
public:
bar getBar();
protected:
bar b;
};
#endif
File: main.cpp
#include "foo.h"
#include "bar.h"
int
main (int argc, char **argv)
{
foo myFoo;
bar myBar;
}
$ g++ main.cpp
In file included from foo.h:3,
from main.cpp:1:
bar.h:6: error: ‘foo’ does not name a type
bar.h:8: error: ‘foo’ does not name a type
You cannot have two classes directly contain objects of the other type, since otherwise you'd need infinite space for the object (since foo has a bar that has a foo that has a bar that etc.)
You can indeed do this by having the two classes store pointers to one another, though. To do this, you'll need to use forward declarations so that the two classes know of each other's existence:
#ifndef BAR_H
#define BAR_H
class foo; // Say foo exists without defining it.
class bar {
public:
foo* getFoo();
protected:
foo* f;
};
#endif
and
#ifndef FOO_H
#define FOO_H
class bar; // Say bar exists without defining it.
class foo {
public:
bar* getBar();
protected:
bar* f;
};
#endif
Notice that the two headers don't include each other. Instead, they just know of the existence of the other class via the forward declarations. Then, in the .cpp files for these two classes, you can #include the other header to get the full information about the class. These forward declarations allow you to break the reference cycle of "foo needs bar needs foo needs bar."
That doesn't make sense. If A contains B, and B contains A, it would be infinite size. Imagine putting having two boxes and trying to put both into each other. Doesn't work, right?
Pointers work though:
#ifndef FOO_H
#define FOO_H
// Forward declaration so the compiler knows what bar is
class bar;
class foo {
public:
bar *getBar();
protected:
bar *b;
};
#endif

How can I fix this circular dependency?

I have three headers that are structured like this:
baz.h
#pragma once
#include "foo.h"
class Foo;
class Bar;
class Baz
{
};
bar.h
#pragma once
#include "foo.h"
class Foo;
class Baz;
class Bar : public Foo
{
};
foo.h
#pragma once
#include "baz.h"
#include "bar.h"
class Baz;
class Bar;
class Foo
{
};
Including these headers is currently giving me errors in bar.h, saying that the base class Foo is undefined.
I thought I had wrapped my head around circular dependencies, and had added forward declarations accordingly, so why won't this code compile?
That is obvious, baz.h and bar.h need foo.h and foo.h it self needs bar.h and baz.h. Create a types.h and forward declare those types and include them into header file and include actual types header into the source codes.
Or simply put all the classes into a single header file.

Why is this error so unclear?

I have the following code:
foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class Foo
{
public:
Foo(Bar bar);
};
#endif //FOO_H
bar.h
#ifndef BAR_H
#define BAR_H
#include "foo.h"
class Bar
{
public:
Bar(Foo foo);
};
#endif //BAR_H
If I compile that, I get the following error message:
expected ')' before 'foo' bar.h line 9
After looking on this website, I fixed it by using a forward declaration of Foo in bar.h, and Bar in foo.h.
My question is, why does the compiler make this error sound like a syntax error, whilst it's actually not ? I would think that catching such an error and return a proper error message would be quite simple.
You have headers with unresolved circular dependency. That is when your code somewhere includes "foo.h" first then after preprocessing it will become
class Bar // expanded from #include "bar.h"
{
public:
Bar(Foo foo); // Foo is not declared at this point
};
class Foo // rest of foo.h content
{
public:
Foo(Bar bar);
};
if your code includes "bar.h" first then after preprocessing it will become
class Foo // expanded from #include "foo.h"
{
public:
Foo(Bar bar); // Bar is not declared at this point
};
class Bar // rest of bar.h content
{
public:
Bar(Foo foo);
};
So there is an error in both cases.
To get around this issue you need to utilize proper forward declarations:
// foo.fwd.h
#ifndef FOO_FWD_H
#define FOO_FWD_H
class Foo;
#endif // FOO_FWD_H
// bar.fwd.h
#ifndef BAR_FWD_H
#define BAR_FWD_H
class Bar;
#endif // BAR_FWD_H
and include them into headers instead of header with complete class declaration:
// foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.fwd.h"
class Foo
{
public:
Foo(Bar bar);
};
#endif //FOO_H
// bar.h
#ifndef BAR_H
#define BAR_H
#include "foo.fwd.h"
class Bar
{
public:
Bar(Foo foo);
};
#endif //BAR_H
and then include headers with class definition only into .cpp or implementation file.
C++ is very hard to parse. When compiler does not know that Foo is name of some type then it expects that we try to declare members with that name in Bar. Code does not parse as any of valid variants of member declarations.
Old compilers just diagnosed such cases as "syntax error". Modern compilers try to be friendlier. The diagnostic likely tries to help us to correct the code towards one of such (or some other similar) valid member declaration.
class Bar
{
public:
Bar (Foo());
Bar (*Moo);
Bar Roo();
};
Unfortunately it guessed totally wrongly since Foo was not meant as member name but as a type of parameter of constructor.

functions that intake objects belonging to classes in other files

Why if I have
in foo.h:
class Foo
{
}
void Bar(const Foo& foo);
it works but:
in foo.h:
class Foo
{
}
in bar.cpp
#include "foo.h"
void Bar(const Foo& foo);
doesn't work (unknown type name 'Foo' is its exact words)?
I don't know what about my question isn't specific and forward declarations don't work they just create a error 'duplicate symbol' so im just going to post the code im working with
in creatures.h
#ifndef CREATURES_H_
#define CREATURES_H_
#include <string>
#include "textio.hpp"
class Creature {
private:
protected:
int statBlock[10];
public:
std::string name = "foo";
Creature ();
void ai(int);
};
class Dwarf : public Creature {
private:
public:
std::string name = "Dwarf";
Dwarf (int);
void defaultDwarfGen();
};
main.cpp
#endif
#include "creatures.hpp"
#include "textio.hpp"
#include <iostream>
int main(int argc, char const *argv[]) {
Dwarf creature_1(0);
return 0;
}
textio.hpp:
#ifndef TEXTIO_H
#define TEXTIO_H
#include <iostream>
#include "creatures.hpp"
void challenge(const Creature& param);
#endif
Your problem is that you are including textio.hpp in creatures.hpp so first time that compiler see function void challenge(const Creature& param)Creature class isn't defined.
When you include createures.hppin textio.hpp CREATURES_H_ is already defined and bypass inclusion)
You can fix it deleting this include or declaring a forward definition for Creature class
In order to answer this question properly, you must provide foo.h, foo.cpp, bar.h, and bar.cpp
In short:
To make use of Bar in foo.h, foo.h must have the declaration for Bar.
To make use of Foo in bar.h, bar.h must have the declaration for Foo.
To make use of Bar in foo.cpp, foo.h or foo.cpp must have the declaration for Bar.
To make use of Foo in bar.cpp, bar.h or bar.cpp must have the declaration for Foo.
When I say, "must have declaration for", you can #include the appropriate header.
If you are trying to use Foo in Bar and Bar in Foo, then you've got a circular reference. The way we overcome this is with a forward declaration.
You can read about forward declarations here: https://isocpp.org/wiki/faq/misc-technical-issues#forward-decl

How to create two classes in C++ which use each other as data?

I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to the other class type? Thanks!
Here's what I have:
File: bar.h
#ifndef BAR_H
#define BAR_H
#include "foo.h"
class bar {
public:
foo getFoo();
protected:
foo f;
};
#endif
File: foo.h
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class foo {
public:
bar getBar();
protected:
bar b;
};
#endif
File: main.cpp
#include "foo.h"
#include "bar.h"
int
main (int argc, char **argv)
{
foo myFoo;
bar myBar;
}
$ g++ main.cpp
In file included from foo.h:3,
from main.cpp:1:
bar.h:6: error: ‘foo’ does not name a type
bar.h:8: error: ‘foo’ does not name a type
You cannot have two classes directly contain objects of the other type, since otherwise you'd need infinite space for the object (since foo has a bar that has a foo that has a bar that etc.)
You can indeed do this by having the two classes store pointers to one another, though. To do this, you'll need to use forward declarations so that the two classes know of each other's existence:
#ifndef BAR_H
#define BAR_H
class foo; // Say foo exists without defining it.
class bar {
public:
foo* getFoo();
protected:
foo* f;
};
#endif
and
#ifndef FOO_H
#define FOO_H
class bar; // Say bar exists without defining it.
class foo {
public:
bar* getBar();
protected:
bar* f;
};
#endif
Notice that the two headers don't include each other. Instead, they just know of the existence of the other class via the forward declarations. Then, in the .cpp files for these two classes, you can #include the other header to get the full information about the class. These forward declarations allow you to break the reference cycle of "foo needs bar needs foo needs bar."
That doesn't make sense. If A contains B, and B contains A, it would be infinite size. Imagine putting having two boxes and trying to put both into each other. Doesn't work, right?
Pointers work though:
#ifndef FOO_H
#define FOO_H
// Forward declaration so the compiler knows what bar is
class bar;
class foo {
public:
bar *getBar();
protected:
bar *b;
};
#endif