【问题标题】:What is the difference between those two ways? [duplicate]这两种方式有什么区别? [复制]
【发布时间】:2020-12-31 13:36:16
【问题描述】:

我只是想了解为什么我不能用这样的大括号创建对象 () 我不明白是什么问题

“BSNode.h”

#pragma once

#include <string>
#include <iostream>

template <class T>
class BSNode
{
public:
    BSNode() {
        
    }
    void printNodes() const {
        std::cout << "test" << std::endl;
    }
};

“main.cpp”

#include "BSNode.h"

int main()
{
    BSNode<int> bsnodeInt1();
    BSNode<int> bsnodeInt2;
    bsnodeInt1.printNodes();
    bsnodeInt2.printNodes();
    return 0;
}

我遇到了这两个错误

main.cpp 3 表达式必须有类类型

'.printNodes' 左边的 main.cpp 3 必须有类/结构/联合

【问题讨论】:

标签: c++


【解决方案1】:

BSNode&lt;int&gt; bsnodeInt1(); 声明一个函数。

BSNode&lt;int&gt; bsnodeInt2;定义了一个新变量,通过默认构造函数初始化。

【讨论】:

  • 所以我不能用大括号来调用一个空的构造函数吗?
  • 不,这很明确,如果您愿意,您可以这样做。一个 obj= A()
  • 从 C++11 开始,您可以使用 {} 代替 ()
猜你喜欢
  • 1970-01-01
  • 2020-08-13
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
相关资源
最近更新 更多