英文:
Typename and Struct definition in C++98
问题
在旧版C++标准中,使用struct
和typenames
存在问题。我遇到了以下错误:
错误:没有匹配的函数调用
这是在使用C++98标准
编译时出现的情况。但在使用C++11
及更高版本时,它按照我的期望工作。
我定义了一个带有template
的struct
,并在main()
函数中使用一些值初始化了Pair
。然后,我打印了结构的成员:
#include <iostream>
template <typename T>
struct Pair
{
T first = 0;
T second = 0;
};
int main()
{
Pair<int> p1 { 5, 6 };
std::cout << p1.first << ' ' << p1.second << '\n';
p1 = { 2, 3 };
std::cout << p1.first << ' ' << p1.second << '\n';
return 0;
}
GDB编译器告诉我:
main.cpp: 在函数‘int main()’中: main.cpp:12:25: 错误:没有匹配的函数调用‘Pair::Pair()’ 12 | Pair<int> p1 { 5, 6 };
但是,如果我们将struct
中成员的声明更改为以下内容:
struct Pair
{
T first;
T second;
};
那么它将正确工作。
有人可以解释为什么会出现这种情况吗?
英文:
There is a problem with usage of typenames
with struct
in the old C++ standard. I have the following error:
> error: no matching function for call to
This is what I get when compiling with C++98 standard
. With C++11
and newer, it works as I expect.
I defined a struct with a template
and in the main()
function I initialized the Pair
from my templated struct
with some values. Then I print the members of the struct:
#include <iostream>
template <typename T>
struct Pair
{
T first = 0;
T second = 0;
};
int main()
{
Pair<int> p1 { 5, 6 };
std::cout << p1.first << ' ' << p1.second << '\n';
p1 = { 2, 3 };
std::cout << p1.first << ' ' << p1.second << '\n';
return 0;
}
GDB Compiler tells me:
main.cpp: In function ‘int main()’:
main.cpp:12:25: error: no matching function for call to ‘Pair::Pair()’
12 | Pair<int> p1 { 5, 6 };
But if we change declaration of members in the struct to this:
struct Pair
{
T first;
T second;
};
It works correctly.
Can somebody explain why it works like this?
答案1
得分: 3
Default member initializers were a feature added to the language in C++11. That means that the = 0
part of your Pair
member declarations is ill-formed before C++11.
默认成员初始化器是在C++11中添加的功能。这意味着在C++11之前,您的Pair
成员声明中的= 0
部分是不合法的。
Uniform initialization syntax was also added in C++11, which means that Pair<int> p1 { 5, 6 }
and p1 = { 2, 3 }
are also ill-formed prior to C++11.
统一初始化语法也是在C++11中添加的,这意味着在C++11之前,Pair<int> p1 { 5, 6 }
和 p1 = { 2, 3 }
也是不合法的。
Changing the definition of Pair
as you did fixes the first issue, but not the second. To get the same functionality with C++98 you will need to add user-defined constructors to Pair
:
像您所做的那样更改Pair
的定义会解决第一个问题,但不解决第二个问题。要在C++98中获得相同的功能,您需要向Pair
添加用户定义的构造函数:
template <typename T>
struct Pair
{
T first;
T second;
Pair() : first(), second() {}
Pair(T f, T s) : first(f), second(s) {}
};
int main()
{
Pair<int> p1(5, 6);
std::cout << p1.first << ' ' << p1.second << '\n';
p1 = Pair<int>(2, 3);
std::cout << p1.first << ' ' << p1.second << '\n';
return 0;
}
英文:
Default member initializers were a feature added to the language in C++11. That means that the = 0
part of your Pair
member declarations is ill-formed before C++11.
Uniform initialization syntax was also added in C++11, which means that Pair<int> p1 { 5, 6 }
and p1 = { 2, 3 }
are also ill-formed prior to C++11.
Changing the definition of Pair
as you did fixes the first issue, but not the second. To get the same functionality with C++98 you will need to add user-defined constructors to Pair
:
template <typename T>
struct Pair
{
T first;
T second;
Pair() : first(), second() {}
Pair(T f, T s) : first(f), second(s) {}
};
int main()
{
Pair<int> p1(5, 6);
std::cout << p1.first << ' ' << p1.second << '\n';
p1 = Pair<int>(2, 3);
std::cout << p1.first << ' ' << p1.second << '\n';
return 0;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论