How to initialize nested struct in C++? - c++

Let's say I have following declaration in the C++:
struct Configuration {
struct ParametersSetA {
float param_A_01;
float param_A_02;
float param_A_03;
} parameters_set_A;
struct ParametersSetB {
float param_B_01;
float param_B_02;
} parameters_set_B;
};
Then somewhere in the code I have following definition:
Configuration config = {
.parameters_set_A = {
.param_A_01 = 1.0f,
.param_A_02 = 2.0f,
.param_A_03 = param_A_01 + param_A_02;
},
.parameters_set_B = {
.param_B_01 = 0.50f,
.param_B_02 = 0.75f
}
};
My question is whether the initialization (especially as far as the param_A_03 item in the nested struct ParametersSetA)
I have used above is correct in the C++?

The problem is that we can't use any of the unqualified names param_A_01 or param_A_02 or an expression that involves any of the two like param_A_01 + param_A_02 as initializer forparam_A_03.
Additionally, you have incorrectly put a semicolon ; after param_A_01 + param_A_02 instead of comma , . I've corrected both of these in the below shown modified program:
Method 1
//create constexpr variables
constexpr float A1 = 1.0f;
constexpr float A2 = 2.0f;
Configuration config = {
.parameters_set_A = {
.param_A_01 = A1,
.param_A_02 = A2,
//--------------vv---vv---->use those constexpr variable(also semicolon is removed from here)
.param_A_03 = A1 + A2
},
.parameters_set_B = {
.param_B_01 = 0.50f,
.param_B_02 = 0.75f
}
};
Working demo
Method 2
Other option is to use qualified names like config.parameters_set_A.param_A_01 and config.parameters_set_A.param_A_02 as shown below:
Configuration config = {
.parameters_set_A = {
.param_A_01 = 1.0f,
.param_A_02 = 2.0f,
//--------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv--->qualified name used here
.param_A_03 = config.parameters_set_A.param_A_01 + config.parameters_set_A.param_A_02
},
.parameters_set_B = {
.param_B_01 = 0.50f,
.param_B_02 = 0.75f
}
};
Working demo

Related

Vector Push_Back VS Emplace_Back

