С将静态类结构具有字符串变量,而在c ++中没有对象

问题描述 投票:0回答:1

必须以代码中描述的格式接收数据,但是问题在于字符串变量的操作。如何避免错误?

class Class1
{
public:
    Class1() = delete ;
    const static int x = 1;
    static struct ZZ {
        const static int z1 = 1;
        const static QString z2 = "word";
    } z;
};

int main(int argc, char* argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << Class1::z.z2;
    return a.exec();
}

错误:

class1.h:13: ошибка: in-class initialization of static data member 'const QString Class1::ZZ::z2' of non-literal type
         const static QString z2 = "word";
                              ^~
class1.h:13: ошибка: call to non-constexpr function 'QString::QString(const char*)'
         const static QString z2 = "word";
                                   ^~~~~~

Source github.com

c++ static qt5
1个回答
0
投票

免责声明:

我的第一印象是此代码滥用了classstruct的实际用途。

这就是说,答案集中在OP代码中与namespace数据成员有关的技术问题。


为了重现该问题,我举了以下类似示例:

static

输出:

#include <iostream>
#include <string>

class Class1
{
public:
    Class1() = delete ;
    const static int x = 1;
    static struct ZZ {
        static const int z1 = 1;
        static const std::string z2 = "word";
    } z;
};

int main(int argc, char* argv[])
{
    std::cout << Class1::z.z2;
}

main.cpp:11:34: error: in-class initialization of static data member 'const string Class1::ZZ::z2' of non-literal type 11 | static const std::string z2 = "word"; | ^~ main.cpp:11:39: error: temporary of non-literal type 'const string' {aka 'const std::__cxx11::basic_string<char>'} in a constant expression 11 | static const std::string z2 = "word"; | ^~~~~~

恕我直言,这已经足够接近OP的问题。

TL; DR:问题在于,必须明确定义Live Demo on coliru成员变量(非内联)或将其标记为static

因此,我的修复程序如下所示:

inline

输出:

#include <iostream>
#include <string>

class Class1
{
public:
    Class1() = delete ;
    const static int x = 1;
    inline static struct ZZ {
        static const int z1 = 1;
        inline static const std::string z2 = "word";
    } z = ZZ{};
};

int main(int argc, char* argv[])
{
    std::cout << Class1::x << '\n'
      << Class1::z.z1 << '\n'
      << Class1::z.z2 << '\n';
}

1 1 word

注意:

虽然Live Demo on coliruinline需要struct ZZ,但对于整数类型可以省去。因此,std::string将被接受而不会受到投诉(不以static const int z1 = 1;开头)。

进一步阅读:

cppreference.com上的inline


为了进行比较,相同的代码在没有Static data members的情况下也可以工作:

inline

输出:

#include <iostream>
#include <string>

class Class1
{
public:
    Class1() = delete ;
    const static int x = 1;
    static struct ZZ {
        static const int z1 = 1;
        static const std::string z2;
    } z;
};

//Class1::ZZ Class1::z;
const std::string Class1::ZZ::z2 = "word";

int main(int argc, char* argv[])
{
    std::cout << Class1::x << '\n'
      << Class1::z.z1 << '\n'
      << Class1::z.z2 << '\n';
}

1 1 word

注意:

[关于Live Demo on coliru,我不太确定。缺少的定义可能会被忽略掉,因为const Class1::ZZ Class1::z;本身从未使用过。

为了检查这一点,我再次修改了示例:

Class1::z

输出:

#include <iostream>
#include <string>

class Class1
{
public:
    Class1() = delete ;
    const static int x = 1;
    static struct ZZ {
        static const int z1 = 1;
        static const std::string z2;
    } z;
};

Class1::ZZ Class1::z;
const std::string Class1::ZZ::z2 = "word";

std::ostream& operator << (std::ostream &out, const Class1::ZZ &z)
{
  return out << z.z1 << ", " << z.z2;
}

int main(int argc, char* argv[])
{
    std::cout << Class1::x << '\n'
      << Class1::z << '\n'
      << Class1::z.z1 << '\n'
      << Class1::z.z2 << '\n';
}

1 1, word 1 word

注意:

注释Live Demo on coliru的定义(如前面的示例中的结果,导致链接错误。

© www.soinside.com 2019 - 2024. All rights reserved.