C++ :: Circular Dependency In Template Arguments

Nov 7, 2013

Today I faced a problem where I had circular dependency in my template arguments. I was trying to make a class hierarchy similar to:

template<class BType>
class A_base {
public:
BType* getB();
};

[Code] .....

Basically I had objects that were of type A<B<A<B<...

Basically I have a tree like structure of heterogeneous types that must facilitate two-way interactions where A's can call B's and B's can call A's. This structure is useful in many contexts the difference is the methods A and B provide are different in each of these contexts. Instead of adding the getA and getB and all the other connectivity methods in every version of A and every version of B, I wanted to create a base class that managed this automatically.

Another piece of advice was break up your code so there is a forward-only and backwards-only dependent types. This is not a complete solution because the two cannot know about the other and this does not really facilitate arbitrary two-way communication (where A calls B then B calls A back). It also makes the code more complicated in that I have two sets of objects and interfaces.

So the solution was to make the template arguments specific to the things I wanted to be flexible. The connectivity interface of A_base and B_base should be constant. Hence that cannot be in the template parameter. It was merely the traits that I wanted to make flexible so... I came up with this solution:

#include <iostream>
template<class aTraitType,class bTraitType>
class A;
template<class aTraitType,class bTraitType>
class B;

[Code] ....

Now this compiles and works great. The problem is that aObj and bObj cannot call their opposite within a trait method because print() does not know anything about the connectivity. So the solution there was to make traits an abstract base class. Then magically everything works!

#include <iostream>
template<class aTraitType,class bTraitType>
class A_base;
template<class aTraitType,class bTraitType>
class B_base;

[Code] .....

So this outputs the following. Clearly there is two-way communication!

Class A is not connected to B
Class B is not connected to A
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty

View 6 Replies


ADVERTISEMENT

C/C++ :: How To Remove Circular Dependency

Jan 23, 2014

I have ran into some sort of Circular dependency between two classes.

Forward declaration doesn't work as I'm allocating the memory for pointer array of Student and it requires the default constructor.

P.S I'm aware I haven't written the BIG 3, it's an incomplete code. Just want to know how to resolve this dependency.

