J'ai différents types de données que j'essaie d'enregistrer dans un HashMap.HashMap hétérogène C++
La HashMap sera créée à l'intérieur d'une fonction d'arguments variables. Les syndicats sous struct et Boost :: N'ont pas fonctionné pour moi,
Les syndicats n'acceptent pas les classes comme types de données. Boost :: any me donne des erreurs pendant que je passe par les varargs.
Des suggestions?
S'il vous plaît dites-moi si je devrais donner plus de détails.
Code avec boost :: tout
#include <iostream>
#include <string>
#include <cstdarg>
#include <boost/any.hpp>
#include <boost/variant.hpp>
class vec3D{
public:
int x;
vec3D(int p){ x=p;}
};
using namespace std;
struct testing{
enum Type {U32, U64, I32, I64, D, S, B, ENDNOW};
Type val;
boost::any Point;
};
void mandatory(int count...){
va_list args;
va_start(args, count);
testing tester;
for (tester.val=va_arg(args, testing::Type) ; tester.val != testing::ENDNOW ; tester.val=va_arg(args, testing::Type)){
//for (testing tester.val = va_arg(args, testing->Type); tester.val != 11; tester=va_arg(args, testing->Type)){
switch(tester.val) {
case 0: cout<< "u32"; tester.Point = va_arg(args, uint32_t); break;
case 1: cout<< "u64"; tester.Point = va_arg(args, uint64_t); break;
case 2: cout<< "32"; tester.Point = va_arg(args, int32_t); break;
case 3: cout<< "64"; tester.Point = va_arg(args, int64_t); break;
case 4: cout<< "double"; tester.Point = va_arg(args, double); break;
//case 5: cout<< "string"; tester.Point = va_arg(args, string); break;
case 6: cout<< "boolean"; tester.Point = va_arg(args, bool); break;
case 7: cout<< "EndNow"; break;
default: break; //this is the end now.
}
cout<<'\t'<<tester.Point<<endl;
}
va_end(args);
cout<<endl;
}
int main(){
mandatory(12, testing::U32, 99, testing::B, 0, testing::D,11E1, testing::I64,5000000, testing::ENDNOW);
return 0;
}
Code avec boost :: variante
#include <iostream>
#include <string>
#include <cstdarg>
#include <boost/any.hpp>
#include <boost/variant.hpp>
class vec3D{
public:
int x;
vec3D(int p){ x=p;}
};
using namespace std;
struct testing{
enum Type {U32, U64, I32, I64, D, S, B, ENDNOW};
Type val;
typedef boost::variant<uint32_t, uint64_t, int32_t, int64_t, double, string, bool, vec3D> point;
point Point;
};
void mandatory(int count...){
va_list args;
va_start(args, count);
testing tester;
for (tester.val=va_arg(args, testing::Type) ; tester.val != testing::ENDNOW ; tester.val=va_arg(args, testing::Type)){
switch(tester.val) {
case 0: cout<< "u32"; tester.Point = va_arg(args, uint32_t); break;
case 1: cout<< "u64"; tester.Point = va_arg(args, uint64_t); break;
case 2: cout<< "32"; tester.Point = va_arg(args, int32_t); break;
case 3: cout<< "64"; tester.Point = va_arg(args, int64_t); break;
case 4: cout<< "double"; tester.Point = va_arg(args, double); break;
//case 5: cout<< "string"; tester.Point = va_arg(args, string); break;
case 6: cout<< "boolean"; tester.Point = va_arg(args, bool); break;
case 7: cout<< "EndNow"; break;
default: break; //this is the end now.
}
cout<<'\t'<<tester.Point<<endl;
}
va_end(args);
cout<<endl;
}
int main(){
mandatory(12, testing::U32, 99, testing::B, 0, testing::D,11E1, testing::I64,5000000, testing::ENDNOW);
return 0;
}
-vous besoin de su pport tout type ou juste un ensemble de types? Si c'est le dernier envisager d'utiliser boost/std variante. – NathanOliver
@NathanOliver qui ne fonctionnait pas avec les classes définies par l'utilisateur. –
Définir "n'a pas fonctionné". 'any' /' variant' ressemble à ce dont vous avez besoin. Si vous ne pouvez pas le faire fonctionner, vous pouvez toujours poster un [mcve] et demander de l'aide pour le réparer. – NathanOliver