Using an iterator template class as a parameter to a function - c++

return Graph_iterator_Vertex(Vertexs.begin());
I do not understand why this line is called the copy constructor, although there is my constructor with a parameter. The arguments constructor parameter written specifically for this design.
All that I have found about a similar issue is the use of typename. But it did not solve my problem.
struct Graph_iterator_Vertex
{
private:
typename std::vector<Vertex<Type_Of_Value_Vertex, Type_Of_Value_Edge>>::iterator iterator;
public:
Graph_iterator_Vertex(typename std::_Vector_const_iterator<std::vector<Vertex<Type_Of_Value_Vertex, Type_Of_Value_Edge>>> iterator_)
{
iterator=iterator_
}
};
const Graph_iterator_Vertex begin(void) const
{
return Graph_iterator_Vertex(Vertexs.begin());
}
where
std::vector<Vertex<Type_Of_Value_Vertex, Type_Of_Value_Edge>> Vertexs;
Error:
error C2440: <function-style-cast>: can not be converted std::_Vector_const_iterator<_Myvec>"in "Graph<Type_Of_Value_Vertex,Type_Of_Value_Edge>::Graph_iterator_Vertex"

you should use
std::vector<Vertex<Type_Of_Value_Vertex, Type_Of_Value_Edge>>::const_iterator
instead of
std::_Vector_const_iterator<std::vector<Vertex<Type_Of_Value_Vertex, Type_Of_Value_Edge>>>
as the type of your function parameter.

Related

Return value type for class member function

I have a couple of A like classes with method, say, get_value(), which return values of different types. And one class B, which returns a value with method value(). For example:
struct A_like1 {
int get_value() const { return 10; }
};
struct A_like2 {
char get_value() const { return 'a'; }
};
struct B
{
float value() const { return 2.5; }
};
Now, I need a function to retrieve that value for these classes. I have a function:
template<typename T>
auto get_value(const T & t) -> result_of<decltype(&T::get_value)(T)>::type
{
return t.get_value();
}
It gives me a number of compilation errors on VS2010 starting with:
Error 1 error C2061: syntax error : identifier 'type' c:\_data\work\vmxi\c++\vox\vox\template_specialization.h 51
For B I have an overload, which works fine. The question is, how to make get_value() work with result_of<>()?
P.S. Hmm, I have just realized that I could've used -> decltype(T().get_value()) instead, which works fine. But why doesn't result_of<>() work? Besides, I was able to declare a variable in .cpp file:
result_of<decltype(&A_like1::get_value)(A_like1)>::type i=0;
The keyword typename can be used to declare that a dependent name, like std::result_of<T>::type, is a type. Otherwise, I believe it's assumed that std::result_of<T>::type is a data member. In C++14, several type traits have gotten a using alias that includes the typename keyword for you. These alias are always have the same name as the trait, with a _t suffix.
Try :
typename result_of<decltype(&T::get_value)(T)>::type
or, with C++14 :
result_of_t<decltype(&T::get_value)(T)>

How to fix discards qualifiers [-fpermissive] for cv::SVMParams?

I have a class ConfigFile that has a getter for the SVMParams member:
cv::SVMParams gerSVMParams()
{
return *m_mapConfig["SVMParams"];
}
The code is a little bit more complicated. m_mapConfig is a
std::map<std::string, std::unique_ptr< IConfigItem > >
And IConfigItem is a template class that looks like this for SVMParams:
template<> class ConfigItem< cv::SVMParams > : public IConfigItem
{
private:
cv::SVMParams m_value;
public:
ConfigItem(const cv::SVMParams& valueIn) : m_value(valueIn) {}
operator cv::SVMParams() const
{
return m_value;
}
};
My problem is when I am trying to auto train the SVM classifier:
classifier.train_auto(trainingData, classes, cv::Mat(), cv::Mat(), configFileIn.getSVMParams());
I am getting an error of kind:
error: passing ‘const ConfigFile’ as ‘this’ argument of ‘cv::SVMParams ConfigFile::getSVMParams()’ discards qualifiers [-fpermissive]
Any suggestions of what I am doing wrong? Or is there a small bug because because the train_auto functions has no const in front of the SVMParams parameter. Or is it modifying it?
Make your function const:
cv::SVMParams gerSVMParams() const
// ^^^^^
The error is you are calling a non-const method on a const object, which the compiler rejects as being potentially unsafe. That said, your implementation is inherently non-const too since you might be inserting an object into your map, so just adding the const won't help.
What you probably want to do is:
cv::SVMParams gerSVMParams() const
// ^^^^^
{
auto it = m_mapConfig.find("SVMParams");
if (it != m_mapConfig.end()) {
return *(*it);
}
else {
return {}; // maybe?
}
}
Found the problem: I was calling the function in which I was calling the getSVMParams() with const ConfigFile&; and that was not allowed by the shared_ptr I used in the map.
Thanks all, anyway!

C++11 std::function: Binding a method without a particular instance

