在下面的2个代码示例中(下面的第一个代码块工作,第二个代码块没有&t;)
#include <iostream>
#include <vector>
#include <string>
struct padStruct
{
int buttonID;
long timeMS;
std::string codetoSEND;
};
int main()
{
padStruct s = {0,0,""}, *s2;
s2 = &s;
s.buttonID = 3; //or (*s2).buttonID = 3;
s.timeMS = 1200; //or (*s2).timeMS = 1200;
s.codetoSEND = "play song"; //or (*s2).codetoSEND = "play song";
std::cout << (*s2).buttonID << ',' << (*s2).timeMS << ',' << '"' << (*s2).codetoSEND << '"' << '\n';
}
这很有效。
相反:
#include <iostream>
#include <vector>
#include <string>
struct padStruct
{
int buttonID;
long timeMS;
std::string codetoSEND;
};
int main()
{
padStruct *s = {0};
//padStruct *s = {0,0,""} //Error C2078 too many initializers
(*s).buttonID = 3; //or (*s2).buttonID = 3;
(*s).timeMS = 1200; //or (*s2).timeMS = 1200;
(*s).codetoSEND = "play song"; //or (*s2).codetoSEND = "play song";
std::cout << (*s).buttonID << ',' << (*s).timeMS << ',' << '"' << (*s).codetoSEND << '"' << '\n';
}
这不是。
有人可以简单解释
为什么我无法在底部代码上初始化padStruct * s = {0,0,&#34;&#34;),而它在顶部工作,否则我得到 < em>错误C2078初始化程序太多 |
更重要的是,为什么我必须创建一个padStruct变量s然后创建一个padStruct * s2然后必须将s2指向s?否则我会收到 错误C2228:&#39; .buttonID&#39;必须有class / struct / union 。
非常混乱。也许至少解释第二个问题有点容易,所以它会陷入其中。谢谢。
答案 0 :(得分:2)
为什么我无法初始化
padStruct *s = {0, 0, "")
因为s
是指针,而不是复合类型。可以将它初始化为零,或者指向padStruct
的新实例的指针,但它只是一个项目。
为什么我必须创建
padStruct
变量s
,然后创建padStruct *s2
,然后必须将s2
指向s
?
您需要初始化s2
指针以指向padStruct
的实例。将其指向s
是一种选择,但还有其他选择。例如,您可以写
padStruct *s = new padStruct { 0, 0, "" };
甚至
padStruct *s = new padStruct { 3, 1200, "play song" };
避免逐个设置字段。
请注意,您可以使用s->buttonID
语法代替(*s).buttonID
。
答案 1 :(得分:0)
在第一个块中,您定义了一个实际的padStruct s
对象,并将另一个指针 s2
定义为padStruct
。
padStruct s = {0,0,""}, *s2
对于s2 = &s
,使s2
指向s
没有错,s2
指针是s
的地址,它是在堆栈中分配的。因此,当您s2
取消引用(*s2).buttonID
时,该值实际上来自s
。 (与s.buttonID
相同)
在下一个区块,您正在做的是initialize a pointer with {0}
。这是错的。指针应初始化为0
或nullptr
。 {0}
仅适用于POD struct
。
padStruct *s = {0};
您需要为s
padStruct *s = new padStruct
准备s
空间,然后s = """
num_markers num_contamination color
0 2.0 0.0 #db5f57
1 100.0 47.0 #db7157
2 104.0 102.0 #db8457
3 102.0 100.0 #db9657
4 NaN NaN #dba957
5 51.0 0.0 #dbbb57
6 NaN NaN #dbce57
7 92.0 63.0 #d6db57
8 NaN NaN #c4db57
9 56.0 42.0 #b1db57
"""
df = pd.read_clipboard() #Copy and paste then do this
D_idx_color = df["color"].to_dict()
df.style.apply(lambda x:"background-color: %s"%D_idx_color[x], subset=["color"])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/jespinoz/anaconda/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
309 method = get_real_method(obj, self.print_method)
310 if method is not None:
--> 311 return method()
312 return None
313 else:
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/formats/style.py in _repr_html_(self)
190 def _repr_html_(self):
191 """Hooks into Jupyter notebook rich display system."""
--> 192 return self.render()
193
194 def _translate(self):
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/formats/style.py in render(self)
415 the rendered HTML in the notebook.
416 """
--> 417 self._compute()
418 d = self._translate()
419 # filter out empty styles, every cell will have a class
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/formats/style.py in _compute(self)
481 r = self
482 for func, args, kwargs in self._todo:
--> 483 r = func(self)(*args, **kwargs)
484 return r
485
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/formats/style.py in _apply(self, func, axis, subset, **kwargs)
489 data = self.data.loc[subset]
490 if axis is not None:
--> 491 result = data.apply(func, axis=axis, **kwargs)
492 else:
493 result = func(data, **kwargs)
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4150 if reduce is None:
4151 reduce = True
-> 4152 return self._apply_standard(f, axis, reduce=reduce)
4153 else:
4154 return self._apply_broadcast(f, axis)
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
4246 try:
4247 for i, v in enumerate(series_gen):
-> 4248 results[i] = func(v)
4249 keys.append(v.name)
4250 except Exception as e:
<ipython-input-155-3bf2a87dcd1f> in <lambda>(x)
14 df = pd.read_clipboard() #Copy and paste then do this
15 D_idx_color = df["color"].to_dict()
---> 16 df.style.apply(lambda x:"background-color: %s"%D_idx_color[x], subset=["color"])
/Users/jespinoz/anaconda/lib/python3.6/site-packages/pandas/core/generic.py in __hash__(self)
829 def __hash__(self):
830 raise TypeError('{0!r} objects are mutable, thus they cannot be'
--> 831 ' hashed'.format(self.__class__.__name__))
832
833 def __iter__(self):
TypeError: ("'Series' objects are mutable, thus they cannot be hashed", 'occurred at index color')
实际指向真实的东西。并记得最后删除它。