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


ADVERTISEMENT

C++ :: Implementing Static Templated Method In Cpp File

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

C++ :: Using Parametered / Templated Comparison Object For Priority Queue

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

C++ :: Getting A Value From Object Passing It To Array

Apr 17, 2014

I am writing a program for a poker game.. I created a class to get cards and create deck.. the problem I am having now is how to deal 5 cards to an array to ve evaluated later. I want to call the array player1[5]. I tried to use pointer but I get the following error

#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const char* FaceString[13] = {"Ace", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};

[code]....

View 4 Replies View Related

C++ :: Passing Array Of Object Gives Compilation ERROR

Dec 21, 2012

This is my question : Define a class named HOUSING in C++ with the following descriptions:

Private members
REG_NO integer(Ranges 10 - 1000)
NAME Array of characters(String)
TYPE Character
COST Float

Public Members
-Function Read_Data( ) to read an object of HOUSING type
-Function Display() to display the details of an object
-Function Draw Nos( ) to choose and display the details of 2 houses selected randomly from an array of 10 objects of type HOUSING Use random function to generate the registration nos. to match with REGNO from the array.

Now I' trying to do this by this way

Code:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class housing {
private:
int REG_NO;
char NAME[10];

[Code] .....

I am trying to pass the entire array of object in DrawNos(). but getting compilation error -

32: 'housing:rawNos(housing * *)' is not a member of 'housing'
48: Structure required on left side of . or .*

What is the problem? How can I pass the array of object in function and use it.

View 3 Replies View Related

C++ :: Importance Of Static Object In A Class And How They Are Different From General Object

Dec 13, 2012

#include "B.h"
class A {
public :
A()
{
s_b = new B();
b = new B();

[Code] ....

In my project i have seen static object as above . But not able to know what is the exact use of it and how they are different from general object .

View 2 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++ :: Static Object Of Same Class

Jul 27, 2013

What is the purpose of having static object of the same class.

E.g.

class someObj{
public:
static someObj obj;
};

how the compiler treats this object

View 3 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++ :: Static Member Allocation - Global Object Creation

Jan 24, 2014

I see many time where static data member is used to count creations of objects -

i.e.

1. the static data member is init to 0

2. the static data member is incremented by 1, in the Class' constructor, every time an object is created

However, if you define a global object of a class,

How can you tell that the static data member is initialized BEFORE the constructor of the global object is called? (i.e. before the global object is created).

Because to my understanding, you do not know in advance the order of global objects' creation -

so the Global Object could be created BEFORE the static data member was created and initialized.

View 14 Replies View Related

C++ :: Non-static Member Reference Must Be Made Relative To A Specific Object

Mar 4, 2012

Code:
class A
{
std::map<std::string, Unit*> aMap;
class B

[Code] .....

This code snippet results in "A non-static member reference must be made relative to a specific object". When I make callA() static, this error goes away, but there is problem with aMap.

View 2 Replies View Related

C++ :: Passing Object By To Queue

Dec 4, 2013

I've been working on a little project and hit a snag. I'm using nodes for a queue and stack class that were created using an existing list node class. I create an object for a student class and I want to enqueue that object.

int main() {
Queue sLine;
Customer stu;
Queue<Student &>
cLine.enqueue(cust);
}

That's basically the coding of it in main. However when I follow the error which says uninitialized reference member ListNode<Student& info>::data;

#ifndef LISTND_H
#define LISTND_H
template< class T > class List;
template< class NODETYPE >
class ListNode {
friend class List< NODETYPE >;

[Code] .....

What I may have been doing wrong? Trying to work within certain contexts.

View 2 Replies View Related

C++ :: Passing Object Into A Function

Feb 13, 2013

This is supposed to check for valid input

if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0)) {
c.integer = 0;
c.numerator = 0;
c.denominator = 0;
}
if((c.integer>0)&&(c.numerator<0))

[Code] ....
And it does just fine, until it's passed into a function, then it doesn't work:

void validInput(Mixed a) {
if(((a.integer < 0)&&(a.numerator < 0))||(a.denominator<=0)) {
a.integer = 0;
a.numerator = 0;
a.denominator = 0;

[Code] ....

It works, when the object is passed, except for two cases (one where the minus sign shifts) and whenever there is a zero or a negative integer in the denominator.

Also, I'm passing the function like validInput(c);

View 4 Replies View Related

C++ :: Passing Ifstream Object Between Functions

Mar 29, 2013

Code:
void lexer(ifstream& inputfile) {
string line;
getline(inputfile,line);

[Code] ......

I am trying to pass input file between two functions. The code compiles but immediately upon running the program, there is a "bad cast" run time error.

View 9 Replies View Related

C/C++ :: Passing A Stream Object By Reference?

May 5, 2014

I need to create a function in my program to open an input file and another function to open an output file and I need to use the files in other functions so Im trying to pass the stream object by reference but then i need a condition that tells the compiler not to reopen the file because then it will delete everything and make me input the file names again. Heres the two functions.

void InputFileOpen(ifstream &inFile) {
string file_name;
if(!inFile.is_open()){
cout<< "Enter the input file name: ";
cin>> file_name;
inFile.open(file_name, ios::in);

[code]....

View 4 Replies View Related

C++ :: Error Passing Object Reference

Nov 23, 2013

Code:

#include <Data.h>
int main(int arg_count, char** argv) {
if(arg_count!=3){
cerr<<"Files misisng
";
exit(1);

[code]...

This actually should work, because it is passing address of polymorphisms object.I have tried changing prototype of test in Data.h, but failed.passing object address/pointers in C++.

View 5 Replies View Related

C++ :: Passing Object By Pointer To Function?

May 20, 2014

I have a Qt classes as follow:

Code:

class Vehicle {
public:
void AddData(QString str, Data* data) {
_myDataMap.insert(str,data);
} virtual void Init();

[code].....

My questions are:

After the main function called d1->Modify; the data stored in _myDataMap will get modified too.

What is the more appropriate way of passing the Data through AddData in such case?

If i do AddData(const Data & data), i will not be able to use inheritance of Data, i.e passing a subclass of Data to AddData.

View 3 Replies View Related

C++ :: Passing Fstream Object To Function

Jul 6, 2013

I want my function to take 'fstream' object as an input, so the program looks like this:

Code:
#include <fstream>
using namespace std;
void test(fstream a){
a.open("test2.txt");
a << "123" << endl

[Code] ....

But I get error: 'std::ios_base::ios_base(const std::ios_base&)' is private|

View 3 Replies View Related

C++ :: Ifstream Object Passing To Functions

Feb 29, 2012

I am having an issue with passing an ifstream object to functions. Here is the code:

Code:
#include <fstream>
using namespace std;
void otherfunction (ifstream *ifs) {
...does stuff, like ifs->open(), then reads from the file...
}

int main () {
ifstream ifs();
otherfunction(&ifs);
}

Here is the error message:

Code: error: cannot convert ‘std::ifstream (*)()’ to ‘std::ifstream*’ for argument ‘1’ to ‘void otherfunction(std::ifstream*)’

Why can't I do that? What does "ifstream (*)()" even mean? And I don't want to change the structure of the program. I have reasons for declaring the ifstream object in the main function (because there are actually two functions that need access to the ifstream object -- neither of which is working).

Also, if I change the main function to be this instead:

Code:
int main () {
ifstream ifs();
ifstream *ifsptr = &ifs; //EDIT 2: forgot the ampersand
otherfunction(ifsptr);
}

I get the same error as above. However, if I change the main function to this:

Code:
int main () {
ifstream *ifsptr = new ifstream();
otherfunction(ifsptr);
}

I get all kinds of crazy errors about "undefined symbols for architecture _____". Here is the actual error message from my program (parseArgs is the real name of otherfunction)

Code:
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in cchumGBV.o
"std::ios_base::Init::~Init()", referenced from:

[Code] .....

View 12 Replies View Related

C++ :: Passing Object To Inherited Class By Constructor

May 1, 2013

I am trying to pass an object to an inherited clas by a constructor. I am having Cboard my board and i need to pass it to the object Cpawn.

Here under is my code:

main
Code:
#include<iostream>#include "Cboard.h"
#include "Cpawn.h"
#include "Cpiece.h"

void main(){
char location[50];

[Code] ....

View 3 Replies View Related

C# :: Passing Object As Parameter Creates A Copy Of It

Dec 17, 2014

Here is my issue: I am making a simple audioplayer in Xamarin.android but i want every time i change the track to make a crossfade effect. So im using 2 mediaplayers at the same time for the fade. The problem is that im defining one time the players and i pass the player as a parameter like this:

public MediaPlayer player = null;
public MediaPlayer player2 = null;
....

If i have to fadeout the player and start the next one im doing it like this:

if (player != null){
if (player.IsPlaying) {
cts = new CancellationTokenSource();
token = cts.Token;
FadeOut (player, 2000 ,token);

[Code] .....

So my problmem is that player and player2 remain always null. Why? i guess c# creates a copy of player and player2 and use this one. How i can pass a mediaplayer as parameter and always use only player and player2?

View 8 Replies View Related

C++ :: Why Can't Static Array Copy Values From Dynamic Array

Mar 13, 2013

But it can the other way around

Code:
static_Array= dynamic_Array;
dynamic_Array = static_Array;

The second statement works and i'm able to print out both arrays with equal values but with the first

[code] static_Array = dynamic_Array;I get incompatible types in assignment of 'int*' to 'int [7]' is the error I get [/code]

View 2 Replies View Related

C++ :: Do Static Functions Have Access To Non Static Data Members Of A Class

Apr 17, 2013

From my book:

"A static function might have this prototype:

static void Afunction(int n);

A static function can be called in relation to a particular object by a statement such as the following:

aBox.Afunction(10);

The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:

CBox::Afunction(10);

where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."

Why exactly cant Afunction access non-static members?

View 7 Replies View Related

C++ :: Accessing Non-static Members Inside Static Member Functions

Sep 11, 2013

What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?

View 7 Replies View Related

C++ :: Starting Vertex Array Object At Index Of Vertex Buffer Object

Sep 22, 2014

I'm trying to figure out how I can create a vertex array object at offset of a vertex buffer object.I've created the buffer object. I'd like the "texs" idnex data to start at the texture coordinate content of the vertex_t structure.

Type definitions:

struct vertex_t {
vector3d_t position;
float s; // Texture coordinate s
float t; // Texture coordinate t
};

Code so far:

// How do I make this start at a certain spot of the VBO?!
glVertexAttribPointer(texs, 2, GL_FLOAT, GL_FALSE, sizeof(vector3d_t), nullptr;
//...

View 1 Replies View Related

C# :: Static Method Inside Non-static Class

Aug 22, 2014

Have following code:

class Program
{
static void Main(string[] args)
{

[Code]....

My question according to what i just wrote:

1. Is that mean that Do() is only available for use by Dog itself because Dog is 'oryginal' Dog, and if i create new dogs - instances of oryginal Dog (dog1, dog2 ...) they cant access because Do is only available fo 'oryginal' one? Is that correct thinking?

2. If i would want to have something common (e.g value) for all dogs is that good way to create static field/method for Dog instead of non-static once then all instances of Dog would access Dog static member to get/change it? Just stupid example: static method GetAmountOfLegs() which return 4 Then all instances can take/call that value from Dog. Is that correct thinking?

View 2 Replies View Related







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