Problems with copy constructor and vector - c++

I met a problem with copy constructor and vector.
#include <iostream>
#include <vector>
using namespace std;
class B {
private:
int val;
};
class A {
private:
B b;
public:
A() {}
A(A& a) {}
};
int main(int argc, char const *argv[])
{
A aa;
A a2 = aa;//it works well
//addA(va, aa);
return 0;
}
But when I do something like this:
#include <iostream>
#include <vector>
using namespace std;
class B {
private:
int val;
};
class A {
private:
B b;
public:
A() {}
A(A& a) {}
};
void addA(std::vector<A>& va, A a) {
va.push_back(a);
}
int main(int argc, char const *argv[])
{
std::vector<A> va;
A aa;
A a2 = aa;
addA(va, aa); // I got a compile error
return 0;
}
there is a compiler error.
But if I change the args in class A's copy constructor to const A&, it works well.
Why is this so?

The argument to std::vector<A>::push_back is const A &, so when the vector tries to copy that into the storage it allocated for the element, it requires a constructor that takes const A &. Because yours doesn't, it can't be used.

Related

What syntax should i use for member initializers?

What syntax should i use for member initializers in c++?
I know how to use it for Attribute but not for functions.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int a, int b,int c):
regVar(a), constVar(b),function(c)
{}
public:
int regVar;
const int constVar;
void function(int a){
cout << a;
}
};
int main(){
MyClass myclass(10,20);
cout << myclass.regVar;
}
Thanks in advance
I tried function(c), c.function etc.

How to use function pointer between 2 classes

i have 2 classes say "Class A" and "Class B". i tried to declare the function pointer prototype in "Class A" and use it in "Class B", but failed. Please look in to my sample code and assist me how to make it success.
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
int (*funcPtr)(int,int);
void PointerTesting(int (*funcPtr)(int,int))
{
//i need to get B::test as an function pointer argument
}
};
class B
{
public:
int test(int a,int b)
{
return a+b;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int (A::*fptr) (char*) = &B::test;
getchar();
return 0;
}
Recommendation: Use < functional >, std::function and std::bind
#include <iostream>
#include <functional>
using namespace std;
class A {
public:
using FnPtr = std::function<int(int, int)>;
void PointerTesting(const FnPtr& fn) {
//i need to get B::test as an function pointer argument
// Example: Print 1 and 2's sum.
int result = fn(1, 2);
std::cout << "Result: " << result << std::endl;
}
};
class B {
public:
int test(int a,int b) {
return (a+b);
}
};
int main(int argc, char* argv[]) {
A a;
B b;
A::FnPtr ptr = std::bind(&B::test, b, std::placeholders::_1, std::placeholders::_2);
a.PointerTesting(ptr);
getchar();
return 0;
}
http://en.cppreference.com/w/cpp/utility/functional/bind
http://en.cppreference.com/w/cpp/utility/functional/function

Initialization by constructor in c++

I have question about constructor,
why following code works correctly:
#include <iostream>
using namespace std;
class mycl
{
private:
int a;
//struct
//{
char b,c;
//} ms;
public:
mycl (int _a,char _b,char _c):a (_a), b (_b), c (_c){}
};
int main() {
// your code goes here
mycl slc (15, 'a', 'f');
return 0;
}
https://ideone.com/wBgM1b
but there is a compilation error in this one
https://ideone.com/Yqxvzk
is it possible to initialize members of complex types this way?
p.s.
thank for translate and for answer.
sorry about wrong language
You want:
mycl(int _a, char _b, char _c) : a(_a), ms{_b, _c} {}
// ^^^^^^^^^^

create an unary_function functor for non-static member function

The code should explain my difficulty. Though the code itself is quite meaningless, I'm planning to add containers in MyClass, and use algorithms with member functions.
#include <cstdlib>
#include <algorithm>
#include <functional>
using namespace std;
class MyClass
{
public:
MyClass() { a = 0; }
~MyClass() {}
private:
int a;
bool tiny_test (int);
int Func();
};
bool MyClass::tiny_test (int b)
{
return a == b;
}
int MyClass::Func()
{
// does not compile
(mem_fun(&MyClass::tiny_test))(this);
// commented below is another attempt, also no success
//mem_fun1_t<bool, MyClass, int> tmp_functor = mem_fun(&MyClass::tiny_test);
//tmp_functor(this);
return 0;
}
int main(int argc, char** argv)
{
return 0;
}
Thanks a lot! Btw, I'm not using a static member function, simply because I believe it must work for non-static member functions.
P.S. Eric, Jarod42, thanks for prompt replies!
bool MyClass::tiny_test (int b)
{ // ^^^^^ You missed this argument
return a == b;
}
Try this:
// Supply one more argument. E.g., 3
(mem_fun(&MyClass::tiny_test))(this, 3);

boost::phoenix::static_cast_ compile errors

I'm encountering a compile error when trying to static cast an argument inside of a boost phoenix lambda. The errors themselves are way too long, so I've posted them to pastebin here.
I've created a minimal example showing what I am trying to do. If I make the variable b into an A pointer and thusly don't cast, then everything compiles and runs correctly. Does anyone know why this is happening?
Minimal example (compile with "clang++ -lboost_thread phoenix_test.cpp"):
#include <boost/phoenix.hpp>
#include <iostream>
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;
class A
{
public:
A(int a)
: mA(a)
{};
int GetX() const {return mA;};
protected:
int mA;
};
class B : public A
{
public:
B(int a)
: A(a)
{};
int GetX() const { return mA + 1; }
};
int main (void)
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (static_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
The compiler used was clang 3.4, and gcc 4.6.3 was also tried.
The error messages seem to imply that you were using dynamic_cast_ there.
Dynamic casts require virtual classes. Add a virtual member to the base class (e.g. the destructor)
Sample:
Live On Coliru
#include <boost/phoenix.hpp>
#include <iostream>
namespace px = boost::phoenix;
using namespace px::arg_names;
using namespace px::local_names;
class A {
public:
virtual ~A() {}
A(int a) : mA(a){};
int GetX() const { return mA; };
protected:
int mA;
};
class B : public A {
public:
B(int a) : A(a){};
int GetX() const { return mA + 1; }
};
int main()
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (px::dynamic_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
Prints
6