C++ no matching function for call - c++

I have error: No matching function for call to 'Goo::Goo()'
This problem is happening to often, can somebady explain to me where do i make mistakes all the time. I How can i overcome this.
Here is the code of the progam:
#include <iostream>
using namespace std;
class Goo{
private:
int a[10];
int n;
public:
Goo(int x){
n=x;
}
Goo(const Goo &g){
this->n=g.n;
for(int i=0;i<g.n;i++){
this->a[i]=g.n;
}
}
Goo operator=(const Goo &g){
this->n=g.n;
for(int i=0;i<g.n;i++){
this->a[i]=g.n;
}
return *this;
}
Goo operator+(const Goo &g){
Goo goo;
for(int i=0;i<g.n;i++){
goo.a[i]=this->a[i]+g.a[i];
}
return goo;
}
friend istream& operator>>(istream &in,Goo &g){
in>>g.n;
for(int i=0;i<g.n;i++){
in>>g.a[i];
}
return in;
}
friend ostream& operator<<(ostream &out,Goo &g){
for(int i=0;i<g.n;i++){
out<<g.a[i]<<" ";
}
return out;
}
};
int main()
{
Goo A,B;
cin>>A>>B;
Goo C=A+B;
cout<<C;
return 0;
}

When you define a custom constructor (among other reasons), the class no longer has a default constructor:
struct Foo {
int x;
};
Foo foo; // OK
struct Foo {
int x;
Foo(int x_) : x{x_} { }
};
Foo foo; // error
You can fix this by either adding a custom default constructor:
struct Foo {
int x;
Foo() { }
Foo(int x_) : x{x_} { }
};
or having at least one constructor with all default parameters:
struct Foo {
int x;
Foo(int x_ = 0) : x{x_} { }
};
Since C++11, you can also force the compiler to emit the default constructor:
struct Foo {
int x;
Foo() = default;
Foo(int x_ = 0) : x{x_} { }
};

Related

friend function in C++ is not accessing private members

I am learning C++ and I have written the below given simple program to understand the working of friend function
(ignore all the complication I made by using complex syntax in the code because I am learning and I practice the syntax that I learn in programs).
The friend function is not accessing the private members of test and stu.
#include<iostream>
#include<conio.h>
class test;
class stu
{
private:
int z;
public:
stu(int z)
{
this->z=z;
}
friend disp(stu,test);
~stu(void)
{
std::cout<<"Destructor of stu class is executed!!"<<std::endl;
}
};
class test{
private:
int x;
public:
test(int a)
{
x=a;
}
friend disp(stu,test);
~test(void)
{
std::cout<<"Destructor is executed!!"<<std::endl;
}
};
class test2:public test
{
private:
int b;
public:
test2(int b)
{
this->b=b;
}
void show(void);
~test2(void)
{
std::cout<<"Destructor of second class executed!!"<<std::endl;
}
};
int main()
{
test t1(3);
test2 t2(5);
t2.show();
stu s1(10);
disp(s1,t1);
return 0;
}
void test2::show(void)
{
std::cout<<"Value of k = "<<b<<std::endl;
}
void disp(stu s2, test t2)
{
int sum;
sum = s2.z + t2.x;
std::cout<<"Sum = "<<sum<<std::endl;
}
Try to define the disp function before the main function :
void disp(stu s2, test t2)
{
int sum;
sum = s2.z + t2.x;
std::cout<<"Sum = "<<sum<<std::endl;
}
int main()
{
test t1(3);
test2 t2(5);
t2.show();
stu s1(10);
disp(s1,t1);
return 0;
}
and change the disp function signature as:
friend void disp(stu,test);

How to code operation overload so it operates to a specific class member such as A<<{{1,2,3},{5,6,7}} in c++?

