-1

can't find an answer to this anywhere. haven't used c++ for long. in this file, 512, 256, 4736, and 448 all red-underlined as error 'expecting type specifier'

// AttackSqrs.h
#include <valarray> 
#include <vector>
#include <unordered_map> 
#include <string> 
#include <iostream>

class AttackSqrs
{
public:
    AttackSqrs();
    ~AttackSqrs();

private:
    void init();
    std::valarray<int> board(512);
    std::valarray<int> vrChessPieces(256);
    std::valarray<int> vrAttackSqrs(4736);
    std::valarray<int> vrNumAttackSqrsEachSqr(448);
};

but when i create this header file:

// diag.h
#include <valarray> 
#include <vector>
#include <unordered_map> 
#include <string> 
#include <iostream>


    void init();
    std::valarray<int> board(512);
    std::valarray<int> vrChessPieces(256);
    std::valarray<int> vrAttackSqrs(4736);
    std::valarray<int> vrNumAttackSqrsEachSqr(448);

the errors go away. thanks in advance for any help.

101010
  • 41,839
  • 11
  • 94
  • 168
codeLizt
  • 21
  • 1
  • 3

3 Answers3

1

You don't initialize members that way. Use the initialization list of the AttackSqrs constructor:

class AttackSqrs
{
    public:
    AttackSqrs();
    ~AttackSqrs();

private:
    void init();
    std::valarray<int> board;
    std::valarray<int> vrChessPieces;
    std::valarray<int> vrAttackSqrs;
    std::valarray<int> vrNumAttackSqrsEachSqr;
};

// Then in a CPP file
AttackSqrs::AttackSqrs() :  board(512), vrChessPieces(256), vrAttackSqrs(4736), 
                 vrNumAttackSqrsEachSqr(448) {}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
1

On the header file, you only declare the variable and their types. Any kind of object construction is implemented in the cpp file.

AttackSqrs.h

class AttackSqrs
{
public:
    AttackSqrs();
    ~AttackSqrs();

private:
    void init();
    std::valarray<int> board;
    std::valarray<int> vrChessPieces;
    std::valarray<int> vrAttackSqrs;
    std::valarray<int> vrNumAttackSqrsEachSqr;
};

AttackSqrs.cpp

AttackSqrs::AttackSqrs() : 
        board(512), 
        vrChessPieces(256), 
        vrAttackSqrs(4736), 
        vrNumAttackSqrsEachSqr(448){

}
B.R.W.
  • 1,566
  • 9
  • 15
  • C++11 allows for initializing members in the class definition. – chris Aug 06 '14 at 00:00
  • That's new to me. You refer to using more template arguments, like Neil Kirk answered? – B.R.W. Aug 06 '14 at 00:02
  • I believe there is new C++11 syntax to call constructor of members in class definition, but I'm not familiar with it. – Neil Kirk Aug 06 '14 at 00:04
  • 1
    @BrunoWerminghoff, It's as simple as `int m_i = 5;` to initialize `m_i` to 5 by default. You can use copy-initialization or list-initialization, but not direct-initialization. – chris Aug 06 '14 at 00:38
0

Your question is about vectors but you use valarray, which I haven't used before. I will answer for vectors.

You need to set the size of the vectors in the constructor.

header file

class AttackSqrs
{
public:
    AttackSqrs();
    ~AttackSqrs();

private:
     std::vector<int> board(512);
};

cpp file

AttackSqrs::AttackSqrs()
: board(512)
{
}

Alternatively, as the sizes are constant, you can use arrays.

class AttackSqrs
{
public:
    AttackSqrs();
    ~AttackSqrs();

private:
    std::array<int, 512> board; // c++11
    int board[512]; // pre-c++11
};
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91