在C ++中初始化类变量float数组

时间:2019-03-27 21:20:08

标签: c++ arrays class

我正在尝试初始化类变量数组。据我了解,普通的类变量初始化是这样的:

class test
{
public:
    static const float f[2];
};
//const float test::f = { 1 ,2};

但是,作为数组,它突然不起作用:

public static void CopyGridViewToClipboard(DataGridView gvCopy)
    {
        if (gvCopy == null) return;
        StringBuilder s = new StringBuilder();

        int offset = gvCopy.ColumnHeadersVisible ? 1 : 0;
        int visibleColumnsCount = 0;

        //count visible columns and build mapping between each column and it's display position
        Dictionary<int, int> indexMapping = new Dictionary<int, int>();
        int currIndex = 0;
        int lastFoundMinDisplayIndex = -1;
        for (int j = 0; j < gvCopy.ColumnCount; j++)
        {
            //find min DisplayIndex >= currIndex where column is visible
            int minDisplayIndex = 100000;
            int minDisplayIndexColumn = 100000;
            for (int k = 0; k < gvCopy.ColumnCount; k++)
            {
                if ((gvCopy.Columns[k].Visible) && (gvCopy.Columns[k].DisplayIndex >= currIndex) && (gvCopy.Columns[k].DisplayIndex > lastFoundMinDisplayIndex))
                {
                    if (gvCopy.Columns[k].DisplayIndex < minDisplayIndex)
                    {
                        minDisplayIndex = gvCopy.Columns[k].DisplayIndex;
                        minDisplayIndexColumn = k;
                    }
                }
            }

            if (minDisplayIndex == 100000) break;

            indexMapping.Add(minDisplayIndexColumn, currIndex);

            lastFoundMinDisplayIndex = minDisplayIndex;
            currIndex++;
        }
        visibleColumnsCount = currIndex;

        //put data in temp array -- required to position columns in display order
        string[,] data = new string[gvCopy.RowCount + offset, visibleColumnsCount];

        if (gvCopy.ColumnHeadersVisible)
        {
            for (int j = 0; j < gvCopy.ColumnCount; j++)
            {
                if (gvCopy.Columns[j].Visible)
                {
                    data[0, indexMapping[j]] = gvCopy.Columns[j].HeaderText;
                }
            }
        }

        for (int i = 0; i < gvCopy.RowCount; i++)
        {
            for (int j = 0; j < gvCopy.ColumnCount; j++)
            {
                if (gvCopy.Columns[j].Visible)
                {
                    data[i + offset, indexMapping[j]] = gvCopy[j, i].FormattedValue.ToString();
                }
            }
        }

        //copy data
        for (int i = 0; i < gvCopy.RowCount + offset; i++)
        {
            for (int j = 0; j < visibleColumnsCount; j++)
            {
                s.Append(data[i, j]);

                s.Append("\t");
            }
            s.Append("\r\n");
        }
        Clipboard.SetDataObject(s.ToString());
    }

整个代码应该可以工作,但是我注释掉了第6行。在第4行中,它仍然抛出

  

错误LNK2001无法解析的外部符号“ public:静态浮点const *   const test :: f“

我尝试了指针和动态分配,但两者均不起作用。我该如何解决此错误,第6行有什么问题吗?

2 个答案:

答案 0 :(得分:1)

就像@NathanOliver所说的那样,您可以在类之外的初始化中添加大小:

class test                                                                   
{                                                                            
public:                                                                      
    static const float f[2];                                                 
};                                                                           
...                                                                          
const float test::f[2] = {1, 2};

或者,您也可以使用constexpr并在类本身内声明数组:

class test                                                                  
{                                                                           
public:                                                                     
    static constexpr float f[2] = {1, 2};                                   
};

答案 1 :(得分:1)

说明

static test::f表示f未绑定到test类实例。换句话说,只有一个单一的f,就像testnamespace而不是class一样。 f只有一个,也只有一个。

注意

此外,您忘记添加必须在编译时指定的f数组的大小。 您还可以始终使用decltype修饰符,该修饰符将自动为您提供正确的成员类型。

基本示例

#include <iostream>

class test
{
public:
    static const float f[2];
};

decltype(test::f) test::f = {1, 2};

int main(){
    // Outputs: 1, 2
    std::cout << test::f[0] << ", " << test::f[1];
}

现代示例

更现代的解决方案将使用std::arrayinitializer listauto占位符类型说明符和constexpr说明符。在这种情况下,您根本不需要提供数组大小。

#include <iostream>
#include <array>

class test
{
public:
    static constexpr auto f = std::array {1.f, 2.f};
};

int main(){
    // Outputs: 1, 2
    std::cout << test::f[0] << ", " << test::f[1];
}