Error: default argument given for parameter after previous specification - c++

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){
}

Related

Error binding properties and functions in emscripten

I'm trying to use emscripten to compile a c++ class and expose bindings. I'm running into an error from the compiler.
#include <emscripten/bind.h>
#include <emscripten/emscripten.h>
using namespace emscripten;
class MyClass {
private:
int _year;
int _month;
int _day;
public:
MyClass() { }
MyClass(int year, int month, int day);
int getMonth();
void setMonth(int);
int getYear();
void setYear(int);
int getDay();
void setDay(int);
bool isLeapYear();
int daysInMonth();
void increment();
void decrement();
};
EMSCRIPTEN_BINDINGS(my_sample_class) {
class_<MyClass>("MyClass")
.constructor<>()
.constructor<int, int, int>()
.function("getMonth", &MyClass::getMonth)
.function("increment", &MyClass::increment)
.function("decrement", &MyClass::decrement)
.property("year",&MyClass::getYear, &MyClass::setYear )
//.property("month", &MyClass::getMonth, &MyClass::setMonth )
//.property("day",&MyClass::getDay, &MyClass::setDay )
;
}
The compiler has no problems with the constructors or the function binding. I run into a problem with the property binding. I only have one uncommented to minimize the errors that I get back (they are just repeats of the same error but for different lines). Here are the errors that I'm getting back.
In file included from MyDate.cpp:1:
In file included from ./MyDate.h:2:
emscripten/bind.h:1393:26: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
auto gter = &GP::template get<ClassType>;
^
./MyDate.h:37:6: note: in instantiation of function template specialization 'emscripten::class_<MyClass, emscripten::internal::NoBaseClass>::property<int (MyClass::*)(), void (MyClass::*)(int)>' requested here
.property("year",&MyClass::getYear, &MyClass::setYear )
^
emscripten/bind.h:569:16: note: template is declared here
struct GetterPolicy;
^
emscripten/bind.h:1399:33: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
TypeID<typename GP::ReturnType>::get(),
^
emscripten\1.38.21\system\include\emscripten/bind.h:569:16: note: template is declared here
struct GetterPolicy;
^
2 errors generated.
shared:ERROR: compiler frontend failed to generate LLVM bitcode, halting
I've looked up binding examples and it appears I'm using the right syntax. Does any one have any idea of what I might be doing wrong?
Found the problem!
The getter functions must be marked as const to avoid these errors.
EX:
int getMonth() const;

No matching function call call to constructor in header file

