I have found out through the compiler that you cannot declare and initialize an array within a class header file. I was trying to figure out how I might go about getting a 5 by 14 array initialized as a private mamber within the class. I have included the header file and the class cpp file, neither are in full working form, but you should be able to get the idea about what i am trying to accomplish. thanks for taking a look.

Dani AI

Generated

correctly observed a compiler error when trying to initialize a non‑static C array inside a class declaration on older compilers. That behavior is historical: pre‑C++11 compilers do not allow in‑class initialization of non‑static array members. pointed in the right direction — initialize the 5×14 array inside the constructor. ’s hint about runtime containers is also useful: modern code often prefers std::array or std::vector for safer initialization and assignment.

Example approaches (5×14 array):

Constructor initialization (works on old compilers)

// Vend.h
class Vend {
    int fasm[5][14];
public:
    Vend();
};

// Vend.cpp
#include <cstring> // for std::memcpy
static const int default_fasm[5][14] = { /* fill rows here */ };

Vend::Vend() {
    std::memcpy(fasm, default_fasm, sizeof(fasm)); // copies POD data efficiently
}

Modern C++ (recommended): use std::array or in‑class member initializer (C++11+)

#include <array>

class Vend {
    using Row = std::array<int,14>;
    std::array<Row,5> fasm = { Row{ /* row0 */ }, Row{ /* row1 */ }, /* ... */ };
};

Troubleshooting notes and recommendations:

  • If the compiler errors on in‑class initializers, compile with a modern standard (g++/clang: -std=c++11 or newer).
  • Arrays don’t support assignment; prefer std::array when copy/assignment is needed.
  • For simple zeroing, std::memset or value initialization (int fasm[5][14] = {};) in the constructor works.
  • For large static defaults that never change, declare the array static in the class and define/initialize it in the .cpp file.

These options cover both historical toolchains and current best practices while matching the 5×14 requirement raised by .

Recommended Answers

All 2 Replies

you cannot declare and initialize an array in header file.but u can crate as many as u want object of any class member in your .c++ file so using this u can create run time array. think on this

All you have to do is move or more specifically code appropriately in Vend::Vend (default constructor) the values you want in fasm. This is how variables are initialized in a class, like int value [1][2] = {4, 8}; would be too code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.