当我尝试将函数指示器提供给数组时,我在构建此代码时遇到问题?有什么想法吗?
编译器错误:
Types 'std::array<char, 6>' and 'char' are not compatible
这是我的代码:
void NextHash( std::array<char,6>* state ) {
std::string tablica = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int j = 5;
for( int i = 0; i < 36; i++ ) {
if( tablica[i] == state[j] ) {
if( i == 35 ) {
state[j] = tablica[0];
j--;
i=-1;
}
else{
state[j] = tablica[i+1];
i = tablica.size();
}
}
}
}
您将指向
std::array
的指针作为参数传入,但随后直接在其上使用索引运算符[]
,而不是首先取消引用指针([]
运算符是为原始指针类型和std::array
定义的,因此混淆(。
我建议更改您的函数以接受引用:
void NextHash( std::array<char,6>& state ) {
。或者继续使用array<char,6>*
但随后取消引用它:
if( tablica[i] == (*state)[j] ) {
...
(*state)[j] = tablica[0];
如果出于安全原因使用 std::array
而不是原始数组/指针,则应考虑使用 at
方法而不是索引运算符:
void NextHash( std::array<char,6>* state ) {
...
if( tablica[i] == state->at(j) ) {
...
state->at(j) = tablica[0]; // this is valid C++ as references can be assigned to
或:
void NextHash( std::array<char,6>& state ) {
...
if( tablica[i] == state.at(j) ) {
...
state.at(j) = tablica[0];