I have seen similar questions asked and tried their solutions but the answers to them do not seem to work. I have the following code:
.h
#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;
struct DialogueNode;
struct DialogueOption {
string text;
DialogueNode *next_node;
int return_code;
DialogueOption(string t, int rc, DialogueNode * nn) : text{t},
return_code{rc}, next_node{nn} {}
};
struct DialogueNode {
string text;
vector <DialogueOption> dialogue_options;
DialogueNode();
DialogueNode(const string &);
};
struct DialogueTree {
DialogueTree() {}
void init();
void destroyTree();
int performDialogue();
private:
vector <DialogueNode*> dialogue_nodes;
};
.cpp
#include "dialogue_tree.h"
DialogueNode::DialogueNode(const string &t) : text{t} {}
void DialogueTree::init() {
string s = "Hello";
for(int i = 0; i < 5; i++) {
DialogueNode *node = new DialogueNode(s);
dialogue_nodes.push_back(node);
delete node;
}
}
void DialogueTree::destroyTree() {
}
int DialogueTree::performDialogue() {
return 0;
}
int main() {
return 0;
}
I get the error: error: no matching function for call to ‘DialogueNode:: DialogueNode(std::__cxx11::string&)’ DialogueNode *node = new DialogueNode(s);
EDIT additional notes on error
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode()
dialogue_tree.h:17:8: note: candidate expects 0 arguments, 1 provided
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(const DialogueNode&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const DialogueNode&’
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(DialogueNode&&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘DialogueNode&&’
Which makes no sense to me because I have the constructor defined to take a string as an argument.
You've declared your constructor as:
DialogueNode(const string);
But defined it as:
DialogueNode(const string &t);
Those two aren't the same; the former takes a const string while the latter takes a const string reference. You'll have to add the & to specify a reference argument:
DialogueNode(const string &);
it is because in the constructor you are specifying that the parameter will be a string of constant type and when creating an object you are passing a string. The type mismatch is the problem, either fix the constructor parameter to string or change when you are creating an object.

cannot convert 'int (B::*)(std::string)' to 'int (*)(std::string) ' in assignment pt2function=&B::generate_callback;

I am new to c++, .I am trying to create a pgm that contains 2 classes ,out of which one class has a member function that would generate a callback function in another class though a function pointer, but i keep getting the following error.
#include <iostream>
#include <string>
using namespace std;
class B
{
private: std::string str1;
public: int generate_callback(std::string str1);
};
int B::generate_callback(std::string str1)
{
if ((str1=="Generate")||(str1=="generate"))
{
Cout<<"Callback generated ";
}
return 0;
}
class A : public B
{
public:
void count(int a,int b);
private: int a,b;
};
void A::count(int a, int b)
{
for ( a=1;a<b;a++){
if(a==50)
{
cout<<"Generating callback ";
goto exit;
}
exit: ;
}
}
int (*pt2function)(string)=NULL;
int main()
{
B obj1;
A obj2;
string str;
cout<<"To generate callback at int i=50 please enter 'generate'";
cin>>str;
obj2.count(1,100);
pt2function=&B::generate_callback;
(obj1.*pt2function)(str);
return 0;
}
The errors :
main.cpp:57: error: cannot convert 'int (B::*)(std::string) {aka int (B::*)(std::basic_string<char>)}' to 'int (*)(std::string) {aka int (*)(std::basic_string<char>)}' in assignment
pt2function=&B::generate_callback;
/home/adt/practice/N_practise/n_pract_2/pract2/main.cpp:58: error: 'pt2function' cannot be used as a member pointer, since it is of type 'int (*)(std::string) {aka int (*)(std::basic_string<char>)}'
(obj1.*pt2function)(str);
^
^
The variable pt2function is a pointer to a non-member function. Such a pointer is not compatible with a pointer to a member-function. Which is what the compiler tells you with the first error: A int (*)(string) is not compatible with a int (B::*)(string).
You need to define pt2function as a pointer to a B member function:
int (B::*pt2function)(string)=NULL;
Now you can initialize or assign a matching member function of B to the variable pt2function.
This also solves the second errors, which basically says that in your current code the variable pt2function is not a pointer to a member function, and therefore can not be used as such.
Pointers to functions and pointers to member functions are really different beasts.
You have mainly two options to get it working in your code:
Change this line:
int (*pt2function)(string)=NULL;
To this:
int (B::*pt2function)(string)=NULL;
That is defining pt2function as a pointer to a member function of B that gets a string and returns an int.
Declare the generate_callback as a static method and invoke it as pt2function(str); in your main function.
In fact, a static member function can be assigned to a pointer to function like the one you have already in use.

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.

C++ errors, maybe coming from .h files, not sure

Ok, I am trying to compile a program:
g++ -std=c++0x a1test.cpp GS1Prefix.cpp EAN.cpp
But I am getting errors that I never seen before.
In file included from a1test.cpp:17:0:
EAN.h:3:25: error: âPrefixâ does not name a type
EAN.h:3:33: error: ISO C++ forbids declaration of âpâ with no type [-fpermissive]
a1test.cpp: In function âbool RegisteredTests(const Prefix*, int&, int*, int*)â:
a1test.cpp:222:68: error: no matching function for call to âisRegistered(const Prefix*&, const char [14], char [6], char [8], char [7])â
a1test.cpp:222:68: note: candidates are:
EAN.h:3:6: note: bool isRegistered(const int*, const char*, char*, char*, char*)
EAN.h:3:6: note: no known conversion for argument 1 from âconst Prefix*â to âconst int*â
GS1Prefix.h:10:6: note: bool isRegistered(const Prefix*, int)
GS1Prefix.h:10:6: note: candidate expects 2 arguments, 5 provided
This is just half of the errors, below i have all the .h files and a link to a1test.cpp (its a long code)
EAN.h
bool isValid(const char* str);
bool isRegistered(const Prefix* p, const char* str, char area[],char publisher[], char title[]);
GS1Prefix.h
const int MAX = 700;
struct Prefix {
int no; // number of entries
int area[MAX]; // area elements
char pubLow[MAX][8]; // low end of publisher range
char pubHgh[MAX][8]; // high end of publisher range
int pubLen[MAX]; // no of chars in publisher string
};
bool load(const char* filename, Prefix* p);
bool isRegistered(const Prefix* p, int area);
int minNoDigits(const Prefix* p, int area);
bool isRegistered(const Prefix* p, int area, const char* publisher);
Link to a1test.cpp ->a1test.cpp
UPDATE: As suggested by remyabel, i made #include "GS1Prefix.h" after #include "EAN.h"
In file included from EAN.cpp:6:0:
EAN.h:3:25: error: âPrefixâ does not name a type
EAN.h:3:33: error: ISO C++ forbids declaration of âpâ with no type [-fpermissive]
Simply switch the order of your headers and your code should compile fine. Because you did not provide GS1Prefix.cpp and EAN.cpp I cannot comment on the rest of the errors (if there are any left.)
#include <cstring>
// GS1Prefix.h
const int MAX = 700;
struct Prefix {
int no; // number of entries
int area[MAX]; // area elements
char pubLow[MAX][8]; // low end of publisher range
char pubHgh[MAX][8]; // high end of publisher range
int pubLen[MAX]; // no of chars in publisher string
};
// EAN.h
bool isValid(const char* str);
bool isRegistered(const Prefix* p, const char* str, char area[],char publisher[], char title[]);
// a1test.cpp
bool load(const char* filename, Prefix* p);
bool isRegistered(const Prefix* p, int area);
int minNoDigits(const Prefix* p, int area);
bool isRegistered(const Prefix* p, int area, const char* publisher);
// .. rest of your file
Header files should include sufficient definition so that they are self-sufficient. In this case you need to supply a definition of the requisite type, Prefix. Just add the include to the top of EAN.h:
#include <GS1Prefix.h>
bool isValid(const char* str);
bool isRegistered(const Prefix* p, const char* str, char area[],char publisher[],
char title[]);
Then you can include EAN.h in any source file knowing that you don't have dependencies to worry about.