C++ :: Unpacking Parameter Pack With Templated Predicate
Apr 20, 2014
I'm trying to generalize the following (working) code:
#include <iostream>
#include <string>
#include <cmath>
int foo (int num) {return num;}
int foo (double num) {return std::round (num);}
int foo (std::string str) {return str.length();}
[code]....
Template argument deduction failed, it says. And using pred_action<T> and pred_action<FIRST> only makes it worse.Do we need to use a visitor pattern or something like that?
View 2 Replies
ADVERTISEMENT
Aug 20, 2013
I have this basic prototype:
struct int_wrapper{int i;};
template <const int_wrapper&... IPack>
void display_all(const int_wrapper&, IPack...);
But when I try to compile it, the compiler says IPack is not a type on the last line. Are packs of references not allowed?
View 5 Replies
View Related
Oct 28, 2014
let me jump straight to main() and my current output:
struct Base {};
struct A : Base { A() {std::cout << "A";} };
struct B : Base { B() {std::cout << "B";} };
struct C : Base { C() {std::cout << "C";} };
struct D : Base { D() {std::cout << "D";} };
[code]...
View 10 Replies
View Related
Oct 23, 2013
I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
For exemple:
Code:
template <class T>
class A {
public:
A() { version = ???? }
std::string_base<T> version;
};
I don't want to pass the default value as parameter of the constructor. how I can do this?
View 6 Replies
View Related
Sep 28, 2014
My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.
#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;
/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);
[Code] ....
View 1 Replies
View Related
Dec 4, 2013
I am developing new project in Qt with existing MFC project . SO in MFC I have a function which uses SYSTEMTime and return CString.
example
CString getTimestampString( void )
{
SYSTEMTIME systemTime;
CString datestr;
[Code]....
PS -> I cant able to make any changes in lib_know as this library is being used by many other projects..
View 1 Replies
View Related
Jul 2, 2013
Here's my function definition
bool validateNumber(string& text, int min = 0, int max = -1, bool useMin = true,
bool getValid = true)
The code takes the string text, and checks the make sure that the input is valid and safe to convert and use as a number. However, sometimes there is not min, and sometimes there is no max. The lack of min is done by using the parameter useMin, while the lack of max is done by max < min.
My predicament is the following call:
validateNumber(text, -2);
Now, max will be used, even though I don't want it. Ideally, I would want to do something like... int max = (min - 1), ... but that doesn't work. I also can't check to see if the parameter hasn't been changed (that I know of), because the following call would make it look like it hasn't
validateNumber(text, -2, -1);
So the question is, is there a way to do what I want, without having to add in a bool useMax parameter? Or is this my only option? I don't want to do that for simplicity, but if I have to, I have to.
View 3 Replies
View Related
Sep 3, 2013
template <typename T> struct avl_tree {
T data;
int balance;
struct avl_tree <T> *Link[2];
static int (*comp)(T, T);
};
In main, I have a function like so:
int compare(int a, int b) {
return ( a - b );
}
Now how do I assign the function pointer in my avl_tree class to the compare function?
I did this:
int (avl_tree<int>::*comp)(int, int) = compare;
But I got the compiler error:
Tree_Test.cc: In function ‘int main()’:
Tree_Test.cc:27:42: error: cannot convert ‘int (*)(int, int)’ to
‘int (avl_tree<int>::*)(int, int)’ in initialization
View 12 Replies
View Related
Oct 7, 2014
I've this variadic template class implementation:
// Predefine template delegate factory
template < typename R, typename... Args >
class brGenericDelegate ;
// C++11 template alias to announce functor definition
template < typename R, typename... Args >
using brGenericDelegateType = std::function< std::shared_ptr<R>(Args...) > ;
[code]....
This template works fine for me. Now I have to compare, if the functors of the generic templates are equals ore not. Actually I see no way to reach this aim, because I've to cast the brDelegate on each request.
View 4 Replies
View Related
Apr 19, 2014
I'm trying to make an templated class of List and trying to make a list of Students(another class) which I made. Its not working.
Code seems to work fine for pre-defined data types but it crashes when it comes to students. I also want to run sort function on list of students.
View 1 Replies
View Related
Mar 12, 2012
Can I determine if a templated class has a particular constructor, in my case using a string within function to which T is used?
Code:
template<class T>
void MakeObject(std::vector<T>& dataVector)
{
std::string str "con string,Joe,24";
// catch if T has string constructor
T someObject(str); // T someObject should have constructor from string
dataVector.push_back(someObject);
}
View 1 Replies
View Related
Feb 3, 2014
This problem just seems really strange to me because it is simple yet for some reason my class cannot pass into another class. The class PASS_OBJECT has a static array (even with 1 element this doesn't work) and when I try to pass this class (after it is initialized) I seem to lose the data inside the PASS_OBJECT. Not only that but even when I declared the class OBJECT with the type of PASS_OBJECT<int> I seem to lose the integer 99. Here's the code, note that if you comment out line 89, 92 and 93 you will notice that line 90 outputs In main 2: 99 just fine but it doesn't otherwise???
#include <iostream>
const int size = 1;
template <class T>
class PASS_OBJECT;
template <class S>
class OBJECT {
[Code] ....
View 2 Replies
View Related
Nov 5, 2014
To generate output data, I'm printing a bunch of vector contents to files. Because the type of variable can differ between vectors, I wrote a templated printing function to print out whatever the content of the vector is. It looks like this:
template <class T>
void SomeClass::PrintVector(std::vector<T>& Values, std::string& outFile) {
std::ofstream out(outFile, std::ios::app);
[Code] ....
I added the fixed because some larger values were being printed in scientific notation. Everything works well. My test code includes 3 vectors of doubles and 3 vectors of unsigneds. All the unsigneds work well and two of the doubles work well, but the third doubles vector prints nonsense unless I disable the fixed.
The calling code is the exact same. I know the values in the vector are correct, because a) if I comment out the "fixed" flag it works, and b) one of the unsigned vectors is sorted based on the values in that double vector (after it is printed, so the sort cannot corrupt the vector print) and works perfectly.
The "nonsense" looks like chinese/weird characters, if that matters.
View 11 Replies
View Related
Apr 16, 2012
I declared a member method to a class in its header file and implemented it in the cpp file. When I build and run the project in XCode, everything works fine. When I try to do it with a makefile, I get undefined symbols linker errors.
parser.h
Code:
#ifndef ENGINE_SCRIPT_PARSER_H
#define ENGINE_SCRIPT_PARSER_H
#include <string>
#include "variable.h"
namespace ninja_game_engine {
[Code] ....
The exact makefile looks like this:
Code:
test:
g++
Code/Engine/Modules/Timer/timer.cpp
Code/Engine/Modules/list.cpp
[Code] ....
I'm pretty sure that there is a weird namespace gotcha that I'm unaware of that LLVM (default OSX compiler) is compensating for that g++ isn't. Or maybe something weird with the optimization? I want the tests running at that level to make sure everything that is volatile is declared as such.
Rest of the code located here: [URL] ....
View 3 Replies
View Related
Aug 19, 2013
On several occasions in my project, I need to sort elements (indeces) based on their linked values. Those values are stored in an array. This means the comparison looks like this:
bool operator()(int i, int j) { return someArray[i] > someArray[j]; }
Because this form returns often but someArray may differ, I wrapped this in a template class:
template<class T>
struct sorter {
std::vector<T>& data;
sorter(std::vector<T>& __d) : data(__d) {}
bool operator()(int i, int j) const { return data[i] > data[j]; }
};
Now, I want to use this in a different way: I want to select the K indeces with the lowest value from someArray attached to it. My idea was to build a priority_queue, push the first K elements and then compare all the next elements to PQ.top(), as such:
INDEX t(0);
for (; t < someLimit; ++t) {
pq.push(t);
if (pq.size() == K) break;
}
for (; t < someLimit; ++t) {
if (someArray[t] < someArray[pq.top()]) { pq.pop(); pq.push(t); }
}
My problem, however, is the definition / initialization of the priority_queue object. My first idea was simply std::priority_queue<INDEX> pq(sorter<VALUE>(someArray));, but calling any function on pq provides the error "expression must have class type" (?) when hovering over pq.
My second guess, std::priority_queue<INDEX, std::vector<INDEX>, sorter<VALUE>(someArray)> pq;, provides the error 'expected a ')'' when hovering over someArray.
What is the correct way to initialize this data structure?
View 1 Replies
View Related
Jul 15, 2013
I've been studying the heck out of the boost metafunction libraries. I understand a good deal of what things like varadic functions and integral sequence wrappers are, but I am having a hard time putting everything together to get working functions, such as performing arithmetic operations or functions like that of std::vector.
Here is an example of what I'm talking about:
// Sequences
template<typename T... N> struct seq;
template<typename T, T... N> struct seq_c;
// Integral constant wrapper
template<int T> struct int_
[Code] .....
My knowledge of all of this is pretty scattered and I've really been trying hard to put it all together. Is this correct? How can I apply this and use it to do more?
View 5 Replies
View Related
Feb 20, 2013
I need to create a templated doubly linked list, with an iterator class within the list class. This program is to function just like the STL list class but I only need to implement functions that I am using, My trouble is I am kind of clueless on the iterator part and the fact that the list is templated is giving me syntax grief. I have pasted the code I have done so far.
1. On the syntax implementing the list and iterator functions outside of the class
2. I am not sure when to deference the iterator in the functions, but think I have it right so far
3. For the reverse function can I copy the list into a new list in reverse then re add them to the original list overwriting the same values? I have the code I have so far there
4. For the iterator erase function, I am not sure if I am deleting the node correctly.
5. I am not sure if I need template <typename T> above the iterator functions. Does the iterator class need to be a template? Right now it is not.
// Templated doubly linked list class
#include <iostream>
using namespace std;
template <typename T>
class list {
private:
Node *head;
Node *tail;
[Code] ....
View 7 Replies
View Related
Jan 21, 2013
I want to specialize a particular function for Integer datatype inside a class template. My XX.h will be
namespace ZZ {
template <class T>
class XX {
void doSomething(T x);
};
}
provide me XX.cpp which has template specialization for the function doSomething on Integer datatype.
I tried the following in XX.cpp
#include "XX.h"
using namespace ZZ;
template <class T>
void XX<T>::doSomething(T x) {
[Code] ...
But this code is not working.
View 2 Replies
View Related
Mar 23, 2015
I am working on building a set of templated data structures for my own learning and have run in to an error when instantiating my templated linked list. I receive the following error:
error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::~LinkedList<int>(void)" (??1?$LinkedList@H@@QAE@XZ) referenced in function _main
--LinkedList.h--
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template<class T>
class LinkedList {
[code]....
why I would be receiving this error?
View 1 Replies
View Related
Dec 9, 2014
I'm making a minimal spanning tree class that inherits a graph class, both are templated. This is my mstree.h so far:
#ifndef _MSTREE_H_
#define _MSTREE_H_
#include "graph.h"
namespace MSTreeNameSpace
[Code]...
and I keep getting these errors:
mst.h:9:25: error: expected template-name before ‘<’ token
class MST : public Graph<T>
^
mst.h:9:25: error: expected ‘{’ before ‘<’ token
mst.h:9:25: error: expected unqualified-id before ‘<’ token
View 3 Replies
View Related
Feb 3, 2013
I'm trying to template the return type for this function (component), I've looked around for example code but there doesn't seem to be any exactly like what I want.
Entity.hpp
class Entity {
public:
Entity();
unsigned int id = 0;
Component& addComponent(std::string);
[Code] ....
Error : 'ent1.component<HealthComponent>' does not have class type
View 2 Replies
View Related
Oct 28, 2013
I have a class as below:
// RemoteControlMonitor.H
typedef void (*keyaction)(unsigned int key);
class RemoteControlMonitor {
private:
keyaction rph;
keyaction rrh;
[Code] .....
But I got compile error as below:
RemoteControlMonitor.H:58: invalid type `void *' for default argument to `void (*)(unsigned int)'
rcx1.C: In function `void __static_initialization_and_destruction_0(int, int)':
rcx1.C:54: ANSI C++ forbids implicit conversion from `void *' in default argument
What can I do?
View 1 Replies
View Related
Aug 23, 2013
Code:
#include <stdio.h>
void ASCII_to_EBCDIC( size_t, unsigned char *);
void EBCDIC_to_ASCII( size_t, unsigned char *);
void to_ASCII(unsigned char *);
void to_EBCDIC(unsigned char *);
/* conversion tables */
static unsigned char
[Code] ....
The above snippet is for a buffer/string, where as i want to pass file name as a parameter and want function to process the file line by line?
View 2 Replies
View Related
Apr 17, 2014
How I can delete a parameter in a function .
int *buildTrail(int antIndex, int start, double *pheromones) {
int *trail = new int[tabu];
bool *visited = new bool[tabu];
trail[0] = start;
visited[start] = true;
[Code] ....
If I comment all lines includes visited word , no exception occurs , Otherwise , exception throws.
Simply put , How can i delete visited parameter as long as its role has been finished?
.
.
.
delete visited ;
return trail;
View 4 Replies
View Related
Jan 16, 2014
Unless I'm missing something, it's now possible(ish)? A little concept is below, very rough around the edges. Still though, if this is valid by standard C++, why can't we have built-in support for float / double template parameters?
#include <iostream>
#include <tuple>
template<int Numerator, int Denominator>
struct Float {
constexpr static float value()
[Code] ....
View 7 Replies
View Related
Oct 14, 2014
I have run into a problem which is mostly just an annoyance. I need to know if i can have pass a derived class to a function which has the base class as its parameter. For example i have been creating a program but i have a function which needs to work for multiple classes all derived from the BaseObject class
Code :
class folder : public BaseObject
{}
class BaseObject
{void function(BaseObject B)}
how would i get the following to work:
function(folder)
View 3 Replies
View Related