C++ 如何在构造函数中将数组作为参数传递?c++;

C++ 如何在构造函数中将数组作为参数传递?c++;,c++,arrays,pointers,parameters,constructor,C++,Arrays,Pointers,Parameters,Constructor,我试图为类调用创建一个构造函数,其中4个数组作为参数传递。我尝试过使用*,&,以及数组本身;但是,当我将参数中的值分配给类中的变量时,会出现以下错误: call.cpp: In constructor ‘call::call(int*, int*, char*, char*)’: call.cpp:4:15: error: incompatible types in assignment of ‘int*’ to ‘int [8]’ call.cpp:5:16: error: incomp

我试图为类调用创建一个构造函数,其中4个数组作为参数传递。我尝试过使用
*,&
,以及数组本身;但是,当我将参数中的值分配给类中的变量时,会出现以下错误:

 call.cpp: In constructor ‘call::call(int*, int*, char*, char*)’:
 call.cpp:4:15: error: incompatible types in assignment of ‘int*’ to ‘int [8]’
 call.cpp:5:16: error: incompatible types in assignment of ‘int*’ to ‘int [8]’
 call.cpp:6:16: error: incompatible types in assignment of ‘char*’ to ‘char [14]’
 call.cpp:7:16: error: incompatible types in assignment of ‘char*’ to ‘char [14]’  
如果你能帮我发现错误并帮我改正,我将不胜感激。 这是我的密码:

.h文件 .cpp文件
原始数组是不可分配的,通常很难处理。但您可以将数组放入
结构中,并对其进行赋值或初始化。本质上,这就是
std::array
的含义

你能行

typedef std::array<int, 8>   num_t;
typedef std::array<char, 14> time_t;

class call_t
{
private:
    num_t    from_;
    num_t    dest_;
    time_t   init_;
    time_t   end_;

public:
    call_t(
        num_t const&     from,
        num_t const&     dest,
        time_t const&    init,
        time_t const&    end
        )
        : from_t( from ), dest_( dest ), init_( init ), end_( end )
    {}
};
typedef std::array num\t;
typedef std::数组时间;
课堂呼叫
{
私人:
来自的数量;
数量目的地;
时间初始化;
时间结束了;
公众:
打电话(
数量常数和起始值,
数量常数和目标,
时间常数和初始值,
时间常数和结束
)
:from_t(from)、dest_(dest)、init_(init)、end_(end)
{}
};
但这仍然缺乏一些基本的抽象,因此它只是一个技术解决方案

为了改进事物,考虑什么例如代码> NoMutht < /代码>真的是。也许是电话号码吧?然后对其进行建模


还可以考虑使用标准库容器
std::vector
,对于C/C++中的
char
std::string
,数组,不能通过执行
this->FROMNU=FROMNU来分配数组

另一半是尝试为数组分配指针。即使你把数组传递给函数,它们也会退化为指向第一个元素的指针,尽管你在定义中说了什么。

< P> <强>在C++中传递一个原始数组作为参数是可能的。< /强>

考虑以下代码:

template<size_t array_size>
void f(char (&a)[array_size])
{
    size_t size_of_a = sizeof(a); // size_of_a is 8
}

int main()
{
    char a[8];
    f(a);
}
模板
空f(字符(&a)[数组大小])
{
size\u t size\u of_a=sizeof(a);//size\u of_a是8
}
int main()
{
chara[8];
f(a);
}

std::array
std::tr1::array
替换数组,如果您没有C++11支持(或者相反,
boost::array
),数组与指针不同,尽管在许多情况下它们被降级为指针。对于用例,考虑<代码> STD::数组< /C> >而不是<代码> []/Cord>数组。您知道变量可以是小写的,对吗?在代码示例中,您使用C++ 11的代码<代码> Auto <代码>。谁有权访问C++11并使用它来代替std::array?“auto”在这里并不重要。我将其替换为明确的“大小”
typedef std::array<int, 8>   num_t;
typedef std::array<char, 14> time_t;

class call_t
{
private:
    num_t    from_;
    num_t    dest_;
    time_t   init_;
    time_t   end_;

public:
    call_t(
        num_t const&     from,
        num_t const&     dest,
        time_t const&    init,
        time_t const&    end
        )
        : from_t( from ), dest_( dest ), init_( init ), end_( end )
    {}
};
template<size_t array_size>
void f(char (&a)[array_size])
{
    size_t size_of_a = sizeof(a); // size_of_a is 8
}

int main()
{
    char a[8];
    f(a);
}