英文:
Constructing a class in a template class
问题
以下是翻译好的部分:
我正在学习有关模板和typename关键字的知识,我在以下代码中遇到了错误:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
class out
{
public:
int i;
out(int i, int j) : i{i}, ob{j}{}
class in
{
public:
int j;
in(int j) : j{j}{}
} ob;
};
template<typename type>
class temp
{
public:
typename type::in ob(3);
type ob1(4, 4);
};
int main()
{
out ob(1, 1);
out::in ob1(2);
temp<out> t;
cout << ob.i << " " << ob.ob.j << endl;
cout << ob1.j << endl;
cout << t.ob.j << endl;
cout << t.ob1.i << " " << t.ob1.ob.j;
}
这是您提供的代码的翻译。如果您有关于代码中的错误或问题的疑问,可以提出并我将尽力回答。
英文:
I am studying about templates and typename keyword I am getting error in the following code:
/*1)*/ #include<iostream>
/*2)*/ #include<cstdio>
/*3)*/ #include<stdlib.h>
/*4)*/
/*5)*/ using namespace std;
/*6)*/
/*7)*/ class out
/*8)*/ {
/*9)*/ public:
/*10)*/ int i;
/*11)*/ out(int i,int j):i{i},ob{j}{}
/*12)*/ class in
/*13)*/ {
/*14)*/ public:
/*15)*/ int j;
/*16)*/ in(int j):j{j}{}
/*17)*/ }ob;
/*18)*/ };
/*19)*/
/*20)*/ template<typename type>
/*21)*/ class temp
/*22)*/ {
/*23)*/ public:
/*24)*/ typename type::in ob(3);
/*25)*/ type ob1(4,4);
/*26)*/ };
/*27)*/
/*28)*/ int main()
/*29)*/ {
/*30)*/ out ob(1,1);
/*31)*/ out::in ob1(2);
/*32)*/ temp<out> t;
/*33)*/ cout<<ob.i<<" "<<ob.ob.j<<endl;
/*34)*/ cout<<ob1.j<<endl;
/*35)*/ cout<<t.ob.j<<endl;
/*36)*/ cout<<t.ob1.i<<" "<<t.ob1.ob.j;
/*37)*/ }
The code shows the following error
Line Error
|24| error: expected identifier before numeric constant
|24| error: expected ',' or '...' before numeric constant
|25| error: expected identifier before numeric constant
|25| error: expected ',' or '...' before numeric constant
In function 'int main()':
|35| error: 't.temp<type>::ob<out>' does not have class type
|36| error: 't.temp<type>::ob1<out>' does not have class type
|36| error: 't.temp<type>::ob1<out>' does not have class type
=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 4 second(s)) ===
If i change the two lines
typename type::in ob(3);
type ob1(4,4);
To
typename type::in ob=typename type::in(3);
type ob1=type(4,4);
It will works fine and producing following output:
1 1
2
3
4 4
Process returned 0 (0x0) execution time : 0.847 s
Press any key to continue.
But i want to know why the error shows, How can i solve the error in above code Please help me?
Thanks for helping.
答案1
得分: 2
如果您想在类的定义中初始化变量,您必须使用赋值语法或花括号。不允许使用普通的括号。
typename type::in ob = typename type::in(3);
type ob1 = type(4, 4);
typename type::in ob{3};
type ob1{4, 4};
这与模板无关,对所有类都适用。其中一个原因是为了使编译器更容易解析。正如评论中提到的,最棘手的解析是一个示例,当需要区分初始化和函数声明时,可以使用{}
而不是()
.
英文:
If you want to initialize variables in the definition of a class you have to use assignement syntax or curly braces. Plain paranthesis is not allowed.
typename type::in ob=typename type::in(3);
type ob1=type(4,4);
typename type::in ob{3};
type ob1{4,4};
This is unrelated to templates and works the same for all classes. One of the reasons is to make parsing easier for the compiler. As mentioned in the comments most vexing parse is an example when disambiguating between an initialization and a function declaration can be done by using {}
instead of ()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论