質問編集履歴

3

書式

2016/12/24 10:09

投稿

NEWBIEEBIEE
NEWBIEEBIEE

スコア62

test CHANGED
File without changes
test CHANGED
@@ -308,4 +308,4 @@
308
308
 
309
309
 
310
310
 
311
- 12/24 19:08に更新
311
+ 12/24 19:08 Chironianさんの回答元に更新しました。

2

コードの修正

2016/12/24 10:09

投稿

NEWBIEEBIEE
NEWBIEEBIEE

スコア62

test CHANGED
File without changes
test CHANGED
@@ -8,17 +8,27 @@
8
8
 
9
9
  ```
10
10
 
11
+ vcap >> mat;
12
+
13
+
14
+
11
15
  MatToBytes(mat, &pBuf );
12
16
 
13
17
  std::string sName;
14
18
 
15
- myshmvector->push_back(sName(*pBuf));
19
+ myshmvector->push_back(sName(pBuf));
16
20
 
17
21
  ```
18
22
 
19
23
  というコードで、MatToBytesにMatと pBufというunsigned charの二重ポインタを引数でとり、格納し、memcpyしたいのですが、
20
24
 
21
- 共有メモリのVectorにてStringで格納する際、コンパイルエラーでbasic stringにnot matchingします。
25
+ 共有メモリのVectorにてStringで格納する際、
26
+
27
+ コンパイルエラーでno match for call to ‘(std::string {aka std::basic_string<char>}) (uchar*&)’
28
+
29
+ myshmvector->push_back(sName(pBuf));
30
+
31
+
22
32
 
23
33
  二重ポインタのせいなのか、std::stringがbasic_stringに合わないのかわかりません。
24
34
 
@@ -36,21 +46,35 @@
36
46
 
37
47
  ```
38
48
 
49
+ MyShmStringVector::iterator it;
50
+
51
+ cv::Mat mat;
52
+
53
+ cv::VideoCapture vcap(0);
54
+
55
+ uchar* pBuf = NULL;
56
+
57
+
58
+
39
- for(it = myvector->begin(); it !=myvector->end(); ++it){
59
+ for(it = myvector->begin(); it !=myvector->end(); ++it){
40
-
60
+
41
- pBuf = (unsigned char*)*it.c_str();
61
+ pBuf = (unsigned char*)(*it).c_str();
42
-
62
+
43
- std::memcpy(mat, pBuf, *it->size());
63
+ std::memcpy(mat, pBuf, it->size());
44
-
64
+
45
- cv::imshow("window1", mat);
65
+ cv::imshow("window1", mat);
46
-
66
+
47
- }
67
+ }
48
-
68
+
49
- ```
69
+ ```
70
+
50
-
71
+ エラーでは
72
+
51
-
73
+ ‘cv::Mat’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
52
-
74
+
53
- it.c_str()というメンバがないといわれます。
75
+ std::memcpy(mat, pBuf, it->size());
76
+
77
+ が出力されます。
54
78
 
55
79
  ごちゃごちゃしていてわからないと思いますが、ご教授の方お願いします。
56
80
 
@@ -60,121 +84,195 @@
60
84
 
61
85
  ```
62
86
 
