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
ADVERTISEMENT
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
Feb 20, 2013
I have a paradigm in a loop of queues of a vector,if a condition is true,increase sizes of the queue of that particular queue in the loop of queues, if condition is false, the queuesize is left as such in loop of queues. After this operation i need to search the queue sizes of all queues and enqueue in the shortest queue.
I want to do something like the code given below
#include <vector>
#include <queue>
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();
[Code] ....
How to implement this logic?
will q[i].size=q[i].size+5 increase the queuesize by 5 for the ith queue?
View 12 Replies
View Related
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
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
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
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
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
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
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
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
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
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
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
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
Mar 16, 2013
will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?
View 10 Replies
View Related
Jul 3, 2014
I have a method to take a Tile object and make an instances of it based on some data from the original object. Than it is suppose to manipulate the a specific instance and save the results. The first loop through it works but it changes all instance as well as the base.
public static int recurse(int count, Tile[,] b,Huristic h,int check) {
if (check==1) {
boardState.Add(B)/>;
return check;
} if (check == 0)
[Code] .....
View 6 Replies
View Related
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
Sep 11, 2014
a function returns a temporary object like
int myfun(){
int x = 0;
return x;
}
this function will return a temporary integer now void fun1(const int & num); this function can receive from myfun().BUT void fun2(int & num); this function cannot receive from myfun() Why is that, Moreover what is lifetime of a temporary object like one returned in myfun() ???
View 7 Replies
View Related
Mar 28, 2014
I am trying to use web api in order to return custom json response keys. For this i have return a custom class to return json response
Custom Class:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
[Code].....
View 2 Replies
View Related
May 4, 2013
I have a combobox with Items 1024
2048
4096
8192
String cach = form.comboCache.SelectedItem.ToString();
I am using the above code to retrive an item selected by user,But this line is giving an exception "Null Reference Exception, Object reference not set to an instance of an object"
View 1 Replies
View Related
Nov 28, 2014
How to build a FiFO queue without using the STL (done that no problem), get it to dequeue (again, done that no problem). However, to get those extra marks, I need to be able to order it using a priority system.
I've tried ordering the NodeDequeue class that I'll show at the bottom of this post, but I just cannot get it to order appropriately. The closest I have got is everything in order but I lose a Node from the memory completely. So, that's no good.
The most logical idea I have thought of right now is to send the largest number to the back of the queue each time it's iterated, eventually, the largest number will end up at the front.
class PriorityQueue : public Queue {
public:
Node* NodeDequeue(void) {
Node* tmp = front;
Node* seek = tmp->getPrev();
[Code] .....
View 3 Replies
View Related
Aug 17, 2014
I'm having a problem with removing an item from a queue. At first in the debugger I got SIGTRAP but I don't get it anymore but the problem still exists. When you try to remove an item from the queue the first nothing happens. Here's the code below compile it and you see what I'm talking about.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char let;
struct Node *nextNode;
};
[code]....
View 12 Replies
View Related
Mar 12, 2014
I have an assignment that needs to display the names of customers to be served according to a sequence. coding to display the names accordingly?
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 20
void create();
[Code].....
This is my output. I have trouble displaying the names of the customers as it outputs null when I try to display my position in queue.
View 4 Replies
View Related
Apr 11, 2013
Why is this code not marking that the queue is full.. I'm just including my Add function that verifies if they want to add another number to the queue. The isFull function works fine, have used it on other programs.
template <class T> //Template currSize function
int Queue<T> :: currSize ()
{
return (rear - front + arraylength) % arraylength; //
[Code].....
The output goes all the way down to 1 spot left, lets the user enter the last element. Then it asks if they want to add another.. At this point, when they hit Y, it lets them add it and it says there are 5 spots left!
View 8 Replies
View Related
Jan 19, 2013
I'm trying to implement Prim's Algorithm and for that I need to have a decreaseKey method for a priority queue (to update the key value in a priority queue). Can I implement this in the STL Priority Queue?
This is the algorithm I'm following:
for each vertex u in graph G
set key of u to INFINITY
set parent of u to NIL
set key of source vertex to 0
en-queue to priority queue Q all vertices in graph
[Code] .....
View 1 Replies
View Related