Returning a Function Pointer in C++ - c++

Here is the code:
void (*)(const dpp::slashcommand_t &event) operator[](const std::string &index);
When I compile this, I get the following error message:
In file included from commands_controller.cpp:3,
from main.cpp:2:
commands_controller.h:28:12: error: expected unqualified-id before ‘)’ token
28 | void (*)(const dpp::slashcommand_t &event) operator[](const std::string &index);
Do you have any suggestion for how I can resolve this error?

When dealing with function pointers, use typdefs. The correct non-typedef syntax is:
void (*operator[](const std::string &index))(const dpp::slashcommand_t &event)
https://coliru.stacked-crooked.com/a/4eabda845ac15a54
Obviously, this is completely insane.
So instead, always use a typedef or alias:
using slash_fn_ptr = void (*)(const dpp::slashcommand_t &event);
slash_fn_ptr operator[](const std::string &index);
https://coliru.stacked-crooked.com/a/fd96b9918b1526b7

Related

Error while adding shared pointer to the vector

abstract base class:
#ifndef BUILDINGORG_H
#define BUILDINGORG_H
#include <iostream>
#include <memory>
#include <vector>
class BuildingOrg
{
public:
BuildingOrg(int _id);
virtual int addBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg,
std::string _type) const;
virtual void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg);
virtual void getInfo()=0;
private:
int id;
std::string type;
};
#endif // BUILDINGORG_H
concrete subclass:
#ifndef BUILDINGCOMPONENT_H
#define BUILDINGCOMPONENT_H
#include "buildingorg.h"
class BuildingComponent : public BuildingOrg
{
public:
BuildingComponent(int _id);
int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg,
std::string _type) const override;
void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
override;
void getInfo() override;
private:
std::vector<std::shared_ptr<BuildingOrg>> building_Org;
};
#endif // BUILDINGCOMPONENT_H
Implementation of subclass:
#include "buildingcomponent.h"
BuildingComponent::BuildingComponent(int _id):
BuildingOrg(_id)
{
}
int BuildingComponent::addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const
{
building_Org.push_back(_buildingOrg);// I am having error here
return 1;
}
void BuildingComponent::removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
{
}
void BuildingComponent::getInfo()
{
}
When I try to put shared pointer in my Vector I get this nasty error;
I really don't know why I am getting the error:
cpp:10: error: passing 'const std::vector<std::shared_ptr<BuildingOrg> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::shared_ptr<BuildingOrg>; _Alloc = std::allocator<std::shared_ptr<BuildingOrg> >; std::vector<_Tp, _Alloc>::value_type = std::shared_ptr<BuildingOrg>]' discards qualifiers [-fpermissive]
building_Org.push_back(_buildingOrg);
I don’t understand what is it saying.
The const in int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const override; is a promise that addBuildingComponent will not change BuildingComponent. However, it tries to modify the member variable building_Org with the push_back()...
Removing the const from addBuildingComponent() should fix the error.
The discards qualifiers part of the error message refers to the conflict with the const qualifier of the member function.
C++ template related error messages can be notoriously difficult to parse at first, but it does get easier with practice :-)
You defined BuildingComponent::addBuildingComponent method as const (i.e. that it won't change member varialbles), but you are adding passed in value to a member list (i.e. changing the member variable).
addBuildingComponent() is a const method. within its scope, *this is const, and so this->building_Org is const.
std::vector::push_back() is a non-const method. So it can't be called in a context where the vector is const.

Error with an object std::string

here I have an error but I don't know why it shows. This is the error:
In file included from Exploit.cc:2:0: Command.hh:35:17: error: field
‘_value’ has incomplete type Command.hh: In constructor
‘Command::Command(const char*)’: Command.hh:27:3: error: ‘_value’ was
not declared in this scope make: *** [Exploit.o] Error 1
And this is Command.hh
class Command {
public:
Command(const char* exp){
_value=exp;
_value.append("\n");
}
~Command();
void request(int fd);
void response(std::string res);
const char* getCommand();
private:
std::string _value;
};
Exploit.cc
typedef std::shared_ptr<Command> CommandPtr;
typedef std::list<CommandPtr> CommandQueue;
typedef std::shared_ptr<CommandQueue> CommandQueuePtr;
Exploit::Exploit(const char* exp, int fd2, int type2): fd(fd2), type(type2){
commands_to_execute = make_shared<CommandQueue>();
commands_executed = make_shared<CommandQueue>();
CommandPtr pr=std::make_shared<Command>( exp);
commands_to_execute->push_back(pr);
}
I hope someone could help me, because It's very weird for me.
Thank you!!
Your forgot to include the string header:
#include <string>
in Command.hh.
On a related note, maybe it's a good idea to make the constructor accept an std::string:
Command(const std::string& exp) {
instead of a const char*.

Error instantiating array of type basic_regex

I have tried all day without any luck...
This works:
std::regex pattern ("Test");
This doesn't work:
std::regex pattern_array[2] {"Test1", "Test2"};
Generating the errors:
mainprog.cpp:534:47: error: could not convert ‘(const char*)"Test1"’ from ‘const char*’ to ‘std::regex {aka std::basic_regex<char>}’
mainprog.cpp:534:47: error: could not convert ‘(const char*)"Test2"’ from ‘const char*’ to ‘std::regex {aka std::basic_regex<char>}’
I have tried to make a class with the same structure as std::regex, but i am unable to recreate the error (it works perfectly).
I am compiling using gcc 4.7.2 running on Linux.
Documentation for std::regex
Thanks, i highly appreciate any help.
Calle
Update:
This is my reconstruction that works:
class testclass
{
public:
testclass(const char* s, bool b = true);
};
testclass::testclass(const char* s, bool b)
{
printf("Bool %d", b);
}
testclass obj1 ("Test");
testclass obj2[2] {"Test1", "Test2"};
Try this.
std::regex pattern_array[2] = { std::regex("Test1"), std::regex("Test2") };
You need to explicitly construct with the constructor because the regex class uses the explicit keyword on its constructor.

Error: default argument given for parameter after previous specification

very simple task for me here and I'm not sure why this is giving me problems, I'm simply making two mockup classes try to compile without any logic in their methods whatsoever using headers and declarations already given to me. Honestly this is just a cut and paste job more than anything, and yet I still came across this golden nugget of love -
cbutton.cpp:11:44: error: default argument given for parameter 4 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:11:44: error: default argument given for parameter 5 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:19:41: error: default argument given for parameter 1 of ‘void cio::CButton::draw(int)’ [-fpermissive]
cbutton.h:11:10: error: after previous specification in ‘virtual void cio::CButton::draw(int)’ [-fpermissive]
cbutton.cpp:53:29: error: ‘virtual’ outside class declaration
Here are the files I'm working with. Thank you everyone, as always!
#include "cfield.h"
namespace cio{
class CButton: public CField{
public:
CButton(const char *Str, int Row, int Col,
bool Bordered = true,
const char* Border=C_BORDER_CHARS);
virtual ~CButton();
void draw(int rn=C_FULL_FRAME);
int edit();
bool editable()const;
void set(const void* str);
};
}
#include "cbutton.h"
namespace cio {
CButton::CButton(const char *Str, int Row, int Col,
bool Bordered = true,
const char* Border=C_BORDER_CHARS){
}
void CButton::draw(int rn=C_FULL_FRAME){
}
int CButton::edit(){
return 0;
}
bool CButton::editable()const {
return false;
}
void CButton::set(const void* str){
}
virtual CButton::~CButton(){
}
}
You specified a default argument in the definition of the function, while they already had a default argument in the class declaration. You can declare default arguments in the class declaration or in the function definition, but not both.
EDIT: Missed the end of your errors: error: ‘virtual’ outside class declaration. It's a rather clear compiler error: virtual keywords belongs to class declarations, not function definitions. Simply remove it from the definition of your destructor.
Corrected source:
namespace cio {
CButton::CButton(const char *Str, int Row, int Col,
bool Bordered, // No default parameter here,
const char* Border){ // here,
}
void CButton::draw(int rn){ // and here
}
CButton::~CButton(){ // No virtual keyword here
}
}
You're not allowed to repeat default arguments when you define a function. They belong only on the declaration. (The actual rule isn't quite that simple, because a definition can also be a definition, but you get the idea...)
You dont include the default parameter in your function definition, the prototype is the only one you need to include the default value into.
#include "cbutton.h"
namespace cio {
CButton::CButton(const char *Str, int Row, int Col,
bool Bordered,
const char* Border){ //remove in def
}
void CButton::draw(int rn){
}

How do I declare a C++ prototype with a void * pointer so that it can take any pointer type?

I want to create a function prototype in C++ so that there is a void * argument that can take pointers of any type. I know that this is possible in C. Is it possible in C++?
[EDIT] Here is a simplified version of the code that I am trying to get to work:
#include <stdio.h>
void func(void (f)(const void *))
{
int i = 3;
(*f)(&i);
}
void func_i(const int *i)
{
printf("i=%p\n",i);
}
void func_f(const float *f)
{
printf("f=%p\n",f);
}
void bar()
{
func(func_i);
}
And here is the compiler output:
$ g++ -c -Wall x.cpp
x.cpp: In function ‘void bar()’:
x.cpp:21: error: invalid conversion from ‘void (*)(const int*)’ to ‘void (*)(const void*)’
x.cpp:21: error: initializing argument 1 of ‘void func(void (*)(const void*))’
$ %
You may use void*, just as with C, but you'll need to cast your argument when calling it. I suggest you use a template function
template<typename T>
void doSomething(T* t) {...}
Yes.
int i = 345;
void * ptr = &i;
int k = *static_cast&lt int* &gt(ptr);
UPDATE ::
What you have shown in the code certainly cannot be done in C++.
Casting between void and any other must always be explicitly done.
Check these SO link for more details on what the C -standard has to say:
1) http://stackoverflow.com/questions/188839/function-pointer-cast-to-different-signature
2) http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type
How about:
void func(void *);
exactly like in C? : P