Error: invalid conversion from 'int' to 'const char*' [-fpermissive] - c++

I am experiencing an error and I can't figure out why.
I have a subclass: (Header)
class motionSensor: public sensorLeaf
{
public:
motionSensor(const int& sensorID, const int& sensorType, bool sensorActivate);
Which inherits from superclass "sensorLeaf"
The subclass has 2 private variables
const float minDistance;
const float maxDistance;
In my class file; here is my constructor:
motionSensor::motionSensor(const int &sensorID, const int &sensorType, bool sensorActivate)
:sensorLeaf(sensorID, sensorType, sensorActivate), minDistance{1.0f}, maxDistance{5.0f}
But once I compile it, I get following error:
/home/jb/EmergencySensor/motionsensor.cpp:8: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
:sensorLeaf(sensorID, sensorType, sensorActivate), minDistance{1.0f}, maxDistance{5.0f}
^
Sensorleaf Constructor is :
sensorLeaf::sensorLeaf(const int sensorID, const std::string sensorType, bool sensorActivate)
{
_sensorID = sensorID;
_sensorType = sensorType;
_sensorActivate = sensorActivate
}
With as private variables:
private:
int _sensorID;
std::string _sensorType;
bool _sensorActivate;
};
After doing some research I found that the error could occur when you define a string as ' ' and not " " but I have not used this anywhere so.

You're trying to force a const int &sensorType from motionSensor constructor to const std::string sensorType of sensorLeaf constructor.
motionSensor::motionSensor(const int &sensorID, const int &sensorType, bool sensorActivate)
------------------------------------------------------------^
:sensorLeaf(sensorID, sensorType, sensorActivate), minDistance{1.0f}, maxDistance{5.0f}
-----------------------^
sensorLeaf::sensorLeaf(const int sensorID, const std::string sensorType, bool sensorActivate)
-------------------------------------------------------------^

Related

Trying to sort vector of structs gets error: passing 'const user_info_t' as 'this' argument discards qualifiers [-fpermissive]

I'm newbie in C++. I have a method in which I'm trying to sort vector of struct user_info_t.
std::vector<user_info_t> phone_book_t::search_users_by_number(const std::string &number_prefix, size_t count) const {
user_info_t one = {{"+78", "a"}, 1.0};
user_info_t two = {{"+77", "a"}, 1.0};
user_info_t three = {{"+79", "a"}, 1.0};
/*contacts_info.push_back(two);
contacts_info.push_back(three);*/
sort(this->contacts_info.begin(), this->contacts_info.end(), numberComparator);
return std::vector<user_info_t>(contacts_info.begin(), contacts_info.begin() + 10);
}
And phone_book_t class code is
class phone_book_t {
public:
phone_book_t() = default;
phone_book_t(const phone_book_t &other) = default;
phone_book_t &operator=(const phone_book_t &other) = default;
~phone_book_t() = default;
std::vector<user_info_t> search_users_by_number(const std::string &number_prefix, size_t count) const;
std::vector<user_info_t> search_users_by_name(const std::string &name_prefix, size_t count) const;
private:
std::vector<user_t> contacts;
std::vector<call_t> calls;
std::vector<user_info_t> contacts_info;
};
The thing is I can't sort vector because it throws an error "error: passing 'const user_info_t' as 'this' argument discards qualifiers [-fpermissive]"
I googled this error and I don't get it at all. I understand there's something in const keyword and for example these lines
/*contacts_info.push_back(two);
contacts_info.push_back(three);*/
also don't compile. CLion tells me in that case that "No matching member function for call to 'push_back' candidate function not viable: 'this' argument has type 'const std::vector<user_info_t>', but method is not marked const"
I can't modify any public fields in phone_book_t class or anything in structs, so I would appreciate if you could help me.

Why initialization of const char array member incompatible in constructor initializer?

I have a Test class with overloaded constructor. Initializing const char array member by string literals work fine. But, initialization by const char * gives error -
error: incompatible types in assignment of ‘const char*’ to ‘const
char [25]’
class Test
{
const char d_arr[25];
public:
Test() : d_arr("Test Class") {}
Test(const char * arr) : d_arr(arr) {}
};
How to resolve this?
You are assigning a pointer to an array, which is not allowed.
i.e. You cannot do following:
const char *arr = "ABC";
const char d_arr[25] = arr;
What you need to do is copy the chars manually i.e. something like:
Test(const char * arr) {
size_t index = 0;
if (arr) {
while (arr[index] && index < 24) {
d_arr[index] = arr[index];
++index;
}
}
d_arr[index] = 0;
}
That all said, as said in comments, its better to use std::string.

Cannot convert int {Class}::* to int*

