C++ :: Initializing Generic Type That Can Be Both Primitive And Object?

Apr 1, 2013

I have defined my own class, Queue, which inherits from my own class, LinkedList. I have been using templates to allow Queues to be of int, string, etc types.

But now I want to be able to store objects in my Queue type. And so the problem I have is that in my LinkedList class, I have two instances where I initialize an instance of my generic type T to 0.

For instance, the removeFirst() method starts like this:

template <typename T>
T LinkedList<T>::removeFirst() {
T a = 0;

And so the compiler complains that it can't convert from int to [in this case] Command&.

What to do?

View 2 Replies


ADVERTISEMENT

C/C++ :: Stack Class For Generic Type Data

Feb 4, 2014

I'm trying to implement my own Stack class, for a generic data type. Here is what I have so far, but I'm having problems compiling the code:

Stack.h:
#ifndef STACK_H
#define STACK_H
#include <vector>
template< typename T >
class Stack {

[Code] .....

View 5 Replies View Related

C/C++ :: Template Class Function Taking A Derived And Generic Object

Feb 23, 2014

I'm trying to write a function for receiving messages, so my classes can communicate with each other. The only issue I get is a compile error asking me to define the base parameter as one of the derived instances. I tried using a void* to fill the need, but then I lose the initial type, which I need to check for. How might I go about writing a generic object for this?

Here's my code:

template<class Object>
class State
{
public:

[Code].....

Should I just have all of the objects inherit in the order of Object >> GenericObject >> DerivedObject?

View 10 Replies View Related

C++ :: Generic Linked List Compilation Errors / Incompatible Pointer Type

May 19, 2014

I am trying to write a generic linked list in c, but for some reason i keep getting errors saying "incompatible pointer type. This is the code and erros:

#ifndef SLIST_H
#define SLIST_H
#include <stdlib.h>
typedef struct {
void *data;
slist_elem *next;

[code]....

View 2 Replies View Related

C++ :: Initializing Object Through Constructor

Oct 15, 2013

sth is in my mind.

fast way of initializing an object through constructor is

Fred::Fred() : x_(whatever) { }

slow way
Fred::Fred() { x_ = whatever; }

ok. i accept it is faster but why?? what is the difference compiler does.

View 6 Replies View Related

C++ :: Initializing Static Field As A Template Object

Aug 26, 2014

I need a static hash table to keep track of all objects of a particular type that are instantiated in a Qt application but I have never used a template class as a static member object before and I can't seem to figure out how to initialize it. QHash is the hash table class that follows the template convetion:

template<class key, class data>

QString is probably self explanatory.

Example Header:

class MyClass {
...
private:
static QHash<QString, MyClass*> instanceTable;
}

Here is my source that doesn't compile.

Example Source

#include header.h
// using default constructor for table...
QHash<QString key, MyClass* instance> MyClass::instanceTable(); // gives Error below.
// Error in above line is "Declaration is incompatible with QHash<QString, Myclass*>"

I have tried doing it a number of different ways and none of them work. How do you initialize a static template object?

View 2 Replies View Related

C++ :: Initializing Array Of String Type In A Class

Apr 28, 2013

I want to use this array as part of my class. I have tried several different angles trying to get it to work but with out success. I have been checking to see if it works by simply using "cout << dayName[3];" It is printing nothing at all. What is the proper way to initialize this array of strings?

First I tried this:
const string dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

then I tried this:
const string dayName[7];
dayName[0] = "Sunday";
dayName[1] = "Monday";
dayName[2] ="Tuesday";
dayName[3] ="Wednesday";
dayName[4] ="Thursday";
dayName[5] ="Friday";
dayName[6] ="Saturday";

My implemetation file code and my header file code is here (header first):

//dayType.h, the specification file for the class dayType
#include <iostream>
#include <string>
using namespace std;
class dayType{

[Code] .....

View 4 Replies View Related

C++ :: Global Char Type Array Initializing

Mar 20, 2015

I am not getting any error below

--------
#include <iostream>
using namespace std;
void main()
{
char sqr[3];
sqr[0] = '1';
sqr[1] = '1';
cout << sqr[0] << sqr[1];
while (1);
}------------

but if I move my char type array before main , I get error l

----------------------------
#include <iostream>
using namespace std;
char sqr[3];
sqr[0] = '1';
sqr[1] = '1';
void main()
{
cout << sqr[0] << sqr[1];
while (1);
}
-------------------

View 8 Replies View Related

C++ :: Initializing Static Map Of Variable Type Abstract Class?

Dec 3, 2014

A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:

//PARENT.H
class Parent {
protected:
static int n;
static std::map<int, Parent*> map;
public:
virtual void pureVirtual() = 0;

[code]....

The Problem:In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.I also can't initialize it as new Parent, seeing as the parent class is an abstract one.

The Question:Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option.

View 3 Replies View Related

C++ :: Initializing Base Class Part From Derived Object Using Copy Constructor

Feb 20, 2012

class Base
{
char * ptr;
public:
Base(){}
Base(char * str)

[code].....

Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.

Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.

I tried using initializer list but could not succeed.

Is there some proper way to do this?

View 4 Replies View Related

C++ :: Declare Template Type Object Inside Template Type Class Definition

Oct 12, 2013

Let me put it into the code snippet:

/**
This class build the singleton design pattern.
Here you have full control over construction and deconstruction of the object.
*/
template<class T>
class Singleton

[Code]....

I am getting error at the assertion points when i call to the class as follows:

osgOpenCL::Context *cxt = osgOpenCL::Singleton<osgOpenCL::Context>::getPtr();

I tried commenting assertion statements and then the debugger just exits at the point where getPtr() is called.

View 7 Replies View Related

C++ :: Initializing Object Of Class Inside Another Class?

Mar 7, 2014

I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock. Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.

class MySeqBuildBlockModule: public SeqBuildBlock {
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)// more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)

[code]....

I would have like to intiantiate an object "myIRns_3" of a class defined in third party library

SeqBuildBlockIRns myIRns_3(pSBBList2);

and would like to access it from the prep function as:

double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();

I tried to instantiate following in either private section or in constructor; but w/o any success:

SeqBuildBlockIRns myIRns_3(pSBBList2);

ERRORS encountered:

When I tried to do it inside the constructor, I get the following errors:

MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union

When I tried to do it in private section, I get the following errors:

MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union

View 5 Replies View Related

C++ :: Getting A Type From Object

Apr 20, 2013

it is not possible to get a type from an object, only from a type. Even with auto, you can only instantiate another type of the same type.I'd like to get a type from an object given another type, mainly because it is less typing and less error prone.

Some psudocode:

Type1 A<X, Y> a;
int b;
Type2 a.InnerType<b>::type c; // obviously won't work

A work around I thought of would be to use functions:

Type1 A<X, Y> a;
int b;
auto c = a.getInnerType(b);

Which is kinda interesting and looks like the way I'm going to go. However, I'm just wondering if there is any other ways of doing this as I don't really want to instantiate an object if I can avoid it (though the optimizer will probably just dump it anyway).

View 2 Replies View Related

C++ :: Derive Type From Object?

Aug 9, 2012

Suppose I'm wanting to define a predicate for some algorithm. I already need to provide the begin/end iterators of a container, can I somehow use the container instance to derive it's type that I can then use as parameters of the lambda predicate ?

Essentially... something along the line of:

Code:
// doesn't compile ! just used as an example
auto something = std::min_element( foo.begin(), foo.end(), [](typeof(foo)::const-reference left, typeof(foo)::const_reference right) {
left.calculateSomething() < right.calculateSomething();
});

Essentially, I don't care about what specific type is stored in foo. I just want a convenient way to get to the type without having to look up that the actual type is :

mynamespace::somebaseclass<templatevar1,templatevar2>::somenestedclass<templatevar1>::value_type

View 5 Replies View Related

C++ :: Using New On Primitive Data Types

Dec 12, 2014

int* count;
count = new int(1); // what???

Is this on the heap?? do i have to delete it now?

So is 'new' on a primitive data type just a way for me to allocate primitive data types (int, char, etc.) on the heap instead of the stack?

And, out of curiosity, can you do that in Java?

View 4 Replies View Related

C++ :: Checking For 3D Primitive Collisions

Aug 4, 2013

I think I may have found a new way of checking for 3d polygon collisions, but I'm not sure. The method involves...

1. finding the planes that the primitives lie on
2. finding the line where the planes intersect
3. if both polys have points on both sides of the line AND have points that overlap on the 1d space of the line, then they intersect.

I have some half done code testing this, and so far it seems to be sound and fairly fast. These are some average time-tests done on my machine for each part:

1. 30 microseconds (both)
2. 7 microseconds
3. TBD

View 5 Replies View Related

C++ :: Object Type Method Return

Apr 29, 2014

I am new to c++ and trying to learn. for instance. i have a struct and method.I am trying to learn what i can do with the method if i define the return type as struct type.

struct S {
int age;
string name;
};
S method() {
//what i can do in here. with the Struct. I mean can i reach members of the struct. etc
}

View 2 Replies View Related

C# :: Determinate Type Of Object And Pass It Through Method

Sep 18, 2014

I got base class called Statistic and inherited classes from it : lsp, cpu, memory. Those methods inheriting some of methods from Statistic and implement also theirself methods. I prepared some new class called ExecuteRequest which is taking Statistic object in the constructor. What i want to achieve is after i put e.g LSP to this class i would like to determinate what specific object it is in this case LSP right? Then having that i would like to put this object to RunRequest method. How can i achieve that? See my code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SOAP_testing {
public class ExecuteRequest

[Code] .....

View 14 Replies View Related

C++ :: Conversions Between Integer Primitive Data Types

May 8, 2014

In my platform (Windows 7 Ultimate 64 bits with Service Pack 1 over a compatible PC with a AMD x86 microprocessor), the next sample C++ code,

#include <iostream>
#include <limits>
using std::cout;
using std::endl;
using std::hex;
using std::showbase;
using std::numeric_limits;

[Code] ....

Compiled with Microsoft Visual C++ 2010 Express, prints this output:

ui = 0xffffffff
ull = 0xffffffff
sll = 0xffffffff
si = 0xffffffff
ull = 0xffffffffffffffff
sll = 0xffffffffffffffff

So, in my platform, conversion from an unsigend integer primitive data type to any bigger integer primitive data type never extends the most significant bit of the former integer and conversion from an signed integer primitive data type to any bigger integer primitive data type always extends the most significant bit of the former integer. This is convenient to mantain the same value when converting between integer primitive data types of the same signedness (i.e, signed integers or unsigned integers).

View 4 Replies View Related

C++ :: Object And Classes - Declaration Of Variable Using User Defined Type

Mar 17, 2013

I am getting a compilation error from the code below. It is when i am naming a variable with my user defined type.

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class person {

[Code] .....

C:Dev-CppTRIAL.PASS.!!!.cpp In function `int main()':
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected primary-expression before "p"
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected `;' before "p"
74 C:Dev-CppTRIAL.PASS.!!!.cpp `p' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
83 C:Dev-CppTRIAL.PASS.!!!.cpp `X' undeclared (first use this function)

View 4 Replies View Related

C++ :: Pass Double Pointer Of Object Into A Node Of Self Similar Type?

Nov 30, 2014

The following code compiles and runs fine till it reaches line 16 and gets a sigsev violation which I'm not sure about as to why. I have no problem passing the object of type node** into the constructor of base and storing it into the double pointer node** copy;; but when I call the function void pass(node** temp) it crashes.

#include <iostream>
class base;
class node {
private:
node** data;
public:

[Code] .....

View 3 Replies View Related

C++ :: Nested Classes - How Members Be Accessed Through Object Of Enclosing Class Type

May 18, 2013

"A nested class has free access to all the static members of the enclosing class. All the instance members can be accessed through an object of the enclosing class type, or a pointer or reference to an object."

How can the members be accessed through an object of the enclosing class type? I understand the pointer and reference part because for them you dont need the full definition, but for creating a object you do?

Also it has free access to all static members because the nested class is part of the enclosed class and with static it exists in everything inside the enclosing class? Right or am I missing something?

View 4 Replies View Related

C++ :: Threads Giving Error - Call Of Object Of A Class Type Without Appropriate Operator Or Conversion

Jan 27, 2015

I made a simple binary tree then decide to try out threads too. I got the following error:

call of an object of a class type without appropriate operator or conversion

Code:
#include "Tree.h"
#include <iostream>
#include <thread>
void main(void){

[Code] ....

I am having a hard time figuring out why the error exists. I tried adding the new operator but that did not work.

View 11 Replies View Related

C :: How To Implement A Generic Command

Jun 11, 2013

I have to make a prgrama using the C programming language that is able to read several lines of commands entered by the user and interpret it as a command to run.

I have to implement the following command:

a) Command generic - program should be able to read any one command and execute the same command on the operating system through primitives for implementing generic processes (eg "ls-l/etc").

View 1 Replies View Related

C++ :: Generic Class Set That Models The Set

Jan 26, 2013

Write a generic class Set that models the set.

Class Set should contain: appropriate constructors, destructor,

overloaded operators = (assignment), += (union), * = (cross-section),

the methods insert, remove, and print.

Write a class Combination which models the combination of numbers and includes object-Member: numbers
(use Set <int>), appropriate constructors, destructor, methods add, delete, print and merge (connects two combination; use overloaded operators of generic class Set).

Implement all methods.

Write a short test program.

View 5 Replies View Related

C++ :: Generic Shape Area Calculator

Dec 6, 2013

I'm building a pretty basic calculator program that calculates the area of generic shapes (triangles, rectangles, and squares); for some reason though, my program is having troubles as soon as it hits the if/else code in the int main section. When I enter triangle, rectangle, or square, it just spits back out the "That's not one of the options. Please re-enter and try again." error line I created. When I isolate and run just the stuff inside the if/else statements it works great, but why it won't just understand my if (shape == triangle).... .

Code:

#include <iostream>
using namespace std;
class figure {
protected:
double x, y;

[Code] ....

View 2 Replies View Related







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