与构造对象的歧义

时间:2018-11-04 22:56:46

标签: c++

这是我写的一个示例:

# Make an example list
l <- list(data.frame(a = 1:3, b = 4:6), data.frame(a = 7:8, b = 8:9))
l
#> [[1]]
#>   a b
#> 1 1 4
#> 2 2 5
#> 3 3 6
#> 
#> [[2]]
#>   a b
#> 1 7 8
#> 2 8 9
# Get a dataframe from it
df <- do.call('rbind', l)
# Add the element numbers by repeating the indices of the list,
# each as many times as the number of rows in that element's dataframe
df$element_number <- rep(seq_along(l), times = sapply(l, nrow))
df
#>   a b element_number
#> 1 1 4              1
#> 2 2 5              1
#> 3 3 6              1
#> 4 7 8              2
#> 5 8 9              2

为什么private void MainForm_Load(object sender, EventArgs e) { int cusLooper = 0; int assyLooper = -1; //Load xml XmlDocument db = new XmlDocument(); db.Load("XML Template.xml"); XmlNodeList nl = db.GetElementsByTagName("Customer"); foreach (XmlNode node in nl) { XmlNode element = node; foreach(XmlNode node2 in node) { XmlNode node3 = node2; if (node2.Name == "Name") { treeView1.Nodes.Add(node3.InnerText); assyLooper = -1; } foreach (XmlNode node4 in node3) { if (node4.Name == "AssemblyNumber") { treeView1.Nodes[cusLooper].Nodes.Add(node4.InnerText); assyLooper++; } if (node4.Name == "Part") { XmlNode element2 = node4; foreach (XmlNode final in element2) { if (final.Name == "PartNumber") { TreeNode nd = treeView1.Nodes[cusLooper].Nodes[assyLooper].Nodes.Add(final.InnerText); nd.ImageIndex = 7; nd.SelectedImageIndex = nd.ImageIndex; } } //assyLooper++; } } } cusLooper++; } } 尝试构造一个新对象struct Foo { Foo() = default; Foo(int) { }; }; int main() { int baz = 10; Foo(1); // OK Foo(baz); // Fails, redefinition return 0; } ,而不是构造一个将参数Foo(baz)传递给构造函数的匿名对象?当我通过编写baz来声明对象baz时,我得到了一个很好的默认初始化对象,但是一旦尝试传递参数,它就会失败。如何解决歧义?

1 个答案:

答案 0 :(得分:5)

Foo(baz);等效于Foo baz;,这显然是一个声明。

并且由于baz早已在相同范围中声明为类型int的局部变量,因此会出现重新定义错误。