I have a very simple constellation from two classes "Id" and "IdValidator" and their corresponding interfaces. Bought classes depend on each other i.e. there is a circular dependency between bought of them. The problem with the circular dependency is solved by using a forward declaration. That all is compiling and linking just fine with clang i.e. git commit : e4d4edafa924fad8902c52a10a2c0ebbd39907bf from branch master
Then I came to the need to split those two classes in different namespaces. All i did is without modifing the working version i created two directories and placed the corresponding class and it's interfaecs in the second directory. The changed the namepsaces and the corresponing includes.
I am able to execute "cmake ../", but when i run "make" I am facing the following error :
when I now compile the former working example i get the error
visitor/validator/IdValidator.cpp:36:23: error: member access into incomplete type 'std::__shared_ptr_access<IdInterface, __gnu_cxx::_S_atomic, false, false>::element_type'
(aka 'IdInterface')
string name = entity->getName();
^
visitor/validator/IdValidatorInterface.h:12:7: note: forward declaration of 'IdInterface'
class IdInterface;
the 4 files of the non working version are in the branch split here i.e. commit 58345716e9b4a6062a470519690502b5fb80662a
here are the files in question:
IdValidator.h
#ifndef _ID_VALIDATOR_
#define _ID_VALIDATOR_
#include <string>
#include <vector>
#include <limits>
#include <memory>
#include "../src/Id.h"
#include "IdValidatorInterface.h"
namespace validator
{
using Id = src::Id;
using IdValidatorInterface = src::IdValidatorInterface;
class IdValidator : public virtual IdValidatorInterface
{
public:
IdValidator();
virtual ~IdValidator();
virtual bool isValid( shared_ptr<IdInterface> entity) override;
vector<string> validate(shared_ptr<IdInterface> entity) override;
};
}
#endif
IdValidator.cpp
#include "IdValidator.h"
#include "../src/Id.h"
#include "../src/IdInterface.h"
using namespace std;
namespace validator
{
using Id = src::Id;
IdValidator::IdValidator()
{
}
bool IdValidator::isValid(shared_ptr<IdInterface> entity)
{
vector<string> listOfErros = validate(entity);
if(listOfErros.empty() ){
return false;
}else{
return true;
}
}
vector<string> IdValidator::validate(shared_ptr<IdInterface> entity)
{
vector<string> stringList = vector<string>();
// do some validation of the name
string name = entity->getName();
if (name == "BadName")
{
string error = "Error. id name is bad";
stringList.push_back(error);
return stringList;
}
return stringList;
}
}
IdValidatorInterface.h
#ifndef _ID_VALIDATOR_INTERFACE_
#define _ID_VALIDATOR_INTERFACE_
#include "BaseElementValidatorInterface.h"
#include <string>
#include <memory>
#include <vector>
using namespace std;
class IdInterface;
namespace validator
{
class IdValidatorInterface : public virtual BaseElementValidatorInterface
{
public:
IdValidatorInterface();
virtual ~IdValidatorInterface();
virtual bool isValid( shared_ptr<IdInterface> entity ) = 0;
virtual vector<string> validate( shared_ptr<IdInterface> entity) = 0;
};
}
#endif
The compiler is basically complaining about the forward declaration "class IdInterface;" in the header file IdValidatorInterface.h. I am not sure how to solve this thus asking for help here. Other articles suggest to include the file in question but that does not help in this situation i.e.
member access into incomplete type ‘QScrollBar’
Member access into incomplete type 'CALayer'
Is this doable in c++ or do they have to be in the same namespace and same folder in order for this to work. I would really like to split those two.
I am using clang version 6.0.1 (emscripten 1.38.12 : 1.38.12) Target: x86_64-unknown-linux-gnu Thread model: posix
Aucun commentaire:
Enregistrer un commentaire