C++ :: Thread Safe Class - Queue Of Objects

May 7, 2013

I am new to C++ programming and i have a small assignment.

How to implement a thread safe class to manage a queue of objects. Accessing the front of the queue should be a blocking operation when the queue is empty.

View 4 Replies


ADVERTISEMENT

C++ :: Creating Priority Queue Of Objects Transaction As A Variable Of Store Class

Dec 4, 2013

I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class.

My store.h
#ifndef __STORE_H_INCLUDED__
#define __STORE_H_INCLUDED__
#include <queue>
using namespace std;
#include "Transaction.h"
struct TransactionCompare;

[Code] ....

The error im getting with this set up is

error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &)
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'

View 6 Replies View Related

C++ ::  Class Safe Array Type Print Function Not Working When Called

Oct 22, 2013

I've created a function where you can choose any bounds for an array based list (positive or negative, as long as the first position is smaller than the last position). However for some reason when I call the print() function in my application program it doesn't do anything. My print function is technically correct (I still have work to do on the output) but I can't figure out why it wont show anything at all. Below is my header, implementation, and main program files, along with results from running the program.

Header File:

//safearrayType Header File
class safeArrayType {
public:
void print() const;
void insertAt(int location, const int& insertItem);
safeArrayType(int firstPlace=0, int maxPlace = 100);

[Code] ....

ResultsEnter the first bound of yourlist: -3(this was my input)
Enter the last bound of yourlist: 7(this was my input)

Press any key to continue...

View 1 Replies View Related

C++ :: Access Objects From Another Thread

Jun 25, 2013

I have the following code below. I am getting a memory access violation when accessing cmd->query_string in the loop function. The loop() function is running in another thread. The cmd object appears to be destroyed after calling the send_command function. How do I create an object on the heap and access the query_string.

std::list<my_command_message_que*> my_command_que;
void loop(){
if(my_command_que.size() > 0){
my_command_message_que * cmd = my_command_que.front();
std::cout << cmd->query_string;

[Code] ....

View 2 Replies View Related

C++ :: Create A Thread To A Class Function

Jan 25, 2014

I'm trying to create a thread to a class function, but it doesnt work for me...

The function : void Server::RecMsg(SOCKET &socket)

the call : std::thread GetMessages(&Server::RecMsg, ClientSocket);

I get the error : "Error1error C2064: term does not evaluate to a function taking 1 arguments"

View 1 Replies View Related

C++ :: Header File - Running Class Function As A Thread

Apr 20, 2014

I have a header file with a bunch of functions. I want to create a thread using the <threads> package and am using

void myclass::functionX() {
}
void myclass::function() {
std::thread tr(myclass::functionX);
}

I am getting the error "no instance of std::thread::thread matches the argument list argument types are (void());

View 1 Replies View Related

C++ :: Increase Sizes Of Queue In A Vector Of Queues And Find Shortest Queue

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

C# :: One Thread Trying To Pass Data To Another Thread Using Serial Port

Jul 30, 2014

I have a class which I wrote and one of its object is "SerialPort" .NET class. In my MainWindow I created instance of my class called "SerialPortComm", then I send through some functions of mine, commands to the Serial Port, and I receive answers through "DataReceived" event.

But when I trying to use Dispatcher.BeginInvoke to write my data I have received (successfully), nothing shows on the RichTextBox which I'm trying to write to.

What can caused that, and How I can make it works?

SerialPortComm.cs

public partial class SerialPortComm : UserControl {
public SerialPort mySerialPort = new SerialPort();
public void Open_Port(string comNumber, int baudRate) {
mySerialPort.PortName = comNumber;
mySerialPort.BaudRate = baudRate;

[Code] ....

View 7 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,

Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;

[Code] ....

This sample code does exactly what I want. So does the author mean the way I did is undefined?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

Let's say I have a Car object , and it contains inner Engine object.

Code:
struct Car{
Engine mEngine;
};

In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}

Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?

Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};

The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .

View 5 Replies View Related

C++ :: Creating A Class That Would Implement Concept Of Double Linked List Like A Queue

Jun 15, 2013

Well, basically, what I've been doing was creating a class that would implement the concept of Double Linked List, but that would behave like a queue ( the STL implementation is deque, or Double Ended Queue ).

The problem occured when I have been trying to generalize the class using templates. I mean, why use only integers ? Why not double or char or what not ?

Worked perfectly before including all the template stuff..

// Source.cpp <=> Main.cpp
#include <iostream>
#include "DList.h"
using namespace std;
int main(void) {
DList<int> *list;
list = new DList<int>();

[Code] .....

The errors returned by the compiler:

Error1error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
Error2error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6

View 6 Replies View Related

C++ :: How To Use Class With Array Of Objects

Oct 12, 2014

class FlashCard {
public:
int a,b;
void PrintCard(void);
int CorrectAnswer(void);
};
void SwapFlashCard(FlashCard &a,FlashCard &b)

[Code]....

Why does Xode warn me that Type'FlashCard'does not provide a subscript operator on line 22,23,29 and 30?

View 7 Replies View Related

C++ :: Add Three Objects Of A Class Matrix

Apr 9, 2014

I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.

Matrix obj1, obj2, obj3, obj4
Obj1=obj2+obj3+obj4

I am using the following code for this

Matrix Matrix::operator + (Matrix &obj) {
if((this->Columns == obj.Columns) && (this->Rows == obj.Rows)) {
for(int i=0;i<obj.Rows;i++)

[Code] .....

the problem is that it is just adding only 2 operands. How can I add more?

View 2 Replies View Related

C++ :: Array Of Class Objects

May 13, 2014

I need an array of class objects but am unsure of how one might accomplish this. I have so far...

//element class driver code
Element Arsenic(lowCeiling, highCeiling);
Element Cadmium(lowCeiling, highCeiling);
Element Chromium(lowCeiling, highCeiling);
Element Copper(lowCeiling, highCeiling);
Element Lead(lowCeiling, highCeiling);
Element Nickel(lowCeiling, highCeiling);
Element Molybdenum(lowCeiling, highCeiling);
Element Mercury(lowCeiling, highCeiling);
Element Selenium(lowCeiling, highCeiling);
Element Zinc(lowCeiling, highCeiling);

Element metal[10] = {Arsenic, Cadmium, Chromium, Copper, Lead, Nickel, Molybdenum, Mercury, Selenium, Zinc};

Could the array be created in this manner or is that illegal code?

View 2 Replies View Related

C++ :: Class Objects In A Vector?

Mar 16, 2013

So I'm trying to store class objects in a vector:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

[Code]....

1. Am I storing it correctly?
2. How would I access the stored data, say, if I wanted to compare it to other stored data or COUT it?

View 1 Replies View Related

C++ :: Class Admin - Array Of Objects

Jun 26, 2013

What I want to do is have an admin class which will hold all the employee objects, can add them, list and calculate salaries. I'm trying to make array of objects, not sure if it's right

here is the code

Code: #include <iostream>
#include <string>
using namespace std;

class Employee {
public:
Employee(string name, short type, int salary)

[Code] .....

View 2 Replies View Related

C++ :: How To Loop Through A Vector Of Class Objects

Mar 22, 2013

For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?

Here's some snippets of the relevant code.

class Employee {
....
virtual void printPay() = 0;
};
class HourlyEmployee : public Employee {

[Code] ....

View 4 Replies View Related

C++ :: How Class Objects Stored In Memory

Aug 23, 2014

For example when I have:

Class A{
B objectB;
};

Now when I instantiate the object of class A like:

main(){
A objectA;//option 1
A* pObjectA = new A();// option2
}

How is objectB stored in memory (stack heap etc.) for both options??

View 1 Replies View Related

C++ :: Cout And Cin Objects Of Iostream Class

Feb 17, 2013

i ran the following code in the latest version of code::blocks and it tells me that the objects cout and cin are not declared in this scope. what is the problem?

I used to use Turbo C++ 3.0 and i had no problem whatsoever with that compiler. But now i am trying to move to code::blocks but it is proving very very hard as all the standards have been changed.

I am a school student and thus, we had been told to practice on Turbo C++ 3.0 and now i am unable to unlearn it. Also, if i use printf in place of cout there is no error but i want to use cout as it is what i am comfortable working with.

#include<fstream>
#include<conio.h>
int main() {
using namespace std;
char name[20];

[Code] ....

Is there some document to which i can refer so as to get the latest C++ standards which is C++0x i believe?

View 7 Replies View Related

C++ :: Limit In Number Of Objects Of Class?

Jul 8, 2013

Is dere is any limit in number of objects of a class?

View 16 Replies View Related

C++ :: Use Array To Store Class Objects?

Feb 11, 2013

So starting with the Item.h file :

#ifndef ITEMH
#define ITEMH
using namespace std;
class Item

[Code]...

I think I'm getting the wrong idea about strings. I thought I could add data just by using '+=' but apparently not.

View 4 Replies View Related

C++ :: How To Make Array Of Objects For Class

Aug 18, 2013

class A {
Public:
A (int);
A(int,int);
int get (int,int);
};

View 5 Replies View Related

C++ :: Create Objects From Abstract Class

Oct 13, 2013

I am working on a project that requires me to create objects from a abstract class that has 2 child classes (that need to be derived). Any examples on how to do this? I looked online and the examples were pretty vague. the main error that I am getting is when I make a temp object with & in front of it (such as Employee &genericEmp) it throws a must be initialized error.

View 6 Replies View Related

C++ :: How To Compare Values Of Two Objects Of Same Class

May 27, 2013

Running into a snag in my program. I can't seem to figure out how to have an object of a class be able to look at all the other objects of its own class.

Reasoning being, I'm working on a game with multiple ships flying around in the same space. Each ship is a class.
Each ship has an x and a y, and needs to compare the angle and distance of other ships' x and y coordinates to see if they're visible on the same screen.

How to tell an object to look at objects of its own class.

Here's some code:

common.h

#ifndef COMMON_H_INCLUDED
#define COMMON_H_INCLUDED
int dist(int x1, int y1, int x2, int y2);
int get_info(int which);

[Code] ....

Basically, I just don't know how to properly write a function, call, or how to get the info I need, to the Player::get_closest() function so that it can see the other play objects.

View 7 Replies View Related

C++ :: Dynamically Create Class Objects?

Nov 3, 2013

regarding dynamic vector.

Shape2DLink.cpp
void Shape2DLink::InputSensor()
{
string shape,type;

[Code]....

So i'm actually using polyphormism for calculating the area of the cross and other shapes. so how do I actually make use of dynamically create an object this way?

do I create them in my Shape2DLink or in my individual child classes?

View 9 Replies View Related







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