Const Discards Qualifers: C++ - c++

I'm using OpenGL to render camera perspectives, and a one point in my code I'm trying to take the direction of the light (shown here as type "Vector4") and multiply it by a matrix of type "Matrix4x4" that represents the Modelview transformation (sorry if this is not making any sense, this is for a school project, as such I'm still learning about this stuff) Anyway, my code goes as follows...
Vector4 lightDirection = data->dir * follow->getModelviewMatrix().getInverse().getTranspose();
data->dir = lightDirection;
setLight(*data);
this give me the following error:
passing 'const vec4<double>' as 'this' argument of 'vec4<T>& vec4<T>::operator=(const vec4<T>&)[with T = double]' discards qualifiers
Again, much of this code is prewritten for the class (namely the vector and matrix types) but if someone could just help me decipher what the error means it would be much appreciated! I can give more information as needed.
I figured 'data' or 'data->dir' were const, however I can find no mention of either of them to be. 'dir' is of type SceneLightData, and when its added on I'm doing this:
void Scene::addLight(const SceneLightData &sceneLight)
{
SceneLightData light = sceneLight;
m_lights.push_back(&light);
}
The error occurs on this line:
data->dir = lightDirection;
EDIT
problem solved. thanks everyone! solution:
void Scene::addLight(const SceneLightData &sceneLight)
{
SceneLightData* light = new SceneLightData;
*light = sceneLight;
m_lights.push_back(light);
}
and
SceneLightData* data = m_lights[i];
data->dir = data->dir * follow->getModelviewMatrix().getInverse().getTranspose();
setLight(*data);

data->dir is constant. It means you cannot change it, and you are trying to assign a different value to it, that's why compiler is getting mad at you. See const-correctness for more.

This error occurs because you are attempting to call a non-const member function on a const object. At some point in your code you are calling the assignment operator of a const vec4<double>.
The assignment operator (operator=) is non-const because assigning to an object will modify the object that is being assigned to (to give it its new value).
It is not clear from your example where exactly the problem is occurring. If you are still having trouble, try reducing the example down to a minimal complete program which demonstrates the issue, and we can explain further.

Related

In my function call I keep getting an error for "primary-expression before 'char'"

I do not understand what is missing in front of char. The line is:
float temp = tinputs(char scale);
I mean I've tried a ton and didn't get passed the error statement. it should be a simple fix though.
From the limited information you've provided I assume you're trying to call function tinputs with argument scale type char.
However you're using a declaration syntax in a call function.
Your code should be like this:
float temp = tinputs(scale);

cast “int32_t” to “static const int32_t”

(Major edit: The way I posed the original question was bit confusing. So, I am trying to improve the question)
I am trying to convert “int32_t” to “static const int32_t” type. However, I could not figure out how to use static_cast and const_cast together. Any help would be appreciated.
I want to do this so that rather than initializing my “static const int32_t IRF_MAX_ENVELOPE_ELEMENTS2” to a hardcore value, I would like to set this based on value passed to the relevant function.
Say, the value of iNoOfSamples_In is 128, I would like to set IRF_MAX_ENVELOPE_ELEMENTS2 to 128 too; but, as as a “static const int32_t” like this:
int32_t iNoOfSamples_In = 128;
static const int32_t IRF_MAX_ENVELOPE_ELEMENTS2 = iNoOfSamples_In;
However, when I go to declare an array of size IRF_MAX_ENVELOPE_ELEMENTS2
double dTime_Scale[IRF_MAX_ENVELOPE_ELEMENTS2]; // Line 80
I get the following errors (line 80 marked in code snippet):
SpecialPulses.cpp(80) : error C2057: expected constant expression
SpecialPulses.cpp(80) : error C2466: cannot allocate an array of constant size 0
SpecialPulses.cpp(80) : error C2133: 'dTime_Scale' : unknown size
So, it seems that max_envelope_elements is not constant.
Just as the error message says, you can't declare a variable as const and then change its value. However, if you are trying to call a function that takes a const int_32, that's fine - just declare the variable as int_32. In that case, the const just says that the function doesn't change the value of the parameter inside the function, but even if it did it wouldn't affect your variable anyway.
By const it means that it's constant and by definition a constant is:
a situation or state of affairs that does not change. - google"
so you cannot change the value of the const variable after you initiated it.
Please do note that const are used most of the time for code readability in replacement for magic numbers in code.
take this for example
if(a>b%2)
//do something
What the heck is 2 and what does that do?
while if you actually can use something like this.
if(a>b%SOME_CONST_VALUE)
//do something
you can actually tell by the const variable what you are actually doing rather than having the programmer/developer who will maintain your code what the heck does that if statement does.
const_cast<int32_t>(IRF_MAX_ENVELOPE_ELEMENTS2)=256;
You can change your constant values like that, I hope this will help your problem.

D language function call with argument

I am learning D and have mostly experience in C#. Specifically I am trying to use the Derelict3 Binding to SDL2. I have been able to get some basic functionality working just fine but I have become stumped on how to create an array argument for a specific call.
The library contains a call
SDL_RenderDrawLines(SDL_Renderer*, const(SDL_Point)*, int) //Derelict3 Binding
And I have been unable to correctly form the argument for
const(SDL_Point)*
The SDL Documentation for this function states that this argument is an array of SDL_Point, but I am unclear how to create an appropriate array to pass to this function.
Here is an example of what I have at the moment:
void DrawShape(SDL_Renderer* renderer)
{
SDL_Point a = { x:10, y:10};
SDL_Point b = { x:500, y:500};
const(SDL_Point[2]) points = [a,b];
Uint8 q = 255;
SDL_SetRenderDrawColor(renderer,q,q,q,q);
SDL_RenderDrawLines(renderer,points,1);
}
And the compiler complains that I am not passing the correct type of argument for const(SDL_Point)* in points.
Error: function pointer SDL_RenderDrawLines (SDL_Renderer*, const(SDL_Point)*, int)
is not callable using argument types (SDL_Renderer*, const(SDL_Point[2u]), int)
I suspect this is a fundamental misunderstanding on my part so any help would be appreciated.
Arrays aren't implicitly castable to pointers in D. Instead, each array (both static and dynamic) has an intrinsic .ptr property that is a pointer to its first element.
Change your code to:
SDL_RenderDrawLines(renderer,points.ptr,1);
given that the call asks for a pointer and length, I feel it is safer to define you own wrapper:
SDL_RenderDrawLines(SDL_Renderer* rend, const SDL_Point[] points){
SDL_RenderDrawLines(rend,points.ptr,points.length);
}
(why it isn't defined I don't know, any performance hit from the extra function call is just a -inline away from being resolved)

C++ problem with std::pair and forward declarations

Unfortunately I still got a problem with my templated code from here:
C++ fancy template code problem
on line 49 in the file 'utility':
error C2440: 'Initializing': cannot convert from 'const int' to 'IntersectionData *'
error C2439: 'std::pair<_Ty1,_Ty2>::second': member could not be initialized
how could i figure out where the problem is? the only place i use a pair with 'IntersectionData*' is here:
#include "MRMaterialMatth.h"
#include "IntersectionData.h"
using namespace std;
struct IShaderMatth {
virtual ~IShaderMatth() {}
vector<pair<MaterialMatth,IntersectionData*> > traceCols;
};
and there are not any other compiler errors
how can I track down this?
//edit: utility is not my code. it must be from std.. the code of line 49 looks like this:
template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}
line 49 is the line of the comment
edit2:
the only places where i change something about the content of tracecols look like this:
IntersectionData* iDataOut = NULL;
if(newIData_out!=NULL)
{
iDataOut = new IntersectionData(*iData);
}
traceCols->push_back(make_pair(MaterialMatth(),iDataOut));
and
if(traceCols){
traceCols->push_back(make_pair(MaterialMatth(), NULL));
}
and
if(traceCols)
{
(*traceCols)[traceCols->size()].second = new IntersectionData(*newIData);
}
is NULL the problem? it's a pointer, so i should be allowed to create a pair with NULL, no??
Try explicitly casting the NULL to IntersectionData * in your call to make_pair().
if(traceCols){
traceCols->push_back(make_pair(MaterialMatth(), (IntersectionData *)NULL));
}
There is a problem initializing one of those pairs.
Ask yourself, "What initializes that?"
The answer is the vector traceCols.
Now ask, "Where am I creating elements in traceCols?"
Once you answer that, you should know what is going wrong.
Watch out for the line (*traceCols)[traceCols->size()].second = new IntersectionData(*newIData) - it seems like that would go out of the vector's bounds (since the largest index of a vector is size() - 1).
I'm not sure if the NULL is causing it - so comment out that line, and see for yourself (or try Dave's suggestion)! If it doesn't work, comment out another. Eventually, you'll either find what line, and be able to ask a more specific question, or it'll be none of those things, and you'll know you have to search somewhere else. That's what I do when I see all these silly compiler error messages.
It looks like you have an assignment somewhere from a pair<MaterialMatth,int>. The compiler is trying to convert from that to the declaration you listed, but it can't convert from an int to a pointer without an explicit cast.

C++ string.compare()

I'm building a comparator for an assignment, and I'm pulling my hair out because this seems to simple, but I can't figure it out.
This function is giving me trouble:
int compare(Word *a, Word *b)
{
string *aTerm = a->getString();
string *bTerm = b->getString();
return aTerm->compare(bTerm);
}
Word::getString returns a string*
Error:
In member function `virtual int CompWordByAlpha::compare(Word*, Word*)':
no matching function for call to...
...followed by a bunch of function definitions.
Any help?
You're comparing a string to a string pointer, and that's not valid. You want
return aTerm->compare(*bTerm);
You aren't getting the different uses of the * operator. The use of the * in "string* bTerm = b->getString()" means "bTerm is a pointer to a string". The use of the * inside of compare(*bTerm) means "take the value of the location pointed to by bTerm" instead of just using compare(bTerm) which simply attempts to compare the value of bTerm itself, which is a hex address.
This is also happening on the left side of that call:
aTerm->compare(*bTerm); //this statement
(*aTerm).compare(*bTerm); //is the same as this statement
The -> operator just reduces the amount of typing required.
P.S.: This kind of stuff you could have easily figured out from Google or your programming textbook. Although others may disagree, I don't feel that questions about completely basic syntax have any place on Stack Overflow.