C++ :: Template Return Struct Fields

Dec 2, 2014

is it possible to define as template the following get functions.

class Config {
public:
enum { NO_ID = 999 };
struct ValueType {
bool a;
size_t b;
std::time_t c;

[code]....

View 6 Replies


ADVERTISEMENT

C :: How To Create Private Fields In A Struct

Nov 6, 2014

How can you actually create private fields in a C struct? I got the concept of VTable and inheriting methods aswell overriding them, but private variables can be obtained with static keyword in sourcefile. If you have variables in a struct, you can access them as soon as you got a ref to the struct. The functions are easy:

Code:

struct classA {
pointerToFunction *p;
} static void* thePrivateFoo() { }
void* publicFoo() {
thePrivateFoo(); /* or something like that */
} ...
/*in init code somewhere in the c file */ classAInstance->p = publicFoo;

But I was thinking about the variables... How is this achievable? I was thinking of a struct with only get/set functions and with no datamembers at all. All vars to be static outside the struct. But this kind of destroys the encapsulation.

View 4 Replies View Related

C++ :: Ioquake3 Engine - Accessing Limited Struct Fields

Feb 29, 2012

I'm currently working on the ioquake3 engine . The ioquake3 engine is separated into 2 different main threads at runtime: the gamecode and the engine. Both are communicating but not all information and my problem resides here.

In the gamecode, there's a struct called gentity_t which contains a lot of fields:

Code:
typedef struct gentity_s gentity_t;
struct gentity_s {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
// DO NOT MODIFY ANYTHING ABOVE THIS, THE SERVER
// EXPECTS THE FIELDS IN THAT ORDER!
//================================
struct gclient_s*client;// NULL if not a client

[Code] ....

This whole entity is passed to the engine at runtime, but only the first two fields are declared for the engine:

Code:
// the server looks at a sharedEntity, which is the start of the game's gentity_t structure
typedef struct {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
} sharedEntity_t;

My problem is that I need to access the health field of gentity_t from the engine. Technically, this is possible, but the health field is not declared in sharedEntity (which is the same memory address than gentity_t in the gamecode), so this is not straightforward.

I am looking for an elegant way to do this, and my constraint is that I must not edit the gamecode, only the engine.

The solutions I've thought:

- Just copy the whole gentity_t fields into sharedEntity_t. This would work I think but would be redundant, and I would like to avoid copying this huge set of fields.

- Include the two headers files declaring the gentity_t and sharedEntity_t structs, and create a Getter and a Setter functions that would cast a gentity_t over a sharedEntity_t and return/set a field. The problem is that I can't simply include them because they are both including some common headers files and this produce a recursive include error (and I can't modify the files to add a check, these are normally in the gamecode).

- Directly access the health field using a clever memory pointer, but I don't even know if that's possible given the huge number of fields prior health with many different types?

View 2 Replies View Related

C++ :: Input To Fields In A Struct And Pass Function To Display - Error C2365

Oct 21, 2014

Goal: Use a struct that has 4 fields, input to those fields, and pass to function to display.

Problem: (38) : error C2365: 'displaySData' : redefinition; previous definition was 'data variable'

Code:
#include <iostream>
#include <string>
using namespace std;
//prototypes
void displaySData(MovieData, MovieData);

[Code] .....

View 3 Replies View Related

C++ :: Template Function In Struct

Nov 6, 2014

I understand template functions and how to call them explicitly when no arguments are passed to them but can't seem to instantiate them explicitly when it resides within a class or struct.

struct Creator {
template <class T>
T * New(T * p) {
return new T;
}
};
Creator cr;
MyClass * pMy = cr.New<MyClass>(); //gives compile error

The compiler complains about MyClass (or any other) being used illegally in expression ...

View 6 Replies View Related

C++ :: Conditionally Return Different Types In Template

Apr 10, 2014

Is there a way to use preprocessing to conditionally return different types in a template. More specifically, is there a way to use preprocessing to block out conditional parts of code for one type verse another?

Example of what I can't get to compile for anything but POD types:

template <class T>returnTypeMatching(string inputName,string typeName) {
if(typeName="string") {
cerr<<"string types matched so returning string '"<< inputName <<"'"<<endl;
return inputName;

[Code] .....

If I switch string to long, the implied conversions let it work. I'm imagining there might be some preprocessing way to hide irrelevant conditions. I mean, this code would cause an error to return a string when T=double, so I get why there is a compiler error. However, is there a way to make that part of the code get hidden in that case when the template is processed when T=double?

View 11 Replies View Related

C/C++ :: Get Return Type Of Function In Template Parameter

Jul 27, 2012

Is this really the preferred way to get the return type, for use in a derived class, of a function defined in the template parameter?

template<class PARAMETER> class C {
         protected:
            typedef typeof (reinterpret_cast<PARAMETER*>(0))->function() returntype;
      };  

This works just fine for me, but seems inelegant.

View 12 Replies View Related

C :: Unable To Return Struct Node Without Using Pointers

Jul 20, 2014

I'm quite new to C and these days I have been playing around with a linked list. I managed to make a working version using pointers, ad only for the sake of learning I was trying to do the same thing just passing the "Object reference" Here is the method that apparently doesn't work..

Code:

struct Node addNode(struct Node head){
int value;
struct Node *n;
printf("Please enter the value
");
scanf("%d", &value);

[Code]...

when I return the function i have something like: head=addNode(head)

Unfortunately it does not work the way I aspect. I suppose that there is something I have left out..

Code:
like n->next=&head
// passing the address of the head at the next pointer of the struct
head =*n
//copy the values of the new node to the old head..

There must be something wrong with this line.. return head; What have I done wrong?

View 2 Replies View Related

C++ ::  Function Return Type Template Parameters Can't Be Deduced

Dec 9, 2013

I have this code:

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

[Code]...

and it does not compile.

The error is:

test.cpp: In function ‘int main()’:
test.cpp:20:30: error: no matching function for call to ‘func1(std::vector<int>&)’
test.cpp:20:30: note: candidate is:
test.cpp:8:45: note: template<class T, class U> std::map<T, T> func1(U)
test.cpp:8:45: note: template argument deduction/substitution failed:
test.cpp:20:30: note: couldn't deduce template parameter ‘T’

View 3 Replies View Related

C++ :: Template Method Based On The Type Passed Should Return A Value

Mar 8, 2014

I have a class where a method based on the type passed I should return a value.

prototype declared in the header file:

template <typename T>int getNum() const;

Code of the cpp file:

template <typename T> int class::getNum() const{
int c = 0;
for(int i=0;i<v.size();i++)
if(typeid(*(Pro*)v.at(i)) == typeid(T)) c++;
return c;
}

To invoke the method as I do:

ostream & operator << (ostream & os, Pro & obj) {
return os << obj.getNum();
}

View 4 Replies View Related

C :: Return Struct From DWORD (unsigned Int) Thread In Windows

Jul 12, 2013

In windows thread function not a void (as pthread in Linux).

Its type is DWORD(unsigned int) so my question is

How to return a struct (contain alot of different data types) from DWORD (unsigned int) thread in windows?

Code:
#include <windows.h>
#include <stdio.h>
#include <conio.h>

typedef struct {
char* name;
int id;

[Code] .......

View 12 Replies View Related

C++ :: Return Struct Pointer From A Void Function And Print

Mar 17, 2013

i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code

struct my_struct {
int x;
} void my_function( my_struct** result ) {
my_struct* x = new my_struct{ 10 };
//...
*result = x;
}

Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...

View 3 Replies View Related

C++ :: Using Set To Get Fields From Reader XML

Mar 28, 2013

I am using set in my code to get fields from a reader xml which are being errored out but it is storing it in ordered form(i.e it is internally sorting it) not in accordance to how the reader xml has the fields. What should i do so that it write the errored file in accordance to the reader xml.

View 1 Replies View Related

C++ :: How To Get Fields From Reader XML

Mar 28, 2013

I am using set in my code to get fields from a reader xml which are being errored out but it is storing it in ordered form(i.e it is internally sorting it) not in accordance to how the reader xml has the fields.What should i do so that it write the errored file in accordance to the reader xml.

View 8 Replies View Related

C++ :: Using STRTOK With Null Fields?

Jul 23, 2013

I am beginner in C++ programming. And i was try use STRTOK code with NULL fields, but in ARRAY fields NULL values is skiped. For example:

input text in Memo2:

AnsiString PAT02[100][20];
for (int IndexVRadku02 = 0; IndexVRadku02 < Memo2->Lines->Count ; IndexVRadku02++) {
AnsiString PNF02 = Memo2->Lines->Strings[IndexVRadku02];
char * PN02;

[Code] ....

Result:

Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6

But i need this result:

Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6

View 2 Replies View Related

C/C++ :: Why To Use Private Fields In A Class

Aug 9, 2014

Why do you use Private fields in a class if an outside class can change the private fields using get and set properties?

View 2 Replies View Related

C++ :: How To Write Functions To Access Various Fields

Sep 16, 2013

Question: How can we write functions to access various fields?

Example: A phone number -- it has an area code, exchange & subscriber -> (123)-456-7890

How can we write a function to access the various fields of a phone number both in a particular class & in the main?

View 5 Replies View Related

C++ :: Avoiding Construction Of Object Fields?

Sep 26, 2014

I have made the following code to illustrate my problem:

#pragma once
#include <bitset>
#include <iostream>

[Code].....

Now I want to avoid my bitset field being constructed before the ConstructTest constructor is called. So at first I tried wrapping it in a unique_ptr but found out that this would give me some potential problems with const functions.

And then I realized I could just set it to NULL, as I have in the above code. I tried that, got unexpected print outs, until I found out that NULL is just equal to 0 in C++ and that the bitset has a = operator that takes a number, int, long or maybe something else. Either way, this effectively constructs the bitset and sets it to the number 0.

So my efforts so far have been shut down. But then how can I avoid the bitset being constructed in advance, if at all possible?

View 5 Replies View Related

C# :: Read And Write 2D Arrays Into Fields?

Nov 17, 2014

reading and writing 2D arrays I've been trying out a few tutorials using FileStream but I couldn't get any of them to work.

Anyway what I'm trying to do is save the playerArray to a .txt file and then read from that .txt into the fields within the GUI. This is supposed to act as a database.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

[Code]....

View 2 Replies View Related

C++ :: Passing Value From Function To Fields Of A Structure?

Jun 22, 2013

Is there a way of passing values gotten from a function into a fields of a struct?

Lets say you got a function that returns sum and square of some numbers. How can I assign those values to the new made struct?

struct Struct
{
int sum_from_func;
int square_from_func;
};

View 1 Replies View Related

C++ :: Reading Configuration INI File To Read Custom Defined Fields

Jun 12, 2013

I’m developing an application where I have some defines, like:

#define PatientName “(0x0010, 0x0010)”
#define PatientId “(0x0010, 0x0020)”
#define Modality “(0x0010, 0x0030)”

And a few more (up to 3000). These defines are used to read certain fields from a DICOM image, that give information about that image (Patient Name, etc…). However I don’t wish to read all these fields. Instead I’m trying to load an .ini configuration which will configure which fields to read. For example:

[PROPERTIES]
# Fields to read
fields=PatienId,Modality

This would only retrieve the Patient Name and a Modality.

The problem is, the values I retrieve from the .ini file (reading and parsing the file with std::fstream) come as a string. How could I retrieve, for example, the defined value from PatientId, i.e. “(0x0010, 0x0020)”? Something like std::cout << # << "PatientId"; won't work.

View 3 Replies View Related

C# :: Method Of Mapping Text File Dictionary To Class Fields

Jan 21, 2014

So I'm rewriting an old project of mine, and I'm trying to determine if there's truly any better way to map the data taken from a text file "dictionary" into the correct class fields for further processing. For example:

FNAME=MY_FIRST_NAME
ADDR=123 SOMEWHERE LN, NOWHERESVILLE, TX 01234
LNAME=MY_LAST_NAME
TOTCALLS=47

In each of these, I'd need the "value" (MY_FIRST_NAME, MY_LAST_NAME, etc) from the "keys" (FNAME, LNAME, etc) to be mapped to the proper class fields. Say, for example, I had this:

Class DataProcessing {
public string Address;
public string FirstName;
public string LastName;
public int TotalCalls;
...
}

I would need DataProcessing.Address to be set to the value in the ADDR key/value pair. The same would be true for each other field. The problem is that based on the text file's source (which isn't under my control, and won't be changed anytime soon), the key/value pairs are not always in the same place...so a second file could have the data as such:

TOTCALLS=47
ADDR=123 SOMEWHERE LN, NOWHERESVILLE, TX 01234
LNAME=DARKPOETCC'S LAST NAME
FNAME=DARKPOETCC'S FIRST NAME

Any smarter way to do this than looping through each line that was read in from the file, and determining where it belongs, such as (pseudo code follows):

IF FieldName == "TOTCALLS" THEN
//Assign to TotalCalls field
ELSEIF FieldName == "ADDR" THEN
//Assign to Address field
ELSEIF FieldName == (You get the picture...)
//Do thing N_Field

View 9 Replies View Related

C/C++ :: How To Use Text Files As SQL Fields To Edit / Delete And Modify Records

Aug 19, 2013

I want to search and display the field stored in text file and also i want to delete the field that is entered. Case 3 and 4 is incomplete.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
    int a;
    int st_id;

[Code] ....

View 6 Replies View Related

C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?

Sep 14, 2014

#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;

[Code] .....

I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.

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







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