Visual studio shows a error saying "the object has type quantifiers that are not compatible with the member function 'somfunc' "
class T_ship {
public:
...
float ship_run(int ship_len);
// function ship_run doesn't change any class member values
};
There is a function in main() with *T_ship pointer passed as input.
void draw_ship(const T_ship * a_shp){
float dist = a_ship-> ship_run(100);
// there is a red wavy line under a_ship
}
Many thanks if someone can help.
If you want the actual pointer to be const and not the object pointed to, put the const in front of the type, so like this:
T_ship * const
However in your case if the function ship_run doesn't modify anything you should
mark it as const at the end of the function as well so like this:
float ship_run(int v) const { /* your code here */ }
Related
I am trying to run a simple C++ program. Whenever I run the code I get the same output 'error: passing 'cont Point' as 'this' argument discards qualifiers [-fpermissive]'. The problem seems to be occurring whenever I call the set function. I am still new to C++ and am still trying to get familiar with its functions so I apologize if the answer may seem obvious. Below is the code that is giving me problems:
#include <QTextStream>
#include <QString>
class Point {
public:
Point(int px, int py)
: m_X(px), m_Y(py) {}
void set(int nx, int ny) {
m_X = nx;
m_Y = ny;
}
QString toString() const {
// m_X = 5;
m_Count++;
return QString("[%1,%2]").arg(m_X).arg(m_Y);
}
private:
int m_X, m_Y;
mutable int m_Count;
};
int main() {
QTextStream cout(stdout);
const Point p(1,1);
const Point q(2,2);
p.set(4,4);
cout << p.toString() << endl;
q.set(4,4);
return 0;
}
In C++, you cannot modify an object marked as const, which is a keyword used in C++, a synonym of "non-modifiable", used to tell the compiler that a variable cannot be modified. In your main,
const Point p(1,1);
const Point q(2,2);
p.set(4,4);
q.set(4,4);
the variables p and q are of type const Point but the member function Point::set is not marked const. Since the member function Point::set is not a const member function of class Point, the compiler thinks the function tries to modify the object it is called on (in the statement p.set(4,4); the function set tries to modify p), but this is not allowed since you marked p (and q) to be const, recall, "non-modifiable".
Now, notice that you could mark member function Point::set as const, but that would be nonsensical since you want to modify the variables (in this case p and q). A solution would be to drop the const qualifier from p and q.
You can watch this video to learn about const member functions; you can also watch this video to learn more about member functions in C++.
I am trying to pass parameters to a function pointer being passed as a parameter.
Code:
void Test(wchar_t* a, wchar_t* b)
{
// ...
}
void Test2(void(*Func)(wchar_t*, wchar_t*))
{
// ...
}
int main()
{
Test2(Test(L"Hello", L"Testing"));
return 0;
}
I am getting this error:
argument of type "void" is incompatible with parameter of type "void (*)(wchar_t *, wchar_t *)"
How do I fix this to accomplish what I'm trying to achieve?
Edit: Sorry for not being clear. What I'm actually trying to accomplish is inject a function into a child process and pass two parameters (wchar_t*, wchar_t*) so I can use them. But the main function can either be void or int argc, char** argv. So I accomplished what I'm trying to achieve by simply using global variables
You probably want to have something like
void Test2(void(*Func)(wchar_t*, wchar_t*),wchar_t* x, wchar_t* y)
{
(*Func)(x,y);
}
int main()
{
Test2(Test,L"Hello", L"Testing");
return 0;
}
instead.
As for your comment
How do i do this in C++ with templates?
I could think of
template<typename Param>
void Test2(void(*Func)(Param, Param), Param x, Param y) {
(*Func)(x,y);
}
void Test(wchar_t* a, wchar_t* b);
int main() {
Test2(Test,L"Hello", L"Testing");
return 0;
}
This should just work fine.
There are more than one way to fix tihs issue, however, let me just try to show why this error is occuring.
Every function has a type of value associated with it. This means, that every function evaluates to a value of some type. This is indicated by its return value.
For example:
int foo(/*whatever*/);
evaluates to an int. So foo(/*whatever*/) can be used anywhere an int is expected. For example like int a = b + foo(/*whatever*/).
Simlarly float bar(/*whatever*/); evaluates to a float, hence bar(/*whatever*/) can be used anywhere a float is expected. For example like float a = b + bar(/*whatever*/).
A function that returns void like void foobar(/*whatever*/) however, evaluates to void and cannot be used where a value of some type (say int, float, etc) is expected.
Now coming to code. This line in your main function has the issue:
int main()
{
Test2(Test(L"Hello", L"Testing")); /* Issue here */
return 0;
}
Here you are passing Test(L"Hello", L"Testing") as the argument to Test2. Now remember, that Test(/*whatever*/), evaluates to a void because Test returns a void.
So what you are doing in that line is something like
Test2(/*something that evaluates to a void*/);
However, Test2 expectes a void (*)(wchar_t*, wchar_t*), which is a pointer to a function that returns void, which is different from void.
So what is happening, is that the compiler is seeing that you are passing a void in a place where a void (*) (wchar_t*, wchar_t*) is expected, so it is correctly indicating that error.
There can be different ways to solve this issue which are mentioned in other answers.
Do I need to use C++ templates?
Of course, you can do that using C++ templates as it follows:
#include<utility>
// ...
template<typename F, typename... A>
void Test2(F &&f, A&&... a)
{
std::forward<F>(f)(std::forward<A>(a)...);
// ...
}
// ...
Test2(Test, L"Hello", L"Testing");
But you don't need them to do what you are trying to do.
#πάνταῥεῖ has already explained why in its answer.
I have grouped several member functions into an array. How do I access a function from the array? I am getting 'error C2064: term does not evaluate to a function taking 0 arguments.' See below.
class A
{
public:
//Constructor
A()
{
//Fill function array
ClipFunction[0] = &A::ClipTop;
ClipFunction[1] = &A::ClipBottom;
ClipFunction[2] = &A::ClipLeft;
ClipFunction[3] = &A::ClipRight;
}
//Declare array
typedef void (A::*ClipFunction_ptr) ();
ClipFunction_ptr ClipFunction[4];
//Clipping functions
void ClipTop();
void ClipBottom();
void ClipLeft();
void ClipRight();
//Start clipping process
void StartClip();
};
//Define clipping functions
void A::ClipTop() {}
void A::ClipBottom() {}
void A::ClipLeft() {}
void A::ClipRight() {}
//Define A::StartClip()
void A::StartClip()
{
//Run through all functions in the array
for (unsigned int i = 0; i < 4; i++)
{
ClipFunction[i](); //ERROR. How do I access ClipFunction[i] ???
}
}
You need to dereference the function like this:
this->(*ClipFunction[i])();
What you're missing is the this or rather the compiler is complaining that it doesn't have the first parameter (the instance of the object invoking the member function) to pass it to the function.
To the compiler the member function:
void A::ClipFunction()
{
}
translates to something like:
void ClipFunction(A* this)
{
}
Hence the error complaining that the function is not one that takes zero arguments.
I think the problem is that you need use "this" explicitly as in http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/topic/com.ibm.xlcpp8l.doc/language/ref/cplr034.htm .
So in your case, you should use
(this ->* ClipFunction[i]) ();
instead of
ClipFunction[i]();
PS
When I reply this post, I didn't see Vite Falcon's answer. Basically we are saying the same thing but I don't think his code " this->(*ClipFunction[i])()" will compile because GCC gives errors on my machine. "(this->*ClipFunction[i])()" is the correct form.
I don't think you want the scope resolution operator :: in your typedef. Instead try putting
typedef void (*ClipFunction_ptr) ();
SO im pretty new to c++ and im trying to pass a 2D array of a struct type by reference to a function. As far as i know they are automatically passed by reference. Here is my code.The problem is probably obvious but i cant figure it out. The complier keeps saying variable or field "function" declared void and bArray was not declared in this scope.
void function(balloons bArray[][5]);
int main()
{
struct balloons
{
float totalWeight;
float largestBalloon;
};
balloons balloonsArray[20][5];
function(balloonsArray);
}
void function(balloons bArray[][5])
{
bArray[1][1].totalWeight = 1.0
bArray[1][1].largestBalloon = 1.0
}
You're defining your struct within main, other parts of your code need to use it also. Move the definition outside the function:
struct balloons
{
float totalWeight;
float largestBalloon;
};
void function(balloons bArray[][5]);
int main()
{
// ...
And you haven't terminated the two statements in your function, you'll need semicolons there:
bArray[1][1].totalWeight = 1.0;
bArray[1][1].largestBalloon = 1.0;
Guys, I am very new to c++. I have just wrote this class:
class planet
{
public:
float angularSpeed;
float angle;
};
Here is a function trying to modify the angle of the object:
void foo(planet* p)
{
p->angle = p->angle + p->angularSpeed;
}
planet* bar = new planet();
bar->angularSpeed = 1;
bar->angle = 2;
foo(bar);
It seem that the angle in bar didn't change at all.
Note that you are passing bar by pointer, not by reference. Pass-by-reference would look like this:
void foo(planet& p) // note: & instead of *
{
p.angle += p.angularSpeed; // note: . instead of ->
}
Pass-by-reference has the added benefit that p cannot be a null reference. Therefore, your function can no longer cause any null dereferencing error.
Second, and that's a detail, if your planet contains only public member variables, you could just as well declare it struct (where the default accessibility is public).
PS: As far as I can see it, your code should work; at least the parts you showed us.
Appearances must be deceptive because the code should result in foo(bar) changing the contents of the angle field.
btw this is not passing by reference, this is passing by pointer. Could you change the title?
Passing by reference (better) would be
void foo(planet& p) {
p.angle += p.angularSpeed;
}
planet bar;
bar.angularSpeed=1;
bar.angle=2;
foo(bar);
You might also consider a constructor for planet that takes as parameters the initial values of angle and angularSpeed, and define a default constructor that sets them both to 0. Otherwise you have a bug farm in the making due to unset values in planet instances.