#include <iostream>
#include <set>
struct ContainerOfA;
struct StructA
{
StructA(ContainerOfA* ptr) : m_b_ptr(ptr)
{}
void CleanMe()
{
m_b_ptr->RemoveStructA(this);
delete this;
}
ContainerOfA* m_b_ptr;
};
struct ContainerOfA
{
void RemoveStructA(StructA *a_ptr)
{
m_set_a.erase(a_ptr);
}
void RemoveAllStructA()
{
for (auto &iter : m_set_a) {
delete iter;
}
}
std::set<StructA*> m_set_a;
};
int main()
{
return 0;
}
g++ (GCC) 10.2.1 20210130
test_me.cpp: In member function ‘void StructA::CleanMe()’:
test_me.cpp:13:12: error: invalid use of incomplete type ‘struct ContainerOfA’
13 | m_b_ptr->RemoveStructA(this);
| ^~
test_me.cpp:4:8: note: forward declaration of ‘struct ContainerOfA’
4 | struct ContainerOfA;
| ^~~~~~~~~~~~
I ran into a circular dependency issue and the sample example is shown above.
Question> Is there a way that I can make it work?
Thank you
Just move the problematic code to a point where the class is defined.
struct StructA
{
// ...
void CleanMe();
// ...
};
struct ContainerOfA
{
// ...
};
inline StructA::CleanMe()
{
m_b_ptr->RemoveA(this);
}
By the way, that's a real code smell having an object know what container it's part of. You should try to architect it so that's not necessary.
Related
I have two classes : Individu and Cite and as u can see Individu is defined before
//file.hpp
#include <iostream>
#include <stdexcept>
#include <vector>
extern Cite CITE;
class Individu {
protected:
static int id;
TYPE t;
public:
Individu();
virtual ~Individu();
static int & getCompteur();
virtual void afficher(std::ostream& ) const;
virtual TYPE getType() const;
};
class Cite {
std::vector<const Individu *> tab;
public:
Cite();
~Cite();
void addPersonne(const Individu *);
int size() const;
};
std::ostream& operator<<(std::ostream&, const Individu& );
#endif
I need to add an Individu one it's instanciated to the tab vector of Cite and sisnce there is just one Cite I declared Exctern CITE Cite to work with just like that :
// file.cpp
#include <algorithm>
#include "file.hpp"
int Individu::id = 0;
Individu::Individu() {
CITE.addPersonne(*this);
id++;
}
Individu::~Individu(){
}
int& Individu::getCompteur() {
return id;
}
void Individu::afficher(std::ostream& o) const{
o << id;
}
void Personne::afficher(std::ostream& o) const {
o << nom << " " << id;
}
std::ostream& operator<<(std::ostream& o, const Individu& i ){
i.afficher(o);
return o;
}
TYPE Individu::getType() const {
throw IllegalException();
}
Cite::Cite(){
}
Cite::~Cite() {
}
void Cite::addPersonne(const Individu * i){
tab.push_back(i);
}
int Cite::size() const {
return tab.size();
}
and when I compile I got this error :
file.hpp:13:8: error: ‘Cite’ does not name a type
13 | extern Cite CITE;
| ^~~~
file.cpp: In constructor ‘Individu::Individu()’:
file.cpp:30:5: error: ‘CITE’ was not declared in this scope
30 | CITE.addPerconne(*this);
| ^~~~
make: *** [makefile:15 : build/deviant.o] Erreur 1
I understand that Cite is not yet defined so that's why I got that error , so hwo can I fix it ?
You have two issues in your code:
extern Cite CITE is declared before the class Cite is defined, so the compiler doesn't know what a Cite is at that point. You should move this declaration after the definition of Cite.
You never define CITE. An extern variable declaration is a promise to the compiler that you will define that variable later. You're essentially saying "I promise a Cite object named CITE exists even though you can't see it right now". You broke that promise by never actually creating that object. You need to define a Cite CITE somewhere (most likely in file.cpp).
main.cpp :
#include "timer.hpp"
int main() {
TCC_TIMER<0> speed_timer;
TC_TIMER<3, TIMER_16BIT> update_timer;
speed_timer = TCC_TIMER<0>::Instance();
update_timer = TC_TIMER<3, TIMER_16BIT>::Instance();
for(;;);
}
timer.hpp :
#ifndef TIMER_HPP
#define TIMER_HPP
class TIMER_GENERAL {
protected:
TIMER_GENERAL() : initialized(false) {};
~TIMER_GENERAL() {};
inline void Init(int number) {
timer_number = number;
initialized = true;
}
inline bool Initialized(void) {
return initialized;
}
bool initialized;
int timer_number;
};
class TIMER_16BIT : public TIMER_GENERAL {
protected:
};
template <class T>
class TIMER_TC : public T {
protected:
};
class TIMER_TCC : public TIMER_GENERAL {
public:
};
template <int number, class T>
class TC_TIMER : public TIMER_TC<T> {
public:
static TC_TIMER<number, T>& Instance(void) {
static TC_TIMER<number, T> timer;
if(!timer.initialized) {
timer.Init(number);
}
return timer;
}
};
template <int number>
class TCC_TIMER : public TIMER_TCC {
public:
static TCC_TIMER<number>& Instance(void) {
static TCC_TIMER<number> timer;
if(!timer.initialized) {
timer.Init(number);
}
return timer;
}
};
#endif /* TIMER_HPP */
When I compile this code with g++ 7.5.0:
g++ -O3 -Wall -std=c++17 main.cpp
I get a warning in the TCC_TIMER class:
dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]:
if(!timer.initialized) {
In TC_TIMER however, I do exactly the same and get no warning.
If I deliberately introduce an error by inserting:
timer = timer + 1;
just before the line with the warning, I get the error:
no match for 'operator+' (operand types are 'TCC_TIMER<0>' and 'int')
which indicates that the compiler knows that timer has the type TCC_TIMER<0>.
Also, when I explicitly cast timer to TCC_TIMER<0>:
if(!static_cast<TCC_TIMER<0>>(timer).initialized) {
the compiler is happy and the warning disappears.
If I use a function instead:
if(!timer.Initialized()) {
everything is ok too.
Can anyone explain why I get this warning in the TCC_TIMER class and not in the TC_TIMER class?
Can anyone explain why I do get this warning while the timer type seems to be ok?
I am trying to work with a two-stage templated alias, but I cannot get the alias to be accepted as a constructor parameter. Is there a way to do this?
An example of what I am trying to do is seen in the following code:
#include <memory>
template <typename Bar>
class Foo
{
public:
using FooPtr = std::shared_ptr<Foo<Bar> >;
static FooPtr getFooPtr(Bar someBar) { return std::make_shared<Foo<Bar> >(someBar); }
Foo(Bar bar) : _bar(bar) {}
Bar getBar() { return _bar; }
private:
Bar _bar;
};
template <typename Bar>
class Foobar
{
public:
using FB = Foo<Bar>;
Foobar(FB::FooPtr fooPtr)
: _fooPtr(fooPtr)
{
}
FB::FooPtr getFooPtr() { return _fooPtr; }
private:
FB::FooPtr _fooPtr;
};
int main()
{
Foobar myFoobar<int>(f::getFooPtr(4));
return myFoobar.getFooPtr()->getBar();
}
When I compile this with g++, I get the following:
dev#ubuntu:~/test/fsm$ g++ -std=c++1z test.cpp
test.cpp:23:23: error: expected ‘)’ before ‘fooPtr’
Foobar(FB::FooPtr fooPtr)
^
test.cpp:28:5: error: need ‘typename’ before ‘Foobar<Bar>::FB::FooPtr’ because ‘Foobar<Bar>::FB’ is a dependent scope
FB::FooPtr getFooPtr() { return _fooPtr; }
^
test.cpp:31:5: error: need ‘typename’ before ‘Foobar<Bar>::FB::FooPtr’ because ‘Foobar<Bar>::FB’ is a dependent scope
FB::FooPtr _fooPtr;
^
test.cpp: In function ‘int main()’:
test.cpp:36:11: error: missing template arguments before ‘myFoobar’
Foobar myFoobar<int>(f::getFooPtr(4));
^
test.cpp:37:11: error: ‘myFoobar’ was not declared in this scope
return myFoobar.getFooPtr()->getBar();
Is the compiler not recognizing the alias FB in the Foobar constuctor? Or is it a problem with resolving Foo::FooPtr to a type?
Thanks!
I tried to implement Properties in c++. I don't no why but if I want to compile my code there are quite a lot of errors. The main Idea was, that a template class and the tamplate constructor will give the requirement Informations.
I would be grateful if somebody could help me!
Compiling Message:
pi#raspberrypi ~/dev/property $ gcc -std=c++0x -o PropertyTest2 PropertyTest2.cpp
PropertyTest2.cpp:22:16: error: expected ‘;’ at end of member declaration
PropertyTest2.cpp:22:19: error: expected unqualified-id before ‘<’ token
PropertyTest2.cpp: In function ‘int main()’:
PropertyTest2.cpp:34:20: error: use of deleted function ‘PropertyTestClass::PropertyTestClass()’
PropertyTest2.cpp:8:7: error: ‘PropertyTestClass::PropertyTestClass()’ is implicitly deleted because the default definition would be ill-formed:
PropertyTest2.cpp:8:7: error: no matching function for call to ‘Property<int>::Property()’
PropertyTest2.cpp:8:7: note: candidates are:
Property4.cpp:21:2: note: template<int (** G)(), void (** S)(int&)> Property::Property()
Property4.cpp:6:7: note: constexpr Property<int>::Property(const Property<int>&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
Property4.cpp:6:7: note: constexpr Property<int>::Property(Property<int>&&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
PropertyTest2.cpp:38:20: error: no matching function for call to ‘Property<int>::Set(int)’
PropertyTest2.cpp:38:20: note: candidate is:
Property4.cpp:30:7: note: void Property<T>::Set(T&) [with T = int]
Property4.cpp:30:7: note: no known conversion for argument 1 from ‘int’ to ‘int&’
Property Class (Property.cpp)
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
template <class T>
class Property {
private:
typedef T (*TGetter)(void);
typedef void (*TSetter)(T &);
TGetter Getter;
TSetter Setter;
public:
typedef T type;
template<TGetter *G,
TSetter *S
>
Property() {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(T &value) {
(this->Setter)(value);
}
};
#endif
Testing file (PropertyTest.cpp):
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter(void) {
return this->_age;
}
void AgeSetter(int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age<&PropertyTestClass::AgeGetter, &PropertyTestClass::AgeSetter>;
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Ok, this time fixed all the problems in your code.
Property.cpp:
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
#include <boost/function.hpp>
template <class T>
class Property {
private:
typedef boost::function <T()> TGetter;
typedef boost::function <void(const T&)> TSetter;
TGetter Getter;
TSetter Setter;
public:
typedef T type;
Property(TGetter G, TSetter S) {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(const T &value) {
(this->Setter)(value);
}
};
#endif
PropertyTests.cpp:
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include <boost/bind.hpp>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter() {
return this->_age;
}
void AgeSetter(const int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age;
PropertyTestClass() : age(
boost::bind(&PropertyTestClass::AgeGetter, this),
boost::bind(&PropertyTestClass::AgeSetter, this, _1))
{}
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Output:
$ ./a.out
Property Test Programm
ReadWrite Property for age
5
I have this code:
hpp:
#include <list>
using namespace std;
class funcionario
{
public:
struct Dia {
int d;
int h;
int id;
int tipo;
};
funcionario ();
void eliminar(int dia, int hora);
private:
list<Dia> agenda;
};
cpp:
#include "myClass.hpp"
funcionario::funcionario(){
agenda = list<Dia> ();
}
void funcionario::eliminar(int dia, int hora) {
list<funcionario::Dia>::iterator it;
it = agenda.begin();
while(it != agenda.end() && (*it).d <= dia) {
if((*it).d == dia && (*it).h == hora) {
agenda.erase(it);
return;
}
++it;
}
}
I get this compiling error:
Funcionario.cpp: In constructor ‘funcionario::funcionario()’:
Funcionario.cpp:5: error: cannot convert ‘std::list<funcionario::Dia, std::allocator<funcionario::Dia> >’ to ‘int’ in assignment
Funcionario.cpp: In member function ‘void funcionario::eliminar(int, int)’:
Funcionario.cpp:9: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
Funcionario.cpp:10: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
Funcionario.cpp:11: error: request for member ‘end’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
I don't know what I'm doing wrong.
Not sure what you're trying to achieve, but the code just needs to be fleshed out a bit with complete function definitions. I got this to compile:
#include <list>
class myClass
{
public:
myClass();
struct myStruct {
int myInfo;
};
void something();
void doSomething(myStruct & ms);
private:
std::list<myStruct> myList;
};
myClass::myClass(){
myList = list<myStruct> ();
}
void myClass::something() {
std::list<myStruct>::iterator it;
it = myList.begin();
while(it != myList.end()) {
doSomething(*it);
++it;
}
}
Incidentally (or maybe directly relevant, not sure) - the copy-initialization of myList in myClass() is unnecessary, as others have stated. The list<> default constructor will do the right thing, and more efficiently.
This seems to be working on my computer, so may it be a compiler problem? Try with another compiler and tell us if it worked
The initialization you're looking for is analogous to Initializing map and set class member variables to empty in C++? - but actually you'll get an empty list automatically (i.e. by the std::list default constructor).
--Edited to reflect your posting of the original code--
H is not declared anywhere.
and is not a valid C++ keyword or token. Use &&.
Use the local header include form of : #include "myClass.hpp"