在 c++ 中定义一个空构造函数

Define an empty constructor in c++

本文关键字:一个 构造函数 c++ 定义      更新时间:2023-10-16

In main.cpp

LLC* llc = new LLC();

这是 llc 中的构造函数.cpp

LLC::LLC() :
{
    cout<<"test"<<endl;
}

这是我得到的错误:

llc.cpp(36): error: expected an identifier
{
^

我犯了什么错误?构造函数在 llc 头文件中的类 LLC 的公共部分中给出.cpp

LLC::LLC() :
{
    cout<<"test"<<endl;
}

应该是

LLC::LLC()
{
    cout<<"test"<<endl;
}

通过添加 : 在构造函数中指定成员初始化列表。这就是为什么编译器期望在那里有一个标识符。如果您不打算初始化其中的任何成员,则应删除该:

LLC::LLC() // Absense of :
{
    cout<<"test"<<endl;
}
相关文章: