header
#include
class Int17Matrix3D {
public:
Int17Matrix3D(int x, int y, int z) : width_(x), height_(y), depth_(z) {
arr_ = new int32_t[kNumLenght_ * x * y * z];
std::memset(arr_, 0, kNumLenght_ * x * y * z * sizeof(int32_t));
}
~Int17Matrix3D() {
delete[] arr_;
}
Int17Matrix3D(const Int17Matrix3D& other) : width_(other.width_), height_(other.height_), depth_(other.depth_) {
arr_ = new int32_t[kNumLenght_ * width_ * height_ * depth_];
std::memcpy(arr_, other.arr_, kNumLenght_ * width_ * height_ * depth_ * sizeof(int32_t));
}
Int17Matrix3D& operator=(const Int17Matrix3D& other);
static Int17Matrix3D make_array(int x, int y, int z);
Int17Matrix3D& operator()(int x, int y, int z);
Int17Matrix3D& operator=(int32_t value);
private:
const int8_t kNumLenght_ = 17;
int32_t current_index_;
int32_t width_;
int32_t height_;
int32_t depth_;
int32_t* arr_;
bool TakeBit(const int32_t* value, uint32_t bit_position) const;
Int17Matrix3D& ClearBit(int32_t* value, int32_t bit_position);
Int17Matrix3D& SetBit(int32_t* value, int32_t bit_position);
int32_t ToDec(const int32_t* arr_, int32_t current_index_) const;
friend Int17Matrix3D operator+(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs);
friend std::ostream& operator<<(std::ostream& stream, const Int17Matrix3D& array);
};
Int17Matrix3D operator+(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs);
Int17Matrix3D operator-(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs);
Int17Matrix3D operator*(const Int17Matrix3D& lhs, const int32_t& rhs);
std::ostream& operator<<(std::ostream& stream, const Int17Matrix3D& array);
cpp file
#include
#include "Int17Matrix3D.h"
Int17Matrix3D& Int17Matrix3D::operator=(const Int17Matrix3D& other) {
if (this != &other) {
delete[] arr_;
width_ = other.width_;
height_ = other.height_;
depth_ = other.depth_;
arr_ = new int32_t[kNumLenght_ * width_ * height_ * depth_];
std::memcpy(arr_, other.arr_, kNumLenght_ * width_ * height_ * depth_ * sizeof(int32_t));
}
return *this;
}
static Int17Matrix3D make_array(int x, int y, int z) {
return Int17Matrix3D(x, y, z);
}
Int17Matrix3D& Int17Matrix3D::operator()(int x, int y, int z) {
current_index_ = GetIndex(x, y, z);
return *this;
}
Int17Matrix3D& Int17Matrix3D::operator=(int32_t value) {
int first_bit_index = current_index_ * kNumLenght_;
for (int bit = 0; bit < kNumLenght_; ++bit) {
if (value & (1u << bit)) {
SetBit(arr_, first_bit_index + bit);
} else {
ClearBit(arr_, first_bit_index + bit);
}
}
return *this;
}
bool Int17Matrix3D::TakeBit(const int32_t* value, int bit_position) const {
int array_index = bit_position / 32;
int bit_index = bit_position % 32;
return ((value[array_index] >> bit_index) & 1) != 0;
}
Int17Matrix3D& Int17Matrix3D::ClearBit(int32_t* value, int32_t bit_position) {
value[bit_position / 32] &= ~(1 << (bit_position % 32));
return *this;
}
Int17Matrix3D& Int17Matrix3D::SetBit(int32_t* value, int32_t bit_position) {
value[bit_position / 32] |= (1 << (bit_position % 32));
return *this;
}
int Int17Matrix3D::GetIndex(int x, int y, int z) const {
return x + y * (width_ * kNumLenght_) + z * (width_ * height_ * kNumLenght_);
}
int32_t Int17Matrix3D::ToDec(const int32_t* arr_, int32_t current_index_) const {
int first_bit_index = current_index_ * kNumLenght_;
int32_t decimal_value = 0;
int32_t exp = 1;
for (int i = 0; i < kNumLenght_; ++i) {
if (TakeBit(arr_, first_bit_index + i)) {
decimal_value += exp;
}
exp *= 2;
}
return decimal_value;
}
Int17Matrix3D operator+(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs) {
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
for (int z = 0; z < lhs.depth_; ++z) {
for (int y = 0; y < lhs.height_; ++y) {
for (int x = 0; x < lhs.width_; ++x) {
int index = lhs.GetIndex(x, y, z);
int val_lhs = lhs.ToDec(lhs.arr_, index);
int val_rhs = rhs.ToDec(rhs.arr_, index);
result(x, y, z) = val_lhs + val_rhs;
}
}
}
return result;
}
Int17Matrix3D operator-(const Int17Matrix3D& lhs, const Int17Matrix3D& rhs) {
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
for (int z = 0; z < lhs.depth_; ++z) {
for (int y = 0; y < lhs.height_; ++y) {
for (int x = 0; x < lhs.width_; ++x) {
int index = lhs.GetIndex(x, y, z);
int val_lhs = lhs.ToDec(lhs.arr_, index);
int val_rhs = rhs.ToDec(rhs.arr_, index);
result(x, y, z) = val_lhs - val_rhs;
}
}
}
return result;
}
Int17Matrix3D operator*(const Int17Matrix3D& lhs, const int32_t& rhs) {
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
for (int z = 0; z < lhs.depth_; ++z) {
for (int y = 0; y < lhs.height_; ++y) {
for (int x = 0; x < lhs.width_; ++x) {
int index = lhs.GetIndex(x, y, z);
int val_lhs = lhs.ToDec(lhs.arr_, index);
result(x, y, z) = val_lhs * rhs;
}
}
}
return result;
}
std::ostream& operator<<(std::ostream& stream, const Int17Matrix3D& array) {
int32_t decimal_value = array.ToDec(array.arr_, array.current_index_);
stream << decimal_value;
return stream;
}
fix this errors
❯ clang++ Int17Matrix3D.cpp -o main
In file included from Int17Matrix3D.cpp:2:
./Int17Matrix3D.h:28:28: warning: default member initializer for non-static data member is a C++11 extension [-Wc++11-extensions]
const int8_t kNumLenght_ = 17;
^
Int17Matrix3D.cpp:21:20: error: use of undeclared identifier 'GetIndex'
current_index_ = GetIndex(x, y, z);
^
Int17Matrix3D.cpp:38:21: error: out-of-line definition of 'TakeBit' does not match any declaration in 'Int17Matrix3D'
bool Int17Matrix3D::TakeBit(const int32_t* value, int bit_position) const {
^~~~~~~
Int17Matrix3D.cpp:54:20: error: out-of-line definition of 'GetIndex' does not match any declaration in 'Int17Matrix3D'
int Int17Matrix3D::GetIndex(int x, int y, int z) const {
^~~~~~~~
Int17Matrix3D.cpp:93:28: error: 'width_' is a private member of 'Int17Matrix3D'
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
^
./Int17Matrix3D.h:30:11: note: declared private here
int32_t width_;
^
Int17Matrix3D.cpp:93:40: error: 'height_' is a private member of 'Int17Matrix3D'
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
^
./Int17Matrix3D.h:31:11: note: declared private here
int32_t height_;
^
Int17Matrix3D.cpp:93:53: error: 'depth_' is a private member of 'Int17Matrix3D'
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
^
./Int17Matrix3D.h:32:11: note: declared private here
int32_t depth_;
^
Int17Matrix3D.cpp:95:27: error: 'depth_' is a private member of 'Int17Matrix3D'
for (int z = 0; z < lhs.depth_; ++z) {
^
./Int17Matrix3D.h:32:11: note: declared private here
int32_t depth_;
^
Int17Matrix3D.cpp:96:29: error: 'height_' is a private member of 'Int17Matrix3D'
for (int y = 0; y < lhs.height_; ++y) {
^
./Int17Matrix3D.h:31:11: note: declared private here
int32_t height_;
^
Int17Matrix3D.cpp:97:31: error: 'width_' is a private member of 'Int17Matrix3D'
for (int x = 0; x < lhs.width_; ++x) {
^
./Int17Matrix3D.h:30:11: note: declared private here
int32_t width_;
^
Int17Matrix3D.cpp:100:27: error: 'ToDec' is a private member of 'Int17Matrix3D'
int val_lhs = lhs.ToDec(lhs.arr_, index);
^
./Int17Matrix3D.h:41:11: note: declared private here
int32_t ToDec(const int32_t* arr_, int32_t current_index_) const;
^
Int17Matrix3D.cpp:100:37: error: 'arr_' is a private member of 'Int17Matrix3D'
int val_lhs = lhs.ToDec(lhs.arr_, index);
^
./Int17Matrix3D.h:33:12: note: declared private here
int32_t* arr_;
^
Int17Matrix3D.cpp:101:27: error: 'ToDec' is a private member of 'Int17Matrix3D'
int val_rhs = rhs.ToDec(rhs.arr_, index);
^
./Int17Matrix3D.h:41:11: note: declared private here
int32_t ToDec(const int32_t* arr_, int32_t current_index_) const;
^
Int17Matrix3D.cpp:101:37: error: 'arr_' is a private member of 'Int17Matrix3D'
int val_rhs = rhs.ToDec(rhs.arr_, index);
^
./Int17Matrix3D.h:33:12: note: declared private here
int32_t* arr_;
^
Int17Matrix3D.cpp:112:28: error: 'width_' is a private member of 'Int17Matrix3D'
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
^
./Int17Matrix3D.h:30:11: note: declared private here
int32_t width_;
^
Int17Matrix3D.cpp:112:40: error: 'height_' is a private member of 'Int17Matrix3D'
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
^
./Int17Matrix3D.h:31:11: note: declared private here
int32_t height_;
^
Int17Matrix3D.cpp:112:53: error: 'depth_' is a private member of 'Int17Matrix3D'
Int17Matrix3D result(lhs.width_, lhs.height_, lhs.depth_);
^
./Int17Matrix3D.h:32:11: note: declared private here
int32_t depth_;
^
Int17Matrix3D.cpp:114:27: error: 'depth_' is a private member of 'Int17Matrix3D'
for (int z = 0; z < lhs.depth_; ++z) {
^
./Int17Matrix3D.h:32:11: note: declared private here
int32_t depth_;
^
Int17Matrix3D.cpp:115:29: error: 'height_' is a private member of 'Int17Matrix3D'
for (int y = 0; y < lhs.height_; ++y) {
^
./Int17Matrix3D.h:31:11: note: declared private here
int32_t height_;
^
Int17Matrix3D.cpp:116:31: error: 'width_' is a private member of 'Int17Matrix3D'
for (int x = 0; x < lhs.width_; ++x) {
^
./Int17Matrix3D.h:30:11: note: declared private here
int32_t width_;
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
1 warning and 20 errors generated.