When I try to call a function, passing in a reference to my variable which has the type int, to the function which takes a type of int, I get an error that seems to point out that the int is the type of the class it was declared in, why is this?
Header:
class MyClass {
public:
int MAJOR = 3;
int MINOR = 3;
int REV = 0;
}
Code:
glfwGetVersion(&MyClass::OPENGL_VERSION_MAJOR, &MyClass::OPENGL_VERSION_MINOR, &MyClass::OPENGL_VERSION_REV);
Error:
error: cannot convert 'int MyClass::*' to 'int*' for argument '1' to 'void glfwGetVersion(int*, int*, int*)'
Change to:
class MyClass {
public:
static const int MAJOR = 3;
static const int MINOR = 3;
static const int REV = 0;
};
If these versions are constant.
Otherwise as:
class MyClass {
public:
static int MAJOR;
static int MINOR;
static int REV;
};
Then somewhere in a .cpp file
int MyClass::MAJOR = 3;
int MyClass::MINOR = 3;
int MyClass::REV = 0;
Check live example here
&MyClass::OPENGL_VERSION_MAJOR is a pointer on member.
You may use
MyClass instance;
glfwGetVersion(&instance.OPENGL_VERSION_MAJOR,
&instance.OPENGL_VERSION_MINOR,
&instance.OPENGL_VERSION_REV);

Can't do a strcpy from 2D array to another 2D array

Both are in the operator= in the same class
here is the definition of the function.
void segment::operator=(const segment& w) {
strcpy(this->phrase, w.getPhrase()); //this line creates a problem.
error is below:
segment.cpp: In member function ‘void segment::operator=(const segment&)’:
segment.cpp:186: error: passing ‘const segment’ as ‘this’ argument of ‘const char*
segment::getPhrase()’ discards qualifiers
segment.cpp:186: error: cannot convert ‘char (*)[40]’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
const char* segment::getPhrase(){
return *phrase;
}
And above is the function getPhrase
I don't know why I can't do a strcpy for that.
I'm trying to completet the assignment.
EDIT:
This is the type of phrase
char phrase[10][40];
There are two problems. First you have to make getPhrase a const method. The second problem is that strcpy doesn't work with an extra level of indirection. You probably need something like this:
const char* segment::getPhrase(int index) const {
return phrase[index];
}
void segment::operator=(const segment& w) {
int index;
for (index = 0; index < 10; ++index) {
strcpy(this->phrase[index], w.getPhrase(index));
}
}
You should replace the 10 with constant
class segment {
//other stuff
static const int kNumPhrases = 10;
char phrase[kNumPhrases][40];
}

How to use a typedef function pointer to register a callback

I'm trying to implement an observer pattern (of sorts) with C++ and I want to use function pointer to do so, but I keep getting an error when trying to cast a function pointer from class B to a typedef function pointer:
#include <map>
typedef int (*OutputEvent)(const char*, const char*, int);
class A
{
private:
int nextListenerId;
std::map<int, OutputEvent> listenerMap;
public:
A(){ nextListenerId = 0;}
~A(){}
inline int RegisterListener(OutputEvent callback)
{
nextListenerId++;
listenerMap[nextListenerId] = callback;
return nextListenerId;
}
};
class B
{
private:
int listenerId;
public:
B(const A& a)
{
OutputEvent e = &B::CallMeBack;
listenerId = a.RegisterListener(e);
}
~B(){}
int CallMeBack(const char* x, const char* y, int z)
{
return 0;
}
};
I created this example and I've pasted it into codepad.org, but when I it fails to compile (it doesn't compile in codepad.org nor in Visual Studio 2010):
Output:
t.cpp: In constructor 'B::B(const A&)':
Line 28: error: cannot convert 'int (B::*)(const char*, const char*, int)' to 'int (*)(const char*, const char*, int)' in initialization
compilation terminated due to -Wfatal-errors.
I don't understand why it can't convert the function pointers. Could anybody help me please?
The function you are trying to cast to OutputEvent is a member function. This is represented clearly in the error message by this:
'int (B::*)(const char*, const char*, int)'
which is a different type than
int (*OutputEvent)(const char*, const char*, int)
because of the B:: part (that means that the function has an invisible this parameter).
If you define your member function as static, then you will be able to cast it to OutputEvent:
class B
{
....
static int CallMeBack(const char* x, const char* y, int z);
...
};
The member function doesn't match the typedef'd prototype because member functions have an invisible 'this' parameter. Make it a static function and it should work.
The class member function has got a hidden parameter, the object, which the global function does not have.
Do something like this:-
B*pB;
int CallMeBack(const char* x, const char* y, int z)
{
return pB->CallMeBack(x,y,z);
}
More difficult if you have several callbacks on the go at the same time. But if there is only one you can have a pointer to the object and call via this.