Continuing this thread I'd like to split it off into another more specific question. I want to bind a function using ChaiScript, and I can do that using std::function, but I can't seem to let std::function know which overload it should use.
Mirroring relevant methods:
class DLLExport Actor
{
public:
Actor(Level* level, const String& name, Actor* parent);
virtual ~Actor();
void setPosition(const Real& x, const Real& y, const Real& z);
void setPosition(const Vector3& position);
};
and then I'm trying to bind it like so:
std::function<void(Actor*, Vector3&)> setPos = &Actor::setPosition; // <-- C2440 here.
m->add(fun(setPos), "setPosition");
What I'm getting is the following error:
2>..\..\ScriptingDemo\Binder.cpp(63): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'std::function<void (Actor *, Vector3 &)>'
2> No constructor could take the source type, or constructor overload resolution was ambiguous
Use a typedef to your member function :
typedef void (Actor::*set_type)(const Vector3&); // Note the syntax for a member function typedef
And use it for a clean static_cast to resolve the overload :
std::function<void(Actor&, const Vector3&)> setPos = static_cast<Actor::set_type>(&Actor::setPosition);
( note the implicit Actor& parameter to the member function, explicit in the above std::function<> signature)
C++ overloaded method pointer.
Basically, put in the cast manually.
std::function<void(Actor*, const Vector3&)> setPos((void(Actor::*)(const Vector3&))&Actor::setPosition);
Now the why:
The = operator for std::function is (itself) a template and provides no type information for its argument. So when you try to assign it to &Actor::setPosition, there is no good way for the compiler to figure out which function you are asking for. Putting in the cast fixes that.
The Actor* argument comes from the type of function Actor::* rather than the function signature itself.
You're trying to convert a member function pointer to a free function pointer, and that can't work. Were that possible, you forgot the const in the std::function template arguments anyway.
Your inclusion of an Actor* is on the right track though :
#include <iostream>
#include <functional>
struct Foo {
void bar(int const &i) { std::cout << i; }
};
int main()
{
using namespace std::placeholders;
std::function<void(Foo*, int const&)> func = std::bind(&Foo::bar, _1, _2);
Foo foo;
func(&foo, 17);
return 0;
}
Note that you still need to provide an instance to call the method on at one point, but what would you expect ?

type casting problem with template class in C++

I am not a template or type casting expert, so I really need some help.
I have to use an existing template class and encounter the following type cast problem.
I put some sample code to illustrtrate the problem.
//template class definition
template <class IntType>
class CUSTOMIZE_Int: public CUSTOMIZE_Type
{
public:
operator const IntType() const;
private:
IntType m_int;
}
template<class IntType>
CUSTOMIZE_Int<IntType>::operator const IntType() const
{
return m_int;
}
// the template class instantiation
typedef CUSTOMIZE_Int<WRAPPER_Int32> CUSTOMIZE_UnsignedInt;
Then in my code, I derive a new class
// the derived class definition
class IntNum: public CUSTOMIZE_UnsignedInt
{
// ctors and new methods;
}
and creat a variable, and try to make the conversion to get the data.
class IntNum& i;
const WRAPPER_Int32 j = i;
with the following compile error:
error: cannot convert "IntNum" to "const WRAPPER_Int32" in initialization.
What is the right way to do the conversion, or what is the problem with my code?
Thanks for any comments!
I guess that you expect operator IntType()
class ...
operator const IntType() const;
}
to act here, when you assign i to j:
const WRAPPER_Int32 j = i;
However, this is not an implicit conversion, you must do it explicitely:
const WRAPPER_Int32 j = (WRAPPER_Int32) i;
Hope this helps.

Why is this default template parameter not allowed?

I have the following class:
template <typename Type = void>
class AlignedMemory {
public:
AlignedMemory(size_t alignment, size_t size)
: memptr_(0) {
int iret(posix_memalign((void **)&memptr_, alignment, size));
if (iret) throw system_error("posix_memalign");
}
virtual ~AlignedMemory() {
free(memptr_);
}
operator Type *() const { return memptr_; }
Type *operator->() const { return memptr_; }
//operator Type &() { return *memptr_; }
//Type &operator[](size_t index) const;
private:
Type *memptr_;
};
And attempt to instantiate an automatic variable like this:
AlignedMemory blah(512, 512);
This gives the following error:
src/cpfs/entry.cpp:438: error: missing template arguments before ‘blah’
What am I doing wrong? Is void not an allowed default parameter?
I think that you need to write:
AlignedMemory<> blah(512, 512);
See 14.3 [temp.arg] / 4:
When default template-arguments are used, a template-argument list can be empty. In that case the empty <> brackets shall still be used as the template-argument-list.
Your syntax is wrong:
AlignedMemory blah(512, 512); //wrong syntax
Correct syntax is this:
AlignedMemory<> blah(512, 512); //this uses "void" as default type!
The error message itself gives this hint. Look at it again:
src/cpfs/entry.cpp:438: error: missing template arguments before
‘buf’
PS: I'm sure 'buf' is a typo. You wanted to write 'blah' - the name of your variable!