I have two different methods for adding elements to a vector.
GUI_Vertices.emplace_back();
GUI_Vertices.back().pos.x = ((float)x / 400) - 1.f;
GUI_Vertices.back().pos.y = ((float)y / 300) - 1.f;
GUI_Vertices.back().texCoord.x = u;
GUI_Vertices.back().texCoord.y = v;
GUI_Vertices.back().color.r = m_Color.r / 128;
GUI_Vertices.back().color.g = m_Color.g / 128;
GUI_Vertices.back().color.b = m_Color.b / 128;
GUI_Vertices.back().color.a = m_Color.a / 128;
The above code works, however I am forced to add a new element to the GUI_Vertices vector.
Vertex NewVertex;
NewVertex.pos.x = ((float)x / 400) - 1.f;
NewVertex.pos.y = ((float)y / 300) - 1.f;
NewVertex.texCoord.x = u;
NewVertex.texCoord.y = v;
NewVertex.color.r = m_Color.r / 128;
NewVertex.color.g = m_Color.g / 128;
NewVertex.color.b = m_Color.b / 128;
NewVertex.color.a = m_Color.a / 128;
GUI_Vertices.emplace_back(NewVertex);
The above code works sometimes and I can conditionally add the NewVertex into the GUI_Vertices vector if needed.
Here is the definition of Vertex:
struct Vertex {
glm::vec3 pos;
glm::vec4 color;
glm::vec2 texCoord;
static VkVertexInputBindingDescription getBindingDescription() {
VkVertexInputBindingDescription bindingDescription = {};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(Vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescription;
}
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions = {};
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Vertex, pos);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, color);
attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
return attributeDescriptions;
}
bool operator==(const Vertex& other) const {
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
namespace std {
template<> struct hash<Vertex> {
size_t operator()(Vertex const& vertex) const {
return ((hash<glm::vec3>()(vertex.pos) ^
(hash<glm::vec4>()(vertex.color) << 1)) >> 1) ^
(hash<glm::vec2>()(vertex.texCoord) << 1);
}
};
}
Later on in program execution, after adding all our Vertex elements to the GUI_Vertex vector I perform the following operation on GUI_Vertex:
memcpy(GUI_VertexAllocation->GetMappedData(), GUI_Vertices.data(), sizeof(Vertex) * GUI_Vertices.size());
I'm copying the memory from GUI_Vertices into a preallocated buffer which will be used by Vulkan to render our vertices.
Now i'm trying to figure out why the first method of adding Vertex objects into GUI_Vertices always works and the second method only sometimes works.
Here is a link to the entire project https://github.com/kklouzal/WorldEngine/blob/GUI_Indirect_Draw/Vulkan/VulkanGWEN.hpp
After recompiling the project the second method will occasionally work so I'm getting some undefined behavior here. I have checked the validity of GUI_Vertices up until the point where we do our memcpy and the data appears to be valid so I'm not sure whats going on.
I would like to get the second method working so I can conditionally add new vertices into the buffer.
NewVertex.pos.x = ((float)x / 400) - 1.f;
NewVertex.pos.y = ((float)y / 300) - 1.f;
...
glm::vec3 pos;
emplace_back will always perform value initialization on the object it creates, which initializes all of the data members. By contrast, Vertex NewVertex; will default-initialize the object, which leaves its members uninitialized (since the GLM types have trivial default constructors).
So pos.z is uninitialized. And your code doesn't initialize it yourself. So you're sending uninitialized garbage to the GPU.
If you create the object with Vertex NewVertex{};, then it will be value-initialized, just like emplace_back does.

Blender, Cycles Render, call to function cause app to crash

I am trying to get the get the color of each sample during Cycles rendering. In order to do so, I am calling the following function
void update_bcd_inputs(int x, int y, float sampleR, float sampleG, float sampleB){
float current_valueR = sStats->m_meanImage.getValue(bcd::PixelPosition(x, y), 0); // += sampleR;
float current_valueG = sStats->m_meanImage.getValue(bcd::PixelPosition(x, y), 1); // += sampleG;
float current_valueB = sStats->m_meanImage.getValue(bcd::PixelPosition(x, y), 2); // += sampleB;
sStats->m_meanImage.set(x, y, 0, current_valueR + sampleR);
sStats->m_meanImage.set(x, y, 1, current_valueG + sampleG);
sStats->m_meanImage.set(x, y, 2, current_valueB + sampleB);
}
in blender/intern/cycles/device/device_cpu.cpp
void path_trace(DeviceTask &task, RenderTile &tile, KernelGlobals *kg)
{
float *render_buffer = (float*)tile.buffer;
int start_sample = tile.start_sample;
int end_sample = tile.start_sample + tile.num_samples;
float inv_weight = 1.0f/tile.num_samples;
for(int sample = start_sample; sample < end_sample; sample++) {
if(task.get_cancel() || task_pool.canceled()) {
if(task.need_finish_queue == false)
break;
}
for(int y = tile.y; y < tile.y + tile.h; y++) {
for(int x = tile.x; x < tile.x + tile.w; x++) {
path_trace_kernel()(kg, render_buffer,
sample, x, y, tile.offset, tile.stride);
int step = tile.offset + x + y*tile.stride;
step *= kernel_data.film.pass_stride;
float sampleR = *(render_buffer +step);
float sampleG = *(render_buffer +step +1);
float sampleB = *(render_buffer +step +2);
update_bcd_inputs(x, y, sampleR*inv_weight, sampleG*inv_weight, sampleB*inv_weight);
}
}
tile.sample = sample + 1;
task.update_progress(&tile, tile.w*tile.h);
}
}
SamplesStatisticsImages sStats is an attribute of CPUDevice
struct SamplesStatisticsImages
{
SamplesStatisticsImages() = default;
SamplesStatisticsImages(int i_width, int i_height, int i_nbOfBins);
SamplesStatisticsImages(const SamplesStatisticsImages&) = default;
SamplesStatisticsImages(SamplesStatisticsImages&&) = default;
SamplesStatisticsImages& operator=(const SamplesStatisticsImages&) = default;
SamplesStatisticsImages& operator=(SamplesStatisticsImages&&) = default;
DeepImage<float> m_nbOfSamplesImage;
DeepImage<float> m_meanImage;
DeepImage<float> m_covarImage;
DeepImage<float> m_histoImage;
};
Here are the files DeepImage.hpp and DeepImage.h.
The problem is that the call to update_bcd_inputs makes Blender to crash without even trying to render an image. I just change the renderer to Cycles, try to add a new material and boom, it crashes.
I figured out it is the set function that is the problem (when I remove it, the app doesn't crash).
Can someone understand why ? I don't have strong C++ skills so I must be missing something.
Here is the crash log as well.
Thanks !
Edit: more precisions
Here is the modified device_cpu.cpp.
The sStats pointer is initialized with nullptr in the CPUDevice constructor
CPUDevice(DeviceInfo& info_, Stats &stats_, bool background_)
: Device(info_, stats_, background_),
texture_info(this, "__texture_info", MEM_TEXTURE),
histoParams(),
#define REGISTER_KERNEL(name) name ## _kernel(KERNEL_FUNCTIONS(name))
...
#undef REGISTER_KERNEL
{
...
sStats = nullptr;
}
and then allocated in
void thread_render(DeviceTask& task)
{
...
sStats = new bcd::SamplesStatisticsImages(task.w, task.h, histoParams.m_nbOfBins);
...
}
histoParams is an attribute of CPUDevice as well.
struct HistogramParameters
{
HistogramParameters() :
m_nbOfBins(20),
m_gamma(2.2f),
m_maxValue(2.5f) {}
int m_nbOfBins;
float m_gamma; ///< exponent for the exponential size increase of histogram bins
float m_maxValue;
};

c++: Proper way to initialize static fields of member struct

I got this:
// mouse.h
class Mouse {
private:
struct Pos {
static GLfloat x;
static GLfloat y;
};
static Pos last;
}
and this:
// mouse.cpp
// 1)
Mouse::Pos Mouse::last = {};
// 2)
Mouse::Pos Mouse::last = { 0.0, 0.0 };
// 3)
Mouse::last.x = 0.0f;
Mouse::last.y = 0.0f;
1), 2) and 3) are the attempts I've made at initializing that thing. I understand that the header should declare that last is static, and that the source should initialize it, but something has been wrong with all my attempts. Could someone please tell me the correct way to do such a thing? Am I missing some very important point? Is this nonsense? It is important that the fields are static. Thanks.
You don't need to declare Pos content as static.
// mouse.h
class Mouse {
private:
struct Pos {
GLfloat x;
GLfloat y;
};
static Pos last;
}
Mouse::Pos Mouse::last = { 0.0, 0.0 };
This should work too
It is important that the fields are static.
Then last will not have any state. It will simply refer to the static x and y values inside Mouse::Pos.
#include "mouse.h"
GLfloat Mouse::Pos::x = 10;
GLfloat Mouse::Pos::y = 10;
Mouse::Pos Mouse::last{};
wandbox example
The following asserts pass:
assert(Mouse::last.x == 10);
assert(Mouse::last.y == 10);