63
- #include <boost/interprocess/managed_shared_memory.hpp>
64
-
65
- #include <boost/interprocess/containers/vector.hpp>
66
-
67
- #include <boost/interprocess/containers/string.hpp>
68
-
69
- #include <boost/interprocess/allocators/allocator.hpp>
70
-
71
- #include <string>
72
-
73
- #include <opencv2/core/core.hpp>
74
-
75
- #include <opencv2/highgui/highgui.hpp>
76
-
77
- #include <cstdlib> // std::system
78
-
79
-
80
-
81
- using namespace boost::interprocess;
82
-
83
-
84
-
85
- typedef unsigned char uchar;
86
-
87
-
88
-
89
- typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
90
-
91
-
92
-
93
- typedef basic_string<char, std::char_traits<char>, CharAllocator> MyShmString;
94
-
95
-
96
-
97
- typedef allocator<MyShmString, managed_shared_memory::segment_manager> StringAllocator;
98
-
99
-
100
-
101
- typedef vector<MyShmString, StringAllocator> MyShmStringVector;
102
-
103
-
104
-
105
- int MatToBytes(cv::Mat image, uchar ** pimage_uchar)
87
+ #include <boost/interprocess/managed_shared_memory.hpp>
88
+
89
+ #include <boost/interprocess/containers/vector.hpp>
90
+
91
+ #include <boost/interprocess/containers/string.hpp>
92
+
93
+ #include <boost/interprocess/allocators/allocator.hpp>
94
+
95
+ #include <string>
96
+
97
+ #include <opencv2/core/core.hpp>
98
+
99
+ #include <opencv2/highgui/highgui.hpp>
100
+
101
+ #include <cstdlib> // std::system
102
+
103
+
104
+
105
+ using namespace boost::interprocess;
106
+
107
+
108
+
109
+ typedef unsigned char uchar;
110
+
111
+
112
+
113
+ typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
114
+
115
+
116
+
117
+ typedef basic_string<char, std::char_traits<char>, CharAllocator> MyShmString;
118
+
119
+
120
+
121
+ typedef allocator<MyShmString, managed_shared_memory::segment_manager> StringAllocator;
122
+
123
+
124
+
125
+ typedef vector<MyShmString, StringAllocator> MyShmStringVector;
126
+
127
+
128
+
129
+ int MatToBytes(cv::Mat image, uchar ** pimage_uchar)
130
+
131
+ {
132
+
133
+ uchar * image_uchar = * pimage_uchar;
134
+
135
+ // class data members of ints
136
+
137
+ int image_rows = image.rows;
138
+
139
+ int image_cols = image.cols;
140
+
141
+ int image_type = image.type();
142
+
143
+
144
+
145
+ int image_size = image.total() * image.elemSize();
146
+
147
+ image_uchar = new uchar[image_size];
148
+
149
+ std::memcpy(image_uchar, image.data, image_size * sizeof(uchar));
150
+
151
+ return 1;
152
+
153
+ }
154
+
155
+
156
+
157
+ int main(int argc, char* argv[])
158
+
159
+ {
160
+
161
+
162
+
163
+ struct shm_remove
106
164
 
107
165
  {
108
166
 
167
+ shm_remove(){shared_memory_object::remove("MySharedMemory");}
168
+
169
+ ~shm_remove(){shared_memory_object::remove("MySharedMemory");}
170
+
171
+ }remover;
172
+
173
+
174
+
175
+
176
+
177
+ if(argc == 1){ //Parent process
178
+
179
+ // Remove shared memory on construction and destruction
180
+
181
+
182
+
183
+ // Create a new segment with given name and size
184
+
185
+ managed_shared_memory segment(create_only,"MySharedMemory",65536);
186
+
187
+
188
+
189
+ //Initialize shared memory STL-compatible allocator
190
+
191
+ CharAllocator charallocator(segment.get_segment_manager());
192
+
193
+ StringAllocator stringallocator(segment.get_segment_manager());
194
+
195
+ //Construct a vector named "MyVector" in shared memory with argument alloc_inst
196
+
197
+ MyShmStringVector *myshmvector = segment.construct<MyShmStringVector>("MyVector")(stringallocator);
198
+
199
+
200
+
201
+
202
+
203
+ cv::Mat InputImage;
204
+
205
+ cv::Mat mat;
206
+
207
+ cv::VideoCapture vcap(0);
208
+
209
+ uchar* pBuf = NULL;
210
+
211
+
212
+
213
+
214
+
215
+ if (!vcap.isOpened())
216
+
217
+ return -1;
218
+
219
+
220
+
221
+ while (1) {
222
+
223
+ vcap >> mat;
224
+
225
+
226
+
227
+ MatToBytes(mat, &pBuf );
228
+
229
+ std::string sName;
230
+
109
- uchar * image_uchar = * pimage_uchar;
231
+ myshmvector->push_back(sName(pBuf));
232
+
110
-
233
+ //Launch child process
234
+
235
+ if(cv::waitKey(30) >= 0) break;
236
+
237
+ }
238
+
111
- // class data members of ints
239
+ // Launch child process
112
-
113
- int image_rows = image.rows;
240
+
114
-
115
- int image_cols = image.cols;
241
+ std::string s(argv[0]); s += " child";
116
-
242
+
117
- int image_type = image.type();
243
+ if(0 != std::system(s.c_str()))
118
-
119
-
120
-
121
- int image_size = image.total() * image.elemSize();
122
-
123
- image_uchar = new uchar[image_size];
124
-
125
- std::memcpy(image_uchar, image.data, image_size * sizeof(uchar));
126
244
 
127
245
  return 1;
128
246
 
247
+
248
+
249
+ // check child has destroyed the vector
250
+
251
+ if(segment.find<MyShmStringVector>("MyVector").first)
252
+
253
+ return 1;
254
+
129
255
  }
130
256
 
131
-
132
-
133
-
134
-
135
-
136
-
137
- int main(int argc, char* argv[])
138
-
139
- {
140
-
141
- if(argc == 1){ //Parent process
257
+ else{// child process
142
-
143
- // Remove shared memory on construction and destruction
258
+
144
-
145
-
146
-
147
- /* 初期化など*/
148
-
149
- struct shm_remove
150
-
151
- {
152
-
153
- shm_remove(){shared_memory_object::remove("MySharedMemory");}
154
-
155
- ~shm_remove(){shared_memory_object::remove("MySharedMemory");}
156
-
157
- }remover;
158
-
159
-
160
-
161
- // Create a new segment with given name and size
259
+ // Open the managed segment
162
-
260
+
163
- managed_shared_memory segment(create_only,"MySharedMemory",65536);
261
+ managed_shared_memory segment(open_only, "MySharedMemory");
164
-
165
-
166
-
262
+
263
+
264
+
167
- //Initialize shared memory STL-compatible allocator
265
+ // Find the vector using c-string name
168
-
169
- CharAllocator charallocator(segment.get_segment_manager());
266
+
170
-
171
- StringAllocator stringallocator(segment.get_segment_manager());
172
-
173
- //Construct a vector named "MyVector" in shared memory with argument alloc_inst
174
-
175
- MyShmStringVector *myshmvector = segment.construct<MyShmStringVector>("MyVector")(stringallocator);
267
+ MyShmStringVector *myvector = segment.find<MyShmStringVector>("MyVector").first;
268
+
269
+
270
+
176
-
271
+ // Use vector in reverse order
272
+
273
+
274
+
177
- cv::Mat InputImage;
275
+ MyShmStringVector::iterator it;
178
276
 
179
277
  cv::Mat mat;
180
278
 
@@ -182,104 +280,32 @@
182
280
 
183
281
  uchar* pBuf = NULL;
184
282
 
185
-
186
-
187
- /* 初期化終了 */
283
+
188
-
189
-
190
-
191
-
192
-
193
- if (!vcap.isOpened()) // もちろんカメラは開いてる
284
+
194
-
195
- return -1;
196
-
197
-
198
-
199
- // キーボード押すまでMatを取り続けるんだ
200
-
201
- while (1) {
202
-
203
- vcap >> mat;
204
-
205
-
206
-
207
- MatToBytes(mat, &pBuf );
208
-
209
- std::string sName;
210
-
211
- myshmvector->push_back(sName(*pBuf)); <-- ココがエラー
285
+ for(it = myvector->begin(); it !=myvector->end(); ++it){
212
-
286
+
213
- if(cv::waitKey(30) >= 0) break;
287
+ pBuf = (unsigned char*)(*it).c_str();
288
+
289
+ std::memcpy(mat, pBuf, it->size());
290
+
291
+ cv::imshow("window1", mat);
214
292
 
215
293
  }
216
294
 
217
- // Launch child process
295
+
218
-
219
- std::string s(argv[0]); s += " child";
296
+
220
-
221
- if(0 != std::system(s.c_str()))
222
-
223
- return 1;
224
-
225
-
226
-
227
- // check child has destroyed the vector
228
-
229
- if(segment.find<MyShmStringVector>("MyVector").first)
230
-
231
- return 1;
232
-
233
- }
234
-
235
- else{// child process
236
-
237
- // Open the managed segment
238
-
239
- managed_shared_memory segment(open_only, "MySharedMemory");
240
-
241
-
242
-
243
- // Find the vector using c-string name
244
-
245
- MyShmStringVector *myvector = segment.find<MyShmStringVector>("MyVector").first;
246
-
247
-
248
-
249
- // Use vector in reverse order
250
-
251
-
252
-
253
- MyShmStringVector::iterator it;
254
-
255
- cv::Mat mat;
256
-
257
- cv::VideoCapture vcap(0);
258
-
259
- uchar* pBuf = NULL;
260
-
261
-
262
-
263
- for(it = myvector->begin(); it !=myvector->end(); ++it){
264
-
265
- pBuf = (unsigned char*)*it.c_str();
266
-
267
- std::memcpy(mat, pBuf, *it->size());
268
-
269
- cv::imshow("window1", mat);
270
-
271
- }
272
-
273
-
274
-
275
- segment.destroy<MyShmStringVector>("MyVector");
297
+ segment.destroy<MyShmStringVector>("MyVector");
276
-
277
-
278
-
279
- }
298
+
280
-
281
- return 0;
299
+
282
300
 
283
301
  }
284
302
 
303
+ return 0;
304
+
305
+ }
306
+
285
- ```
307
+ ```
308
+
309
+
310
+
311
+ 12/24 19:08に更新

1

初心者マーク

2016/12/24 10:08

投稿

NEWBIEEBIEE
NEWBIEEBIEE

スコア62

test CHANGED
File without changes
test CHANGED
File without changes