Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有结构数组的类构造函数(直接)_C++_Arrays_Class_Initialization_Structure - Fatal编程技术网

C++ 具有结构数组的类构造函数(直接)

C++ 具有结构数组的类构造函数(直接),c++,arrays,class,initialization,structure,C++,Arrays,Class,Initialization,Structure,更新: 我看到我在这里制造了很多混乱,关于那个家伙。。谢谢罗曼关于向量的想法^^ 澄清我的问题:我想通过将整数作为参数传递(我不想为dem数组创建全局或局部变量)并将它们复制到类成员中,从而将结构数组straigth放入已创建的类(在main()中) 问题是我在初始化类时失败,我的尝试: struct _Note { int nFrequenz; int nTakt; }; class _Chart { public: _Chart( std::vector<_N

更新:

我看到我在这里制造了很多混乱,关于那个家伙。。谢谢罗曼关于向量的想法^^

澄清我的问题:我想通过将整数作为参数传递(我不想为dem数组创建全局或局部变量)并将它们复制到类成员中,从而将结构数组straigth放入已创建的类(在main()中)

问题是我在初始化类时失败,我的尝试:

struct _Note
{
    int nFrequenz;
    int nTakt;
};

class _Chart
{
public:
    _Chart( std::vector<_Note> vNotes )
    {
        for( int i = 0; i < vNotes.size(); i++ )
            this->_chart.push_back( vNotes[i] ); 
    }

private:
    std::vector<_Note> _chart;
};

void main()
{
    _Chart x(
        { {1,2}, {2,3} } 
    ); 
    // not working, wrong initialization
    // still confused about the brackets ^^

    getchar();
}
struct\u注释
{
国际NF频段;
int nTakt;
};
类图
{
公众:
_图表(标准::矢量vNotes)
{
对于(int i=0;i图表。推回(vNotes[i]);
}
私人:
std::矢量图;
};
void main()
{
_图表十(
{ {1,2}, {2,3} } 
); 
//不工作,初始化错误
//仍然对括号感到困惑^^
getchar();
}

当作为参数传递时
struktur p[]
衰减为指针(类似于
struktur*p
)。这个指针的大小肯定不是您在确定元素数量时所需要的大小


相反,为什么不提供一个类似于标准容器的双迭代器构造函数呢?

数组在传递时会衰减为指针。因此,必须将数组中元素的计数作为附加参数传递给函数。然而,在这个场景中,您有一个选择


glob
定义移动到
klasse
声明上方。然后在构造函数中,您应该能够获得具有
sizeof(glob)/sizeof(struktur)的对象计数。之后您应该能够动态分配并进行成员复制。

为什么不使用std::vector容器模板

class klasse
{
public:
    klasse(std::vector<struktur>& strs)
    {
        _chart = new struktur[strs.size()];
        for( int i = 0; i < strs.size(); i++ )
        {
            this->_chart[i] = strs[i];
        }
    }

private:
    struktur* _chart;
};
CLASSE类
{
公众:
klasse(标准::向量和标准测试)
{
_图表=新结构图[strs.size()];
对于(int i=0;i_图[i]=strs[i];
}
}
私人:
结构图;
};
总的来说():

void main()
{
std::向量myStructs;
myStruct.push_back(glob[0]);
myStruct.push_back(glob[1]);
myStruct.push_back(glob[2]);
klasse y(myStructs);
getchar();
}
这是简单而优雅的


[Upd.]是的,在您的示例中,
sizeof(p)
将等于
sizeof(uintptr\t
),因为p是指针类型。

您还需要传递元素计数:

klasse( struktur p[], size_t count )
{
    _chart = new struktur[count];
    // copy array in one shot as no deep copy is needed here.
    memcpy(_chart, p, count * sizeof(struktur));
}

如果没有为数组指定长度,您希望
sizeof(p)
是什么???最好使用
std::vector
来修复此问题。我会尝试将参数类型设置为
klasse
const,并尝试使用赋值运算符而不是()。
main()
返回
int
,而不是
void
。此外,以下划线开头,后跟大写字符的名称将保留给实现(编译器和标准库)。请避免通过完全销毁原始问题来编辑问题。这将使任何现有答案无效。更愿意放置另一个更新部分来指定更改,或者指定对您有用的内容!!你的“括号”不起作用。只需创建一个
\u注意
的实例,将其添加到
main()
中的向量中,然后将其传递给构造函数。是的,如果您不想复制向量两次,我郑重建议您通过引用获取
vNodes
。是的,
sizeof(p)==sizeof(uintpttr\t)
这对于x86机器是4为什么不首先使用
std::vector
作为成员?是的。但问题是“如何初始化”,我刚刚演示了如何初始化。std::vector作为成员,std::copy也可以。
klasse( struktur p[], size_t count )
{
    _chart = new struktur[count];
    // copy array in one shot as no deep copy is needed here.
    memcpy(_chart, p, count * sizeof(struktur));
}