在模板类中构建一个类

huangapple go评论73阅读模式
英文:

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&lt;iostream&gt;
 /*2)*/ #include&lt;cstdio&gt;
 /*3)*/ #include&lt;stdlib.h&gt;
 /*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&lt;typename type&gt;
/*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&lt;out&gt; t;
/*33)*/    cout&lt;&lt;ob.i&lt;&lt;&quot; &quot;&lt;&lt;ob.ob.j&lt;&lt;endl;
/*34)*/    cout&lt;&lt;ob1.j&lt;&lt;endl;
/*35)*/    cout&lt;&lt;t.ob.j&lt;&lt;endl;
/*36)*/    cout&lt;&lt;t.ob1.i&lt;&lt;&quot; &quot;&lt;&lt;t.ob1.ob.j;
/*37)*/ }

The code shows the following error

      Line                        Error
     
      |24|  error: expected identifier before numeric constant
      |24|  error: expected &#39;,&#39; or &#39;...&#39; before numeric constant
      |25|  error: expected identifier before numeric constant
      |25|  error: expected &#39;,&#39; or &#39;...&#39; before numeric constant
            In function &#39;int main()&#39;:
      |35|  error: &#39;t.temp&lt;type&gt;::ob&lt;out&gt;&#39; does not have class type
      |36|  error: &#39;t.temp&lt;type&gt;::ob1&lt;out&gt;&#39; does not have class type
      |36|  error: &#39;t.temp&lt;type&gt;::ob1&lt;out&gt;&#39; 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 ().

huangapple
  • 本文由 发表于 2020年1月3日 20:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定