1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Cat
{
string name;
int age;
};
struct Pencil
{
float sharpness;
float length;
};
struct ListOfThings
{
enum Type
{
CatList,
PencilList
} type;
void *listData;
};
vector<Pencil> pencils;
vector<Cat> cats;
vector<ListOfThings> myThings;
void format(const ListOfThings::Type a)
{
swap(a[1],a[2]);// i have error
}
int main()
{
cats.push_back( {"Joey", 4});
cats.push_back( {"Mitzi", 6});
cats.push_back( {"Garfield", 10});
format(&cats); // error ...............
pencils.push_back( {1.0f, 17.6f});
pencils.push_back( {0.6f, 12.1f});
pencils.push_back( {1.6f, 24.6f});
pencils.push_back( {0.2f, 18.0f});
myThings.push_back( {ListOfThings::CatList, &cats});
myThings.push_back( {ListOfThings::PencilList, &pencils});
cout << "Hey look, I just found the lists of the things I own. I made them a while ago. Let's take a look at them!\n\n";
for (const ListOfThings& list : myThings)
{
if (list.type == ListOfThings::CatList)
{
cout << "Ahh... A list of cats... curious...\n";
for (const Cat& cat : *static_cast<vector<Cat>*>(list.listData))
{
cats.erase(cats.begin()); // Ok i found the ssolution ...
// ........
cout << cat.name << ", age: " << cat.age << '\n';
}
}
else if (list.type == ListOfThings::PencilList)
{
cout << "Hmm... A bunch of pencils...\n";
for (const Pencil& pencil : *static_cast<vector<Pencil>*>(list.listData))
{
cout << "sharpness: " << pencil.sharpness << ", length: " << pencil.length << '\n';
}
}
}
}
|