using multiple instance within the function

hi,
I am trying to reuse multiple instance of different class within a function by passing it as argument using template.but there 's some error when i compile it
i am placing the code below for vivid scenario

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
struct client_creds {
	std::string client_id;
	MQTTClient client_ref;
	mqtt_tb_client *client;
	std::string topic;
	int num_count { 0 };
};
//std::vector<std::string> topic_container{"rpi2/temp", "sense/apple", "sense/bannana", "sense/util", "mqtt/temp",
//       "sense/temp",
//  "sense/pine", "sense/fortis/udap"};
std::vector<client_creds> publisher;
std::vector<client_creds> subscriber;
std::vector<std::unique_ptr<client_tb>> clients;
std::vector<client_creds> paho_client_contnr;
std::vector<std::unique_ptr<topics_tb>> topics;
std::vector<std::string> client_id_cont;
int finish_thread = 1, num_clients, num_topics, num_pubs, num_subs;
std::string _port;
std::string _ip_addr;
using future_store = std::vector<std::future<void>>;
std::mutex coutmutex;

void retireCompletedThreads(future_store &threadList) {
	for (auto i = threadList.begin(); i != threadList.end(); /* ++i */) {
		if (i->wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
			i->get();
			i = threadList.erase(i);
		} else {
			++i;
		}
	}
}

void waitForAllThreads(future_store &threadList) {
	for (auto &f : threadList) {
		f.get();
	}
}

std::mutex coutMutex;
void gen_rndm_number(std::string rndm_strng, std::string xml_strng) {

	int num_clnts_strt, fsthsh, sechsh, num_clnts_end;
	std::cout << "you have to give random id now" << std::endl;
	//	std::string random_clntstrng = json_tree.get<std::string>(
	///	"dynamic");
	std::string phrse_clnt_rnge = rndm_strng.substr(rndm_strng.find('m') + 1);
	std::cout << "the phrase of the client range is" << phrse_clnt_rnge
			<< std::endl;
	std::size_t num_hash = std::count(rndm_strng.begin(), rndm_strng.end(),
			'#');
	fsthsh = phrse_clnt_rnge.find('#') + 1;
	sechsh = phrse_clnt_rnge.find("#", fsthsh);
	num_clnts_strt =
			atoi(
					phrse_clnt_rnge.substr((phrse_clnt_rnge.find('#') + 1),
							sechsh - 1).c_str());
	if (num_clnts_strt == 0) {
		num_clnts_strt = (rand() % 10 + 1);
	}
	if (num_hash % 2 == 0) {
		int thdhsh = phrse_clnt_rnge.find("#", sechsh);
		num_clnts_end = atoi(
				phrse_clnt_rnge.substr(sechsh + 1, thdhsh).c_str());
		if (num_hash == 4) {
			//num_clnts_nd_rnge=num_clnts_end;
			//num_strt=num_clnts_strt-num_clnts_end;
		}
	}
	boost::random::random_device rng;
	if (num_clnts_end != 0) {
		boost::random::uniform_int_distribution<> rngernd_dist(num_clnts_strt,
				num_clnts_end);
		if (xml_strng == "clients") {
			num_clients = rngernd_dist(rng);
		} else if (xml_strng == "topics") {
			num_topics = rngernd_dist(rng);
		}

		//	std::cout
		//		<< "the random number of topic to be created is"
		///<< num_<< std::endl;
	} else {
		boost::random::uniform_int_distribution<> rnd_dist(0, num_clnts_strt);
		if (xml_strng == "clients") {
			num_clients = rnd_dist(rng);
		} else if (xml_strng == "topics") {
			num_topics = rnd_dist(rng);
		}
		///	std::cout
		///	<< "the random number of clients to be created is"
		//	<< num_clients << std::endl;
	}
	std::cout << "the first delimiter of the range is" << num_clnts_strt
			<< std::endl;
	std::cout << "the end delimiter of the range is" << num_clnts_end
			<< std::endl;
	//std::cout<<"the random number is"<<rnd_num<<std::endl;
}

template < class T>
int parse_json_confg_val_frmxml(boost::property_tree::ptree json_valTree,std::string json_strng,std::string num_json_key,auto xml_it,T t){
	namespace prop_tree = boost::property_tree;
	int stat_val=0;
    typedef void (T::*my_func)(const char *);
//    my_func f=pmemfn;
	std::istringstream json_clnt_strm(json_strng);
    prop_tree::read_json(json_clnt_strm, json_valTree);
    int size_json_key=json_valTree.size();
    std::cout<<"the size of json is"<<size_json_key<<std::endl;
	for (prop_tree::ptree::iterator json_it = json_valTree.begin();
					json_it != json_valTree.end(); ++json_it) {
				std::cout<<"the iterator is"<< json_it->first<<std::endl;

				if (json_it->first == num_json_key) {
					//std::cout<<"you have come here for dynamic creation of client"<<std::endl;
					std::cout << "you have to give random id now" << std::endl;
					std::string random_clntstrng = json_valTree.get<std::string>(
							json_it->first);
					//generates random number of client list from this function
					gen_rndm_number(random_clntstrng, xml_it->first);
				//	std::cout << "the random number of clients generated is"
						//	<< num_clients << std::endl;
				} else if (json_it->first == "static") {
					//std::cout<<"you have come here for static elaborate creation of client"<<std::endl;
					BOOST_FOREACH (prop_tree::ptree::value_type &rowPair , json_valTree.get_child(json_it->first)){
					// rowPair.first == ""
					BOOST_FOREACH (prop_tree::ptree::value_type& itemPair, rowPair.second) {
						++stat_val;

					//	client_id_cont.push_back(itemPair.second.get_value<std::string>());
						std::cout << "the defined  id is--->"<<itemPair.second.get_value<std::string>() << "\n";
						//dfnd_clnt_obj(new T ());
						std::unique_ptr<T> dfnd_clnt_obj(new T {"usual"});
						 if(num_json_key =="num_topic"){
											//std::cout<<"the random topic is"<<rnd_tpc_path<<std::endl;
                            std::bind(&T::set_rnd_topic,&t,itemPair.second.get_value<std::string>());
                          //  topics.push_back(std::move(dfnd_clnt_obj));
							//std::cout << "the defined topic is--->"<<topics[topics.size()-1]->get_topic() << "\n";
						}if(num_json_key == "num_client"){
                                        //(dfnd_clnt_obj->f)(itemPair.second.get_value<std::string>().c_str());
                                        std::bind(&T::set_rand_clnt_id,&t,itemPair.second.get_value<std::string>());
                                        clients.push_back(std::move(dfnd_clnt_obj));
                                        //std::cout << "the defined client id is--->"<<clients[clients.size()-1]->get_clnt_id() << "\n";
                                    }

						//num_clients++;

					}
				}
			}

		}
	std::cout << "the number of static clients created is"
						<< stat_val << std::endl;
	return stat_val;
}

Last edited on
Please use the code tags and intuitive/systematic indentation. See http://www.cplusplus.com/articles/jEywvCM9/
(You can edit your post.)
thanx for the comment sorry i couldnt post the full code because it is not allowing to do so
but there 's some error when i compile it


Don't be vague.

Post the text of the errors in full verbatim, and the code where the errors come from. You can make more than one post in order to get all of the code, but don't overdo it.

Preferably post something we can compile ourselves.

If you have written such code yourself, I would expect by now you ought to to know more about how to fix it.

Don't start a new topic about the same subject. I suspect your last 4 posts are about the same thing.
Last edited on
Topic archived. No new replies allowed.