boost python optional raises an error - c++

I have the following code:
//test.cpp
#include <Physical_file.h>
#include <boost\python.hpp>
using namespace boost::python;
using namespace FMS_Physical;
BOOST_PYTHON_MODULE(python_bridge)
{
class_<Physical_file, boost::noncopyable>("pf")
.def(init<const string&, optional<int, const string&>>())
;
}
//Physical_file.h
#include <fstream>
#include "tools.h"
using namespace std;
namespace FMS_Physical
{
class Physical_file
{
public:
fstream filefl;
string workingDir;
string fileName;
int fileSize;
block currBlock;
block FHBuffer;
bool opened;
string openMode;
/************
functions
************/
Physical_file(void);
Physical_file(const string &FileName, int FileSize,const string &Dir = getCurrentPath());
Physical_file(const string &FileName, const string &Type, const string &Dir = getCurrentPath());
~Physical_file(void);
};
}
there is some more code, I think it's irrelevant to the question.
when I try to compile the code, I get the following error:
Error 5 error C2664: 'FMS_Physical::Physical_file::Physical_file(const FMS_Physical::Physical_file &)' : cannot convert parameter 1 from 'const std::string' to 'const FMS_Physical::Physical_file &'
when I delete the optional from the definition of the constructor (in test.cpp) the error disappears, but I don't get the optional parameters.
I'm compiling using VS2010, python27 and c++ boost libs.
Can any one explain why I get this error and how can I solve it?
EDIT
I tried exposing the third constructor instead using the following line:
.def(init<const string&, const string &, optional<const string &>>())
this didn't cause any errors.

You have forgotten to give a default to FileSize.
For this declaration to work: init<const string&, optional<int, const string&>>
you need a constructor that can be called with only a const string& parameter. You do not have such at the moment, which is the exact complaint of the compiler.

Related

C++ Error when calling contructor (Codecademy)