error C2146: syntax error : missing ';' before identifier 'ContextRecord'

i have a header file which contained all of the class' functions including code so the class didn't have a cpp file. everything worked. I added the cpp file and moved the function code over to that and now i get this error when compiling. the header that im getting the error in ((x86)\microsoft sdks\windows\v7.0a\include\winnt.h(6361)) isnt even included by the file that im changing. does anyone know what the reason for this might be? i can provide code i just don't know what would be helpful.
the cpp file:
#include "Fisherman.h"
void Fisherman::Initialise(){
memset((void*)&mListener, 0, sizeof(X3DAUDIO_LISTENER));
memset((void*)&mEmitter, 0, sizeof(X3DAUDIO_EMITTER));
memset((void*)&mDSPSettings, 0, sizeof(X3DAUDIO_DSP_SETTINGS));
XAUDIO2_VOICE_DETAILS details;
mCastSplash->GetSourceVoice()->GetVoiceDetails(&details);
mEmitter.ChannelCount = details.InputChannels;
mEmitter.CurveDistanceScaler = 1.0f;
X3DAUDIO_VECTOR emitterPos = { 0.0f, 0.0f, 0.0f};
mEmitter.Position = emitterPos;
X3DAUDIO_VECTOR emitterVel = { 0.0f, 0.0f, 0.0f };
mEmitter.Velocity = emitterVel;
mDSPSettings.SrcChannelCount = mEmitter.ChannelCount;
mDSPSettings.DstChannelCount = mXACore->GetChannelCount();
FLOAT32 * matrix = new FLOAT32[mDSPSettings.SrcChannelCount * mDSPSettings.DstChannelCount];
mDSPSettings.pMatrixCoefficients = matrix;
X3DAUDIO_VECTOR front = { 0.0f, 0.0f, 1.0f };
X3DAUDIO_VECTOR top = { 0.0f, 1.0f, 0.0f };
mListener.OrientFront = front;
mListener.OrientTop = top;
X3DAUDIO_VECTOR listenerVel = {0.0f, 0.0f, 0.0f};
mListener.Velocity = listenerVel;
X3DAUDIO_VECTOR listenerPos = { 0.0f, 0.0f, 0.0f };
mListener.Position = listenerPos;
}
void Fisherman::Rotate (int MouseDeltaX){
X3DAUDIO_VECTOR input = mListener.OrientFront;
X3DAUDIO_VECTOR result;
float theta = -(X3DAUDIO_PI/1000)*MouseDeltaX;
float cs = cos(theta);
float sn = sin(theta);
if(cs < 0.00001) cs = 0.0f;
result.x = input.x * cs - input.z * sn;
result.z = input.x * sn + input.z * cs;
result.y = 0.0f;
mListener.OrientFront = result;
}
bool Fisherman::Cast(Fish* aFish){
mCast->Play(0);
mCastOut = true;
mFish = aFish;
X3DAUDIO_VECTOR seg_v = Multiply(mListener.OrientFront, 30);
X3DAUDIO_VECTOR pt_v = {mFish->GetX(), 0.0f, mFish->GetZ()};
float proj_v_length = Dot(pt_v, mListener.OrientFront);
X3DAUDIO_VECTOR proj_v = Multiply(mListener.OrientFront, proj_v_length);
X3DAUDIO_VECTOR dist_v = Subtract(pt_v, proj_v);
if(VectorLength(dist_v) < mFish->GetRadius()){
mEmitter.Position = mFish->GetEmitter().Position;
return true;
}else{
mEmitter.Position = Multiply(mListener.OrientFront, 15);
return false;
}
}
void Fisherman::ReelIn(Fish* aFish){
mFish = aFish;
mFish->MoveCloser(mReelSpeed);
mReelingIn = true;
}
void Fisherman::ReelOut(Fish* aFish){
mFish = aFish;
mFish->MoveFurther();
mReelingIn = false;
}
void Fisherman::SetReelSpeed(float deltaTime){
float reelSpeed = 1.0f - deltaTime;
if(reelSpeed < 0.0f){
reelSpeed = 0.0f;
}
if(reelSpeed > 10){
mReelSpeedList.push_back(reelSpeed);
if(mReelSpeedList.size() > 3){
mReelSpeedList.pop_front();
}
reelSpeed = 0.0f;
std::list<float>::const_iterator iterator;
for (iterator = mReelSpeedList.begin(); iterator != mReelSpeedList.end(); ++iterator){
reelSpeed += *iterator;
}
mReelSpeed = reelSpeed/mReelSpeedList.size();
}else
mReelSpeed = reelSpeed;
mReelClickTimer = 0.1 / mReelSpeed;
}
void Fisherman::PlayReelClick(){
if(!mReelClick[0]->IsPlaying()){
mReelClick[0]->Play(0);
}else if(!mReelClick[1]->IsPlaying()){
mReelClick[1]->Play(0);
}else if(!mReelClick[2]->IsPlaying()){
mReelClick[2]->Play(0);
}else if(!mReelClick[3]->IsPlaying()){
mReelClick[3]->Play(0);
}else if(!mReelClick[4]->IsPlaying()){
mReelClick[4]->Play(0);
}else {
return;
}
}
bool Fisherman::NothingPlaying(){ // check to see if any sounds are playing
if(mCast->IsPlaying())return false;
if(mCastSplash->IsPlaying())return false;
for(int i =0; i < REEL_CLICK_OBJECTS; i++){
if(mReelClick[i]->IsPlaying()){
return false;
}
}
return true;
}
void Fisherman::SetReelingIn(bool isReelingIn){
mReelingIn = isReelingIn;
}
void Fisherman::SetCastOut(bool hasCastOut){
mCastOut = hasCastOut;
}
float Fisherman::GetReelClickTime(){
return mReelClickTimer/CLICK_TIMER_MULTIPLIER;
}
bool Fisherman::IsReelingIn(){
return mReelingIn;
}
float Fisherman::GetReelSpeed(){
return mReelSpeed;
}
X3DAUDIO_LISTENER Fisherman::GetListener(){
return mListener;
}
X3DAUDIO_EMITTER Fisherman::GetEmitter(){
return mEmitter;
}
X3DAUDIO_DSP_SETTINGS Fisherman::GetSettings(){
return mDSPSettings;
}
XASound* Fisherman::GetCastSound(){
return mCast;
}
XASound* Fisherman::GetCastSplashSound(){
return mCastSplash;
}
bool Fisherman::HasCastSplashed(){
return mCastSplashBool;
}
void Fisherman::SetCastSplashed(bool splashed){
mCastSplashBool = splashed;
}
bool Fisherman::IsCastOut(){
return mCastOut;
}
the header:
#ifndef _FISHERMAN_H_
#define _FISHERMAN_H_
#include <X3DAudio.h>
#include <math.h>
#include <list>
#include "VectorCalculations.h"
#include "Fish.h"
#include "XASound.hpp"
using AllanMilne::Audio::XASound;
const float CLICK_TIMER_MULTIPLIER = 2.5f;
const int REEL_CLICK_OBJECTS = 5;
class Fisherman{
public:
Fisherman(XACore* aXACore, XASound *cast, XASound *castSplash, std::list<XASound*> reelClickList)
: // a Fish Object pointer for the fisherman to interact with.
mFish (NULL), mXACore (aXACore),
//XASound objects with sounds for the fisherman
mCast (cast), mCastSplash (castSplash)
{
mReelSpeedList.clear();
for(int i = 0; i < REEL_CLICK_OBJECTS; i++){
mReelClick[i] = reelClickList.front();
mReelClick[i]->SetVolume(2.0f);
reelClickList.pop_front();
mReelClickList.push_back(mReelClick[i]);
}
mCastSplash->SetVolume(7.0f);
mXACore = aXACore;
mCastSplashBool = false;
mCastOut = false;
mReelingIn = false;
mCastDistance = 0.0f;
Initialise();
}
~Fisherman(){}
void Initialise();
void Rotate(int MouseDeltaX);
bool Cast(Fish* aFish);
void ReelIn(Fish* aFish);
void ReelOut(Fish* aFish);
void SetReelSpeed(float deltaTime);
void PlayReelClick();
bool NothingPlaying(); // check to see if any sounds are playing
void SetFront(X3DAUDIO_VECTOR front);
void SetReelingIn(bool isReelingIn);
void SetCastOut(bool hasCastOut);
float GetReelClickTime();
bool IsReelingIn();
float GetReelSpeed();
X3DAUDIO_LISTENER GetListener();
X3DAUDIO_EMITTER GetEmitter();
X3DAUDIO_DSP_SETTINGS GetSettings();
XASound* GetCastSound();
XASound* GetCastSplashSound();
bool HasCastSplashed();
void SetCastSplashed(bool splashed);
bool IsCastOut();
private:
XACore *mXACore;
XASound *mCast;
XASound *mCastSplash;
XASound *mReelClick[REEL_CLICK_OBJECTS];
float mCastDistance;
float mReelClickTimer;
float mReelSpeed;
std::list<float> mReelSpeedList;
std::list<XASound*> mReelClickList;
X3DAUDIO_LISTENER mListener;
X3DAUDIO_EMITTER mEmitter;
X3DAUDIO_DSP_SETTINGS mDSPSettings;
Fish *mFish;
bool mCastOut;
bool mCastSplashBool;
bool mReelingIn;
};
#endif
including windows.h at the top of the header file solved this issue.
Does the error also say PCONTEXT undefined?
Try adding #include <Windows.h> before #include <X3DAudio.h>.
In reply to comment about unresolved external symbol:
That's because you didn't define Fisherman::SetFront in the cpp file.
Whole code would be helpfull. Did you remember obvious to give namespace to names of functions etc?
class A { void foo() { } };
=>
class A { void foo(); }
void A::foo() { ... }
etc?
Fastest way would be reverting back to state when it worked, and not moving whole at once, but only one function at a time. Then when problem would occure you could give us that specific code or try to fix it yourself.
It is very possible that there is just a missed ';', somewhere before this line.
Double-check your code, remember that everything you define in a cpp file has to already be declared in your class definition.
I'd prefer to post a comment, but apparently I'm not allowed.
Anyway, as a simpler way to give us a feel of what you're trying to do, definatley post your code - it does not have to be exactly as you use, but should be a concise piece of generic code, highlighting how you are doing something. (SSCCE) If it really is a bug in how you typed it, it would be good to have your exact code, but put it on pastebin and provide a link so that this page doesn't end up with reams of code.
EDIT:
winnt.h apparently causes all sorts of problems: Weird compile error dealing with Winnt.h
it is used to provide definitions for winAPI specific headers, as far as I know. One of the solutions proposed on that thread is to get the definitions from a different windows header before including whichever header requires those definitions.

non static or const array syntax

what's wrong with this syntax? sorry for the newbie question.
source:
Level::Level()
{
NintyDegreeDirections[4] =
{
1.0f, 1.4f, 2.4f, 0.1f
}
...rest of class
header:
//all necessary includes
class Level
{
private:
float NintyDegreeDirections[4];
...rest of header
how do I have an array as a instance member? I'm converting from C#
In the current version of C++ (C++11), you can initialize the member array like this:
Level::Level()
: NintyDegreeDirections( { 1.0f, 1.4f, 2.4f, 0.1f } )
{
}
C++11 isn't universally supported and if you don't have support for this in your compiler you will have to assign to each member in turn.
E.g.:
NintyDegreeDirections[0] = 1.0f;
NintyDegreeDirections[1] = 1.4f;
//...
Did you try:
NintyDegreeDirections[0] = 1.0f;
NintyDegreeDirections[1] = 1.4f;
/* ... */