小编Sva*_*ana的帖子

如何将数组传递给构造函数?

我想将一个数组传递给构造函数,但只传递第一个值 - 其余的看起来像垃圾.

这是我正在研究的简化版本:

#include <iostream>

class board
{
    public:
        int state[64];
        board(int arr[])
        {
            *state = *arr;
        }
        void print();
};

void board::print()
{
    for (int y=0; y<8; y++)
    {
        for (int x=0; x<8; x++)
            std::cout << state[x + y*8] << " ";
        std::cout << "\n";
    }
}

int main()
{
    int test[64] = {
        0, 1, 2, 3, 4, 5, 6, 7,
        1, 2, 3, 4, 5, 6, 7, 8,
        2, 3, 4, 5, 6, 7, 8, 9, …
Run Code Online (Sandbox Code Playgroud)

c++ arrays constructor visual-c++

15
推荐指数
3
解决办法
4万
查看次数

如何从位移中获得"丢失"位?

我想比特移位一个变量并存储一个在布尔中移出的位.

就像是:

unsigned int i = 1;
bool b = rshift(&i); // i now equals 0 and b is set to true
Run Code Online (Sandbox Code Playgroud)

如何实现这一目标?

c++ bit-manipulation visual-c++

10
推荐指数
2
解决办法
7022
查看次数

为什么我得到"矢量迭代器+偏移超出范围"错误?

以下代码导致此错误:

if (bestLine.size() > searchDepth - depth)
    bestLine.erase(bestLine.begin(), bestLine.end() - searchDepth - depth);
Run Code Online (Sandbox Code Playgroud)

当我检查searchDepth - depth错误时的值时,它是0.

基本上,

if (bestLine.size() > 0)
    bestLine.erase(bestLine.begin(), bestLine.end());
Run Code Online (Sandbox Code Playgroud)

导致此错误.(或者不是.请参阅下面的评论.)

据我所知,上面的代码应该擦除整个向量,这是在这种情况下的期望行为.

我究竟做错了什么?

c++ vector visual-c++

3
推荐指数
1
解决办法
9107
查看次数

为什么我无法声明向量全局变量?

在我添加之前,所有内容都在编译 vector<Move> bestLine;

search.h

#ifndef SEARCH_H
#define SEARCH_H

#include <vector>
#include "types.h"
#include "position.h"
#include "move.h"
#include "moves.h"

U8 searchDepth;
U8 searchCount;
vector<Move> bestLine; // The compiler doesn't like this.

void searchPosition(Position &P, U8 depth);
void searchMove(Move &M);

#endif
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

1>d:\test\search.h(12): error C2143: syntax error : missing ';' before '<'
1>d:\test\search.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\test\search.cpp(30): error C2065: 'bestLine' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)

似乎编译器没有识别Move,所以bestLine没有被声明.我认为这可能是一个循环依赖,并尝试声明一个不完整的类型Move,但它没有任何影响.有人可以解释我错过的东西吗?

c++ variables vector global-variables visual-c++

0
推荐指数
1
解决办法
5732
查看次数