I'm taking a C++ Codecademy course and I ran into this error when calling my constructor.
profile.cpp:4:1: error: prototype for ‘Profile::Profile(std::__cxx11::string, int, std::__cxx11::string, std::__cxx11::string)’ does not match any in class ‘Profile’
Profile::Profile(string new_name, int new_age, string new_city, string new_pronouns) {
^~~~~~~
In file included from profile.cpp:1:0:
profile.hpp:4:7: error: candidates are: Profile::Profile(Profile&&)
class Profile {
^~~~~~~
profile.hpp:4:7: error: Profile::Profile(const Profile&)
profile.hpp:15:5: error: Profile::Profile(std::__cxx11::string, int, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)
Profile(string new_name, int new_age, string new_city, string new_country, string pronouns);
^~~~~~~
Here is my code in profile.cpp
#include "profile.hpp"
#include <vector>
using namespace std;
Profile::Profile(string new_name, int new_age, string new_city, string new_pronouns) {
name = new_name;
age = new_age;
city = new_city;
country = new_country;
pronouns = new_pronouns;
}
Here is my code in profile.hpp
#include <iostream>
#include <vector>
using namespace std;
class Profile {
private:
string name;
int age;
string city;
string country;
string pronouns;
vector<string> hobbies;
public:
Profile(string new_name, int new_age, string new_city, string new_country, string pronouns);
};
Here is my code in main.cpp
#include <iostream>
#include "profile.hpp"
int main() {
Profile sam("Sam Drakkila",30,"New York","USA","he/him");
}
I believe there is a problem with calling the constructor (as stated in title) but I am not sure. I have only started OOP a few days ago so I am very new and need a Simplified answer.
Here is the problem. I encountered an error before where I was calling 5 arguments in main.cpp but only declared 4 arguments to be called. To fix that issue I added "New_country" as a parameter. I simply forgot to update profile.cpp to add another parameter after updating profile.hpp with "new_country".

Creating an array of string_view elements throws error: unable to find string literal operator ‘operator""sv’ with

I have the following (modified) code where I want to create an array of string_view type objects.
I see this error when compiling corresponding to each line
unable to find string literal operator ‘operator""sv’ with ‘const char [8]’, ‘long unsigned int’ arguments
"Sensor2"sv,
The code:
#include <iostream>
#include <array>
#include <string_view>
struct Abc
{
static constexpr std::array<std::string_view, 6> SomeValues = {
"Sensor1"sv,
"Sensor2"sv,
"Actuator1"sv,
"Actuator2"sv,
"Cpu1"sv,
"Cpu2"sv
};
};
int main()
{
Abc abc;
std::cout<<abc.SomeValues[3];
return 0;
}
You need using namespace std::literals;.
See also this question.

C++ compile error: redefinition of 'class::class'

I have added the errors within comment lines of the code for the highlighted lines by the compiler.
header file:
#ifndef ADDRESS_H_EXISTS
#define ADDRESS_H_EXISTS
#include <iostream>
#include <string>
using namespace std;
class Address{
private:
string address1;
string address2;
string city;
string state;
string zipCode;
public:
Address(){} //note: 'Address::Address()' previously defined here|
Address(
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
){}
NOTE: #endif exists at the end of header file
source file:
#include <iostream>
#include <string>
#include "address.h"
using namespace std;
Address::Address(){} // error: redefinition of 'Address::Address()'
Address::Address( // error: redefinition of 'Address::Address(const string&,
// const string&, const string&, const string&, const string&)'|
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
):
address1(address1),
address2(address2),
city(city),
state(state),
zipCode(zipCode)
{
Address::address1 = address1_c;
Address::address2 = address2_c;
Address::city = city_c;
Address::state = state_c;
Address::zipCode = zip_c;
}
All of the most popular questions about this error concluded that header guards were needed, although, there are guards already included in this code.
I thought I was misunderstanding how to properly separate the initialization list between header and source files but when I commented that out it was still producing the same error.
What you're typically supposed to do is define the function prototypes in the header file, and the function definition in the source file. However, in your header file, you seem to have specified a definition of the function already using the empty {} brackets. Hence the compiler is complaining that you've redefined the function definition in the source file. If you remove those two {} empty blocks in the header file and replace them with a semicolon ;, it should solve this error.
Basically, it should look like this in your header:
Address();
Address(
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
);
You are getting redefinition errors ( which i think are linker errors, and not compile errors ) because Address::Address() and Address::Address(const string&, const string&, const string&, const string&, const string&) are already defined in the header file, and you define them again in the CPP file
To avoid that, you need to replace function definition by declarations in your header file, by replacing {} by ; in your header file, this way :
public:
Address(); //By replacing '{}' by ';', you change that function definition into a function DECLARATION
Address(
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
); // Same for here
At the exception of inline and template functions, function declaration goes in header file, and definitions goes into the CPP file

static members get 'is not a static member of class'

Problem
I want to assign values in a class definition which is in a separate header file from the class declaration cpp.
On compilation I recieve theses error messages:
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mRegexes’ is not a static member of ‘class bob’const std::map<uint,std::string> bob::mRegexes = {
^
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mResponses’ is not a static member of ‘class bob’ const std::map<uint,std::string> bob::mResponses = {
both of which have been absolutely infurriating because I do not understand why the compiler is ignoring thetypedef for std::string I feel like I'm missing something here but I'm not sure why the bob.h file is seeing the parameters differently than the bob.cpp.
bob.h
#ifndef BOB_H
#define BOB_H
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <map>
typedef unsigned int uint;
// This was first to go when I started having problems.
/*using std::string;*/
using std::map;
// boost::regex > c++11::regex (gcc doesn't follow standards).
using boost::regex;
class bob
{
enum respond_to{
QUESTION,
YELL,
NAME,
DEFAULT,
LENGTH
};
public:
static const respond_to mResponseTypes;
static const map<uint,std::string> mRegexes;
static const map<uint,std::string> mResponses;
static std::string hey(std::string sentence);
static const std::string* evaluate (const std::string& sentence);
static const std::string* getResponse(const std::string& sentence, const respond_to& type) noexcept(true);
};
#endif
bob.cpp
#include "bob.h"
const std::map<uint,std::string> bob::mRegexes = {
{QUESTION, "[a-z]+\\?"},
{YELL,"[A-Z]+"}
};
const std::map<uint,std::string> bob::mResponses = {
{QUESTION,"Sure"},
{YELL,"Whoah, chill out!"},
{DEFAULT,"Whatever."}
};
// ...

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*.