class Student;
class Course {
char *name;
Student *s[3];

[Code].....

View 14 Replies View Related

C++ :: Doubly Linked List - Circular Dependency Error

May 15, 2013

When compiling the source I get the following error

"pms_program.h:54:56: error: expected declaration specifiers or ‘...’ before ‘CourseType’"

the code you are looking for in pms_course.h ( there is a circular dependency between pms.h , pms_course.h and pms_program.h)

function prototype AppendProgram ( part of doubly linked list )

I have attached the full course code.

As much as I like I cannot change the structure of the startup code ...

please see attached zip file for the entire source code.

View 6 Replies View Related

C++ ::  Writing A Function Template With Template Arguments?

Mar 14, 2014

I have a function:

template<class Iterator, class T>
void a(Iterator, Iterator, const T&);

and I want to be able to simplify calls to 'a' with calls like

a(someIteratableContainer);

instead of having to call:

a(someIteratableContainer.begin(), someIteratableContainer.end(), valueOfTheContainersElementType);

I also want to be able to generalize the function to handle any of the standard iteratable contains: array, vector, deque, whatever.

I was under the impression I could write:

template<template<class T> class U> a(U<T>& container) {
a(container.begin(), container.end(), g(T()));
}

where 'g()' returns an object of the element type. However, the compiler is claiming, no matter how I write a call to the overload, the original template is selected and/or the overload is invalid, depending on the various ways I attempt to write said overload.

View 7 Replies View Related

C++ :: Function With Template Arguments

May 26, 2013

I'm trying to create a callback wrapper for pointers to member functions of any class by using templates, std::function and std::bind. This will be used to send incoming sf::Event's to classes who register callbacks with an event manager. I based my code off of the example on this page: URL.....Here's what I have:

class EventCallback
{
std::function<bool(const sf::Event&)> func;

public:
template<typename T>
EventCallback(T* object, bool(T::*func)(const sf::Event&)) { func = std::bind(func, object, std::placeholders::_1); }
bool run(const sf::Event& evt) { return func(evt); }
};

[code]....

View 4 Replies View Related

C++ :: Enable If With Variadic Template Arguments

Sep 17, 2014

template <typename FIRST, typename... REST, typename std::enable_if<std::is_convertible<FIRST, Base*>::value>::type* = nullptr>
void foo (FIRST first, REST... rest) {}

that successfully allows me to enable the function foo() only if FIRST is convertible to Base*, but I also only want foo() enabled if each type in REST... meets the same condition. What is the syntax for that? If no such syntax exists, how to achieve that effect?

View 7 Replies View Related

C++ :: Expanding Variadic Template Arguments In Lambda

Feb 1, 2013

I'm trying to expand a template + argument parameter list inside a lambda function like this:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()

[Code] ....

But this does not work:

The compiler error is "error: parameter packs not expanded with ‘...’:|"

However, when I do the following:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {

[Code] ....

It works just fine. That shows that lambda threads are able to take variadic arguments...

So here is my question; what is the correct capture clause for capturing the variadic object correctly?

View 3 Replies View Related

C++ :: Getting Type Alias From Template Class Without Specifying Arguments And Instantiating It

Feb 4, 2015

For example I want to get the type of std::vector<>::size_type without specifying a template argument.

Here's what I want to do:

std::vector<>::size_type x{5};

Is there any way this can be done?

View 4 Replies View Related

C++ :: What Is Dependency In Makefile

Aug 7, 2014

What is dependency in Makefile.

Secondly I have made Makefile but cannot use make -f . How to use it?

Third what is make [OPTION] [TARGET]

View 4 Replies View Related

C++ :: Class Definitions Have Interwoven Dependency

Mar 29, 2013

I have an exercise from my text that defines a StrBlob class, then a StrBlobPtr class to hold weak pointers to the StrBlobs. This is from C++ Primer (5th Edition) and coincidentally, the entire chapter is available on-line at here.

My problem is that the begin and end functions of StrBlob can't be defined until the entire StrBlobPtr class is defined. Forward declarations don't cut it, since begin and end need more than pointers.

The solution (if you also look at the errata for the book) seems to be to define StrBlob, leave begin and end undefined, then full define StrBlobPtr, and following that, finally define StrBlob::begin() and StrBlob::end().

Anyhow, the above works, as I show in the included code below - but it seems like a hack and messy. What would be the proper way to do this? My text may be obfuscating the issue in the pursuit of pedagogy.

Additionally, how would one separate StrBlob and StrBlobPtr into there own headers? I'd think it impossible, since the StrBlob would have to nestle an "#include "StrBlobPtr.hpp" in the center of it's own definition...?

Code:
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <stdexcept>
class StrBlobPtr; // Forward declaration

[Code]....

As I said, the above works (compiles, haven't -tested- it extensively) but it seems messy.

View 2 Replies View Related

C# :: Breaking Dependency In Static Method For Unit Test?

Feb 6, 2014

I have function that returns historical data. I can access it, using file name. If I use file name, it reads that file and saves it to dictionary, so that in the future, if historical data is required for the same file, it does not read it again (it's lazy loading). If no file is supplied to the function, it tries to read file which is given in app settings.

However, for unit testing, I do not want to read any file. Instead, I want it to use small sample of hardcoded historical data. In order to do that, I think, I need to introduce interface to it. Then I can use some IoC to choose between different implementation for unit testing purpose and ordinary launch of application.

Function to get history is given as follows:

public static class Auxiliary
{
private static Dictionary<string, MyData> _myData;
public static MyData GetData(string fileName = null)
{
// ...
}
}

I have created default Unit Test project with Visual Studio so, as far as I know, by default it uses MSTest as test runner and MSUnit as unit testing framework but it does not have any IoC container so I should manage NuGet packages for solution and install Unity.

As far as I know, MSUnit (aka Moles) can unit test static methods (it's unconstrained isolation framework, like Typemock Isolator, unlike NUnit) but still many people suggest not to use any static methods for unit testing.

Should I use shim or stub [URL] Stubs should be used for faking external dependencies and here it is not external library, but my own code.

View 1 Replies View Related

C++ :: Application That Has One Static Library Dependency - Linking Error

Feb 16, 2012

I am trying to build an application that has one static library dependency, however I am getting this error when linking:

1>ClCompile:
1> All outputs are up-to-date.
1>LINK : fatal error LNK1104: cannot open file 'TestWrapperLib.obj'

Why I might be getting that? I have the .lib in the depends line, and the directory where it is at in the include line.

View 1 Replies View Related

C++ ::  how To Declare Template Function Inside Template Class

Dec 5, 2013

I'm trying to implement a simple template array class, but when i came into the operator< i actually have to use a template :

my code is something like :

template<typename _Type, std::size_t _Size>
class array {
public :

[Code] ......

but i am having an error of shadows template param 'class _Type' is it w/ the name conflict between the array template parameter and the function template parameter ?

View 6 Replies View Related

C++ :: Use Of Class Template Requires Template Argument List

Nov 6, 2013

Error1error C2955: 'DoubleLinkedListInterface' : use of class template requires template argument listdoublelinkedlist.h10
Error2error C2244: 'DoubleLinkedList<T>::DoubleLinkedList' : unable to match function definition to an existing declaration doublelinkedlist.cpp7

Error3 .cpperror C2244: 'DoubleLinkedList<T>::~DoubleLinkedList' : unable to match function definition to an existing declaration 12

.h

#pragma once
#include "DoubleLinkedListInterface.h"
#include "Node.h"
#include <iostream>

[Code]....

View 4 Replies View Related

C++ :: Circular Right Shift A Cstring

Jan 29, 2013

I need to circular right shift a cstring in C++

Let's say I have unsigned char test[10] = "HELLO!!!"; How would I go about circularly shifting this to the right? Inline assembly instructions would be ok too

View 6 Replies View Related

C++ :: Rotating A Circular Disk?

Aug 30, 2013

Any algorithm or function to rotate a displayed circle. To turn it 360 degrees like a car-tire. (It's needed to turn a turn-table in a model-railrod control program) .....

View 14 Replies View Related

C++ :: Circular Reference Not Working?

Mar 15, 2013

It's compiling but it's not working, it enters in stack overflow. It's a Doubly Linked List I'm compiling in Visual Studio. I think there's nothing wrong with this declaration, but there's just might be it:

class ListItem;
class List {
public:
ListItem *firstItemRef;

[Code] .....

View 7 Replies View Related

C/C++ :: Circular Permutation Through Function

Jan 17, 2015

I need to create such a function that the content of the first is put into the second, the content of the second into the third and the content of the third into the first.

For example, output should be like this
3
2
1
But the code below prints out:
1
2
2
Where am I making a mistake?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

[Code]....

View 2 Replies View Related

C# :: Circular Progress Bar In WPF (Not Silverlight)

Jul 30, 2014

Is there a way to make a circular progress bar in WPF without using another library or NuGet Package?

Like a tutorial on how to make it from scratch?

Right now, I'm using the existing ProgressBar UserControl but it looks horrible especially after I change the Foreground color from Green to DarkBlue.

View 6 Replies View Related

C++ :: Partial Template Specialization With Template Class

May 27, 2013

I have a generic template class with another template in one of its types. Now I want to specialize one of its methods for a particular (template) class, which leads to a compile error, however.

Here is the example:

#include <stdio.h>
template<typename Type>
class Obj1 {
public:
void ID() { printf("Object 1, size = %zu

[Code] .....

GCC ends with:
:35:27: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Type, template<class> class O> class Foo’
:35:27: error: expected a class template, got ‘Obj2<Type>’

What is wrong with the specialization? Can it even be achieved and how (if so)?

View 1 Replies View Related

C++ :: Template With A Specialized Template Class Parameter?

Nov 2, 2014

how I want the code to look. Only problem is it doesn't work (Line 11). I have some experience with templates but I'm not a pro.

Basically I want the "Channels<3>" to be a type that I can use to specify a Cable with similar to vector<float/int> it would be Cable<Channels<2 or 3>>.

What have I messed up with the syntax?

#include <iostream>
#include <vector>
using namespace std;

[Code].....

View 4 Replies View Related

C :: Circular Queue Based On Struct

Oct 6, 2013

I am stuck with how to make a circular queue that are based on a struct. Have been reading about the implementation but cant really understand it fully. Here is what i got so far.

Code:
#define SIZE 10
typedef struct {
char reg;
char brand;
int modelyear;
int mileage;

[Code] .....

View 8 Replies View Related

C++ :: Fixing Circular Dependencies With Pointers

Dec 18, 2012

I've been making a project that requires different files to have access to objects declared in other files such that circular dependencies are created. I've done some research and discovered that pointers and forward declarations should be able to fix this.

Example:

File 1 declares variable x, must edit x and y

File 2 must edit x and y, declares variable y

I know this isn't the best example, as you could probably declare x and y in the same file, but please suffice it to say that I'm unable to do that in my project.

View 10 Replies View Related

C++ :: Creating Circular Linked List

Jan 30, 2015

I've been making a circular linked list and I'm trying to assign each list a "name" and its next "link" but when I run my code it crashes.

Soldier *Head;
Head = NULL;
string names[] = {"Arman","Bogut","Castro","Damascus","Elene"};
for (int i = 0; i < 5; ++i) {

[Code] .....

View 4 Replies View Related

C/C++ :: Make Circular List And Print It

Apr 28, 2015

I have this program. I am trying to do this Circular List but i think something going wrong. The first of all is the list.The second is if my code for delete and select function are correct and the third i would like my program getting a "n" number of names and then make the circural list then print it and then when i select a number delete every node until give us the only one left.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define NUM_PER_LINE 6
typedef struct node {
char name[SIZE];
struct node * next;

[Code] .....

View 1 Replies View Related

C :: Access A FIFO (Circular Queue) Between Two Cores

Oct 31, 2014

I want to write a program where one core (core 0) will fill the FIFO and other core (core 1 ) will delete the data from FIFO.

Core 0:

I create a fifo

Code:
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

And my en-queue function is in core 0 and writing to specific memory location..(which works perfectly)

Code:
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;

[Code] ....

Now the problem is in the core 1. Here I am unable to read the values from the specific memory location. I am getting garbage value. Where I am doing some stupid error.. I did not understand

Code:
(front->ptr) = (unsigned int *) memory_location;

When I print the (front->ptr) it shows correct memory address but inside the De-queue function in core 1, I am getting wrong value..

Code:
int deq(int buf[n]) {
front1 = front;
printf("Val %d ", front->info); // showing wrong value
if (front1 == NULL) {
printf("

Error: Trying to display elements from empty queue");
return 0;

[Code] ...

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved