K
Klaas Vantournhout
Hi,
Recently I obtained a problem with virtual inheritance when implementing
it in multiple files. To present the problem I have included at the
bottom of this post the code of the 4 files. I show results with the
gnu compiler but i have the same results with the intel compiler.
file1.cpp compiles without a problem, the problem occurs when compiling
file2.cpp. It complains about not finding the constructor of the class
base.
[klaas@blackdwarf cpptest]$ g++ -c -Wall file2.cpp
file2.cpp: In constructor 'doublederived::doublederived(int)':
file2.cpp:3: error: no matching function for call to 'base::base()'
file1.hpp:3: note: candidates are: base::base(int)
file1.hpp:1: note: base::base(const base&)
When derived does not virtual inherit base, this does not give a
problem. Is there a reason why this is not working?
Regards
Klaas
// file1.hpp
class base {
public:
base(int);
};
class derived : public virtual base {
public:
derived(int);
};
// file1.cpp
#include <iostream>
#include "file1.hpp"
base::base(int a) { std::cout << a << std::endl;}
derived::derived(int a) : base(a) { }
// file2.hpp
#include "file1.hpp"
class doublederived : public derived {
public:
doublederived(int);
};
// file2.cpp
#include "file2.hpp"
doublederived::doublederived(int a) : derived(a) { }
Recently I obtained a problem with virtual inheritance when implementing
it in multiple files. To present the problem I have included at the
bottom of this post the code of the 4 files. I show results with the
gnu compiler but i have the same results with the intel compiler.
file1.cpp compiles without a problem, the problem occurs when compiling
file2.cpp. It complains about not finding the constructor of the class
base.
[klaas@blackdwarf cpptest]$ g++ -c -Wall file2.cpp
file2.cpp: In constructor 'doublederived::doublederived(int)':
file2.cpp:3: error: no matching function for call to 'base::base()'
file1.hpp:3: note: candidates are: base::base(int)
file1.hpp:1: note: base::base(const base&)
When derived does not virtual inherit base, this does not give a
problem. Is there a reason why this is not working?
Regards
Klaas
// file1.hpp
class base {
public:
base(int);
};
class derived : public virtual base {
public:
derived(int);
};
// file1.cpp
#include <iostream>
#include "file1.hpp"
base::base(int a) { std::cout << a << std::endl;}
derived::derived(int a) : base(a) { }
// file2.hpp
#include "file1.hpp"
class doublederived : public derived {
public:
doublederived(int);
};
// file2.cpp
#include "file2.hpp"
doublederived::doublederived(int a) : derived(a) { }