C++ :: Set Member To Same Value In All Instances Of Templated Structure

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


ADVERTISEMENT

C :: Filling Structure Client / Storing Different Instances In Array

Mar 31, 2013

Here is what I have going on.

Code:

struct client {
char firstName[stnd];
char lastName[stnd];
long ID;
char email[stnd];
float funds;
float wager;
}

typedef client...I would like to have these fields filled with this function below and stored in an array... Basically Multiple users and this is my function for it, if I can get it to work proper -.- .... I don't get syntax errors but I do get warnings

Code:

void getct(client *cl, int *pclientCounter) {
char input[buff];
char *pinput = NULL;
int typef = 0;
int lengthf = 0;
}

[code]....

View 3 Replies View Related

C/C++ :: Templated Data Structure - Error LNK2019 / Unresolved External Symbol

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

C++ :: How To Use Structure Pointer Through A Structure Public Member Definition

Dec 7, 2014

Why doesn't this compile?

struct hi(){
void other();
}histructure;

void hi::other(){
std::cout << "Hi!" << std::endl;

[Code] ....

Makes no sense the structure is written before the structure member function is called so why is there compile errors ??...

View 3 Replies View Related

C/C++ :: Value Assignment To Structure Member Inside The Structure?

Oct 7, 2014

Is it possible to assign a value to structure member inside the structure.like.....

struct control{  
char tbi:2 =0;
char res:1 =0;
};

View 6 Replies View Related

C++ :: Define A Templated Class While Implementing Default Value On Templated Arguments?

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

C++ :: Why Is Structure Member Not Displaying

Jan 7, 2015

I have started working with structures so here's a side project from my text book. It's purpose is fairly simple; it asks for the sales of each quarter of the year from 4 different divisions and then calculates the average quarterly sales and total annual sales and finally displays all the data. My problem is that in the function "displayCompanyInfo" the statement

std::cout << "Division " << R.division_name << std::endl;
does not display the name of the division. With that in mind here is the code:
#include <iostream>
#include <string>
struct CompanyInfo
{

[Code]....

As you can see the last part of the output has statements that say "Division" however they do not say the name of the division afterwards. I don't understand why that is?

View 2 Replies View Related

C :: How To Assign String Hello To Structure Member

Feb 17, 2015

See the simple code below. The compiler gives message:

assigning to type "char[20]" from type "char *".

I've tried everything else that seems reasonable but none works. How can I assign string Hello to the structure member?

Code:
int main() {
struct strc1
{
char msg1[20];
char msg2[5];
}
talk;
talk.msg1 = "Hello";
}

View 5 Replies View Related

C++ :: Dynamic Table As A Structure Member?

Mar 24, 2014

is it possible to make something like that?

struct type_name
{
char Status[i];
string Status[j];
.
.
} object_names;

The problem is I dont know how many statuses my object will have. Is it possible to make it in an dynamic array?

View 3 Replies View Related

C/C++ :: Assigning Value From Struct Member To Outside Structure

Jun 20, 2013

I have the code bellow, and i print the data but cannot assign it to global variable.

struct frequency_data {
 char frequency[10];
 int32 frequenca_mesatare;
 int16 PWM_F;
 int32 PR2;

[Code] ....

View 5 Replies View Related

C :: Set Struct Member Variable For Structure Inside Def

Mar 12, 2014

This is with Linux gcc

Code:
typedef struct _a
{
int id;
} a;
typedef struct _b
{
a my_a;
my_a.id = 1; // error: expected specifier-qualifier-list before "my_a"
} b;

I get error: expected specifier-qualifier-list before "my_a"

I must set the id for the kind of struct created inside the struct def because main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different values I will cast to the correct one and know it's members by it's id. How do I ?

View 10 Replies View Related

C++ :: Pass Pointer To Member Function (polymorphic Structure)?

Jan 3, 2015

I am trying to create a callback system for input events in my game engine.

Here is a cut down version of the EventManager.h file

#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };

[Code].....

This works if the function pointer being passed to the event manager is not a member function.

I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.

For example, if I want to make a player object it would be something like

class PlayerObject : public Object{...};

Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before

This is how I intend to use this

class OtherScene : public Scene{
void p_pressed(void){
//pause
}

[Code].....

View 6 Replies View Related

C++ :: Composition And Member Access Inside Composed Structure

Jan 18, 2013

I have a question regarding composition and accessing members "deep" inside the composed structure. For example;

class A {
private:
int m_myInt;
public:
int myInt() const {return this->m_myInt;};
void myInt(int newInt) {this->m_myInt = newInt;};

[Code] ....

Now, from somwhere I have access to an object of type B where I want to update the A::m_myInt. How would you do this without "breaking" the whole purpose of private/public members?

B myB;
myB.m_a.myInt(3); // Not allowed, and not desireable

I thought about implementing access through functons kind of like;

A & B::a() {return this->m_a;};
myB.a().myInt(3);

but I'm worried that this exposes my B::m_a-object too much. This would allow

myB.a() = A();
, right?

The following is a more desireable way of acces, but doesn't work for updating;

A const & B::a() {return this->m_a;};
myB.a().myInt(3); //Disallowed? myInt(int) is non-const.

What about this? Is this a good way of doing it?

class A {
private:
int m_myInt;
public:
int myInt() const {return this->m_myInt;};

[Code] ....

I guess it works? It would lead to a lot of data shuffling in case of larger sub-components.I would really like to do the following without exposing my components so much:

B myB;
myB.a().myInt(3);

Can it be done?

View 11 Replies View Related

C++ ::  Calling All Instances Of Object?

May 28, 2013

So I am wondering how I would go about calling a function for all instances of an object. I tried googling it, but all I saw was solutions like making an array of pointers to the objects. What if I don't know how many objects there will be? Isn't there an easier way?

View 19 Replies View Related

C++ ::  how To Correctly Initialize Instances Of A Class

Feb 1, 2015

I am struggling to understand what the correct way to initialize instances of a class for object orient programming is.

If I am not mistaken, there are 2 methods to initialize an instance "instance_name" of CLASS1 by calling the constructor:

(1) CLASS1 instance_name(argument);
(2) CLASS1 instance_name=CLASS1(argument);

As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Why only method (2) will work inside another class? Or is there another fundamental mistake I am making?

(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)

#include <iostream>
using namespace std;
class CLASS1{

[Code].....

View 6 Replies View Related

C++ :: Class Instances To Text File

Mar 29, 2015

Code:
#include<iostream>
#include<conio.h>
#include<string.h>
#include<ostream>
#include<fstream>
#include<iomanip>
using namespace std;
class MathProblem {

[Code] ...

So my program is quite simple, have the user answer the answer to a question, and then compare with the correct answer. I needed to implement an inheritance of MathProblem in my second class aswell.

The program runs as I intend but I wish to have the data forwarded to a text file. After that I must read back to the command prompt the text file contents. It's fairly easy for simple statements but I don't understand how to forward all my data from my classes.

View 3 Replies View Related

C++ :: Search Specified File For All Instances Of Substring

Mar 24, 2012

I'm trying to search a specified file for all instances of substring "abbaa."

The professor is making us use a 6x3 array in order to understand finite state machines.

Well the code I wrote runs perfectly, but the case for '/n' doesn't catch. It just flows through without it.

Significant portion of code:

Code:
if(fin) {
fin >> inChar;
while(fin) {
switch(inChar) {
case 'a':
currentState = state[currentState][a];

[Code] .....

View 3 Replies View Related

C++ :: Static Const Class Instances?

Jun 17, 2012

i have this rather large class, which (in a way) somehow resembles a custom dialog control). This control is supposed to display data, which it does just fine. To do so, it maintains a

byte settings[10];

array, which holds information on how to display the data.

There are multiple ways to represent this custom set of data.In order to remain flexible in representing it, i thought of implementing some sort of DisplayProvider, which can be registered to the base class and provides that settings byte array.

Preferably, i would now have a set of static const instances of this provider.Using a struct would work nicely here:

PHP Code:

struct DisplayProvider
{
int settings[10];
}
static const DisplayProvider prov1 = {1,2,3,4,5,6,7,8,9,0}; 

The problem: The DisplayProvider would have to do some pre-processing, before handing over control to the base class, which then does the main work.I would end up with something like this:

PHP Code:

class DispalyProvider
{
baseclass* owner;
 int settings[10];
void PreProcessing(...);//ends up calling the owner.Processing(...) function
}; 

The main thing here is, that i dont really see a way to create a stock of default "static const DisplayProvder = {...}"s, as i could when using a struct.

View 7 Replies View Related

C++ :: How To Compare Templated Class With Variadics

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

C++ :: Making Templated Class Of List?

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

C++ :: Determine If A Templated Class Has A Constructor

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

C :: Making Fork Call And Have The Instances Run Every Five Minutes

Dec 18, 2014

Understanding Fork() calls. I have to write a C program in UNIX machine, and make two Fork Calls Inside it and have these two instances run every five minutes all round the clock. How to achieve this.

View 1 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

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 View Related

C++ :: Passing Object With Templated Static Array Into Another

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

C++ :: Templated Function Using Ofstream Strange Characters

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







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