You can redefine operator << in class by overload it.
However, how do you code it so that it would operates specific to a certain class member?
for example
class C
{
int a;
double b;
}
// I would like something like
void main ()
{
C c;
c.a << 1; // sets class member a to value 1;
}
I want a operator defined in Class C that operates specifically to class member a.
a pesudo-code would be
class C
{
int a;
double b;
void operator << (istream & fin)
{
... fin.get()... some code
}
}
Stating the obvious for a moment, assuming the variable is public, you'd use:
class C
{
int a;
double b;
}
// I would like something like
void main ()
{
C c;
c.a = 1; // sets class member a to value 1;
}
The << and >> operators are bit shifts, which have their own meaning. Overloading those for your own purpose is probably a bad idea.
The C++ way of doing things is to avoid setting member variables externally where possible (e.g. using RAII approaches, to set data at initialisation)....
class C
{
public:
C(int a, double b) : a(a), b(b) {}
int getA() const { return a; }
double getB() const { return b; }
private:
int a;
double b;
};
.... Or by adding a setter method if you really need it, e.g.
class C
{
public:
C(int a, double b) : a(a), b(b) {}
int getA() const { return a; }
double getB() const { return b; }
void setA(int v) { a = v; }
void setB(double v) { b = v; }
private:
int a;
double b;
};
You could in theory generate a new type, and overload the operators for that type, but it's not something I'd recommend (because changing the meaning of an operator is almost always a bad idea)
struct MyIntType {
int i;
// overload cast operator
operator int () {
return i;
}
// assign
MyIntType& operator = (const int& v) {
i = v;
return *this;
}
// not recommended :(
MyIntType& operator << (const int& v) {
i = v;
return *this;
}
};
class C
{
public:
MyIntType a;
double b;
};
void main ()
{
C c;
c.a << 1;
}
Having read your comment above, it sounds like you want to do this:
class C
{
public:
// I'm still not recommending this :(
C& operator << (const int& v) {
a = v;
return *this;
}
private:
int a;
double b;
};
void main ()
{
C c;
c << 1; //< now sets c.a
}

the differences between function-object and function-pointer?

i defined a class, then save the pointer to Foo in the priority_queue, and use the cmp-function that i defined.
but if the cmp-funtion calls the function-object, an error occurs:
class Foo
{
friend bool cmp(Foo *, Foo *);
public:
Foo() = default;
Foo(int x):val(x) {}
private:
int val;
};
bool cmp(Foo *a, Foo *b)
{
return a->val < b->val;
}
int main()
{
priority_queue<Foo*, vector<Foo*>, decltype(cmp)*> que;
que.push(new Foo(5));
que.push(new Foo(6));
return 0;
}
the functione-object runs normally.
class Foo
{
friend struct cmp;
public:
Foo() = default;
Foo(int x):val(x) {}
private:
int val;
};
struct cmp
{
bool operator()(Foo *a, Foo *b)
{
return a->val < b->val;
}
};
int main()
{
priority_queue<Foo*, vector<Foo*>, cmp> que;
que.push(new Foo(5));
que.push(new Foo(6));
return 0;
}
You need to construct your que variable with the function you wish to use as a comparison.
#include <vector>
#include <queue>
using namespace std;
class Foo
{
friend bool cmp(Foo*, Foo*);
public:
Foo() = default;
Foo(int x):val(x) {}
private:
int val;
};
bool cmp(Foo* a, Foo* b)
{
return a->val < b->val;
}
int main()
{
// vvv
priority_queue<Foo*, vector<Foo*>, decltype(cmp)*> que(cmp);
que.push(new Foo(5));
que.push(new Foo(6));
return 0;
}

Dereferencing pointer to functor inside a dereferenced class

I have a functor like this
struct foo
{
int a;
foo(a) : a(a) {}
int operator()(int b) { return a+b; }
};
And a class like this
class bar
{
public:
foo* my_ftor;
bar(foo* my_ftor) : my_ftor(my_ftor) {}
~bar() {}
};
Then suppose a pointer to this class, which contains a pointer to foo.
foo MyFoo(20);
bar MyBar(&MyFoo);
In a function I pass a reference to bar, and I want to run the functor. I got it working the following way:
void AnyFunction(bar* RefToBar)
{
int y;
y = RefToBar->my_ftor->operator()(25);
}
Is there any other "cleaner" way to dereference the functor? Something akin to
y = RefToBar->my_ftor(25);
won't work, sadly...
Any idea? Thank you
Use real references:
class bar {
public:
foo &my_ftor;
bar (foo &f) : my_ftor(f) {}
};
void AnyFunction (bar &reftobar) {
int y = reftobar.my_ftor(25);
}
And call like this
foo myFoo(20);
bar myBar (myFoo);
AnyFunction (myBar);
In the interest of completeness, here is another answer that is more of a modern approach.
class foo {
public:
foo (int i) : a(i) {}
int operator() (int x) const {
return x + a;
}
private:
int a;
};
template <typename F>
void AnyFunction (const F &func) {
int y = func(25);
}
So you can pass in a foo directly:
AnyFunction (foo (20));
Or another kind of function object, like a lambda:
AnyFunction([](int x) -> int {
return x + 20;
});
You could also extend bar to include the following function:
int run_foo (int x) const {
return my_ftor (x);
}
And bind it (#include <functional>):
AnyFunction (std::bind (&bar::run_foo, &myBar, std::placeholders::_1));
Use std::function they are designed to hold functor of any sort.
#include <functional>
#include <iostream>
struct foo
{
int _a;
foo(int a) : _a(a) {}
int operator()(int b) { return _a+b; }
};
class bar
{
public:
std::function<int (int)> _ftor;
bar(std::function<int (int)> my_ftor) : _ftor(my_ftor) {}
~bar() {}
};
void AnyFunction(bar& RefToBar)
{
int y = RefToBar._ftor(25);
std::cout << "Y: " << y << std::endl;
}
int AnotherFunction(int b)
{
return b + 11;
}
int main(int argc, char const *argv[])
{
foo MyFoo(20);
bar MyBar(MyFoo);
bar MyBar_2(AnotherFunction);
bar MyBar_3([](int b) { return b + 56; });
AnyFunction(MyBar);
AnyFunction(MyBar_2);
AnyFunction(MyBar_3);
return 0;
}
http://ideone.com/K3QRRV
y = (*RefToBar->my_ftor)(25);
(better use std::function and don't violate demeter)

Friend Function calling Static Members of Derived Classes. Not getting expected output

My first post here :)
I am having a problem with the following C++ code. I have an ABC class A, and two derived classes B and C. All of them have a static member called id:
using std::cout;
class A
{
private:
friend int bar(A& a);
static const int id = 1;
virtual void foo() = 0;
};
class B : public A
{
private :
friend int bar(A& a);
static const int id = 2;
void foo() { /*Do something*/ }
};
class C : public A
{
private:
friend int bar(A& a);
static const int id = 3;
void foo() { /*Do something*/ }
};
int bar(A& a)
{
return a.id;
}
int main()
{
B b;
C c;
cout << bar(b) << "\n";
cout << bar(c) << "\n";
return 0;
}
I was expecting this code to print out 2 and 3 - rather it prints out 1 and 1 (bar() is always using A::id). What am I doing wrong? Any ideas?
Based on the comments below, this the final code I am using. It works, but would love to hear more thoughts :)
#include <iostream>
using std::cout;
class A
{
private:
virtual void foo() = 0;
};
class B : public A
{
private:
template <typename T>
friend int bar(T& t);
static const int id = 2;
void foo() { /*do something*/ }
};
class C : public A
{
private:
template <typename T>
friend int bar(T& t);
static const int id = 3;
void foo() { /*do something*/ }
};
template <typename T>
int bar(T& t)
{
return t.id;
}
int main()
{
B b;
C c;
cout << bar(b) << "\n";
cout << bar(c) << "\n";
return 0;
}
a.id will be defined at compile-time as A::id. You would need to define a virtual member (non-static) function in class A and have it overridden in B and C to return their respective ids and call this function in bar.
Is there any way to avoid writing int foo() { return id; } for all the derived classes?
Yes, using templates. For example:
template <typename T>
int foo (T& x)
{
return x.id;
}
However, if id is private, this doesn't reduce the code by all that much.