C/C++ :: How To Use The List Container To Hold A Class

Dec 11, 2013

How would I use the list container to hold a class?

class A {
    private : int x;
    public  : void setX(int val) { x = val; }
};  

class B {
    private : std::list<A> pdata;
    public  : void addToList();
};  

For adding, I thought of trying something like

void B::addToList() {
   A *tmp = new A;  
   if(A != 0) {
      tmp->setX(5);
      pdata.insert(tmp);
      delete tmp;
   }
}

How would I do what I'm trying to do? Or is this the wrong way to go about it? For the actual program, "B" would contain several lists of various classes.

View 2 Replies


ADVERTISEMENT

C++ :: How To Hold Pointers / References To Abstract Class

Nov 15, 2014

I have an abstract class named Terrain, and a class named RoadMap, which supposed to hold an N*N array of Terrains. But I'm not sure what type should the RoadMap class hold:

Code:
#ifndef TERRAIN_H
#define TERRAIN_H
class Terrain {

[Code] ....

I can't use an array of refernces here, so I tried this:

Code: Terrain** terrain; and then I thought this was the way to go:

Code: Terrain (*terrain)[]; But now I'm not sure.

The N*N matrix size supposed to be determined according to a given input... What type should I use there?

View 2 Replies View Related

C++ :: Hold Vector Of Pointers In Same Class As Instances Of Those Objects?

Feb 10, 2015

Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this

class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level

[Code]....

When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?

View 2 Replies View Related

C++ :: Constructing Text Adventure - World Class To Hold Objects From Character

Jun 19, 2014

I am working with a new text adventure. The way i want to construct it is by having a class for all living things. in the class you have basic things as: health, gold, vector for inventory holding "struct item". etc...

There is also a class called world, wich navigates through the world.

World class contains of: player location, and a map containing info about the room etc...

Here comes the problem. I want there to be characters to be placed out in different maps, so basically i want the world class to hold objects from Character.

How to do it. In world class i made a map...

std::map<int,"content">

content is a struct i made above in world class:

struct content{
std::string name; // location name
std::string info; // info about location
std::vector<Character>characters;
std::vector<item>items;
};

To sum it up, i have a std::map<int,content>map the int stands for location id. Content holds more info about the room and what's in it

btw the classes are in different files and that means i have to include "Character.h" in the world file so i can set up the vector of characters.

View 2 Replies View Related

C/C++ :: Implementing Container Class That Holds Point Class

Nov 28, 2014

I have a Point class that's already implemented. My goal is to implement a container class called Line that holds the Point class. I'm not allowed to use any existing container classes for this. Here's what I'm working with:

//File: Point.h
#ifndef POINT_H_
#define POINT_H_
class Point {
public:
Point(int x = 0, int y = 0);
Point(const Point & t);
virtual ~Point() {};

[Code] ....

How I'm supposed to write Line.cpp...how do I access/add Points to Line without using something like a vector? I probably should've included what I've written so far.

#include "Line.h"
/**
* Default Line constructor
*/
Line::Line() {

[Code] ....

View 6 Replies View Related

C++ :: Design Own Container With Properties Of Both Vector And List

Dec 14, 2014

How can I write my own container which has properties of both vector and list.

E.g. I can access elements directly using [] operator like vector and behave like list in terms of memory (means we don't have to shift elements if we want to insert in between like list)....

View 1 Replies View Related

C++ :: What Is Container Class

Sep 22, 2013

What is container class? what its advantage ?

View 2 Replies View Related

C++ :: How To Create A Container Class

Feb 9, 2013

I know that it's a class that contains data from another class I just do not know how to make them both work together.

View 1 Replies View Related

C++ :: Best Std Container Class For Application?

Jan 16, 2012

I have a class called CTestObject and it has one variable int id. I want to add instances of this to an std container and be able to:

1. quickly determine if the value of "int id" exists in the container and update the object

or

2. add a new object to the container

Which std container is the best for this application? What method should I use to search the container for the object id?

View 14 Replies View Related

C++ :: How To Store Data From A Class Into A Container

Feb 10, 2013

I'm having trouble understanding on how to actually store into a container from another class. Any simple example.

View 9 Replies View Related

C++ :: Pass Object To Class Which Stores It In Container As Unique Pointer

Jul 24, 2013

class A (abstract)
class B : A
class C {
void add ( A(&*?) a )
std::vector<std::unique_ptr<A>> data; //unique_ptr<A> because A is abstract and therefore vector<A> isn't possible
}

upper situation. What is the best way to pass add an object of class B to C?

with C::add(A* a){ vector.push_back( unique_ptr<A>(a) ); }
and
int main() {
C c;
c.add( new B() );
}

This works, but i don't think it's very nice, because you could delete the pointer in main. What happens then with the unique_ptr? I could probably make C::add( std::unique_ptr<A> u_p ); but maybe it can be avoided that the "user" (in main() ) has to create the unique_ptr itself.

View 10 Replies View Related

C++ :: SDL - Check For A Key That Is Hold Down?

Sep 13, 2013

I'm using SDL to try to create a Run and Shoot game. But I do not know how to check if a key is down while the user is HOLDING it.

I do know how to check if a key was pressed.

I have tried with the "event.key.keysym.sym" and "Uint8 *keystate = GetKeyState(NULL)" both worked to check if a key was down but I thought that the GetKeyState(); Function would even check when a key where HELD down

I want my player to move while holding down left or right arrow. So I did something like:

Code:

Uint8 *keystate = GetKeyState(NULL);
if (keystate[SDLK_RIGHT]) {
apply_surface(x++, y, player, screen);
}

How to check if a key is held down?

View 2 Replies View Related

C++ :: Hold Screen Via GUI To Console

May 15, 2013

I had a program (on console) that uses a third-part software to draw some graphs. In order to hold the graphs on the screen, I used cin.get(); and that worked.

Now I created a GUI with Qt. The code remains generally the same. The code continues to call the software to draw graphs (during drawing graphs, there is a console opened automatically). Butcin.get(); in the code cannot hold the graphs on screen anymore. The graphs appear and disappear immediately.

View 4 Replies View Related

C++ :: Variable That Can Hold String And Int In Same Time?

Feb 12, 2014

Is there a variable that can hold string and int in the same time?

If not, what can I do if I want to input data with string and int like a password for an example.

View 3 Replies View Related

C++ :: Simple Way To Hold Numbers With Lot Of Decimals

Apr 8, 2013

I have a task to hold a number like 4.0000000000000000199e+30 and, in a variable like long double (the largest of the data type) doesn't hold the whole number, holds only 4.099e+30, like that.

Any way to hold the whole number?

View 1 Replies View Related

C++ :: Hold Group Of Variables In One Variable

Oct 4, 2014

So far i know that the pointer is address value to the real variable. Pointer size depends on operation system right?

for example
32bit systems: 4byte pointers
64bit systems: 8byte pointers
128bit systems: 16byte pointers?

Anyways, there have to be an variable type in c++ what can hold a pointer.

Let's imagine that the int is the thing im looking for

struct stc1 {
char *chars;
int ints;
}
struct stc2 {
char *chars;
float floats;

[Code]....

My wish is to hold different types of variable or groups of different type of variables in one variable.

I am developing program in windows now yet it will be used in linux and who knows with what x bit systems.

View 2 Replies View Related

C++ :: Create Structure To Hold Data For A Kennel

Apr 3, 2013

// This program creates a structure to hold data for a kennel

#include<iostream.h>
struct KennelList {
int dogID;
char gender;
int age;

[Code] ....

View 2 Replies View Related

C++ :: Declaration Of Variable To Hold A Table Of 5 Records

Aug 27, 2014

im doing a program to store name, age, time and fitness. and i need to hold a table of 5 such records.can i do this?

#include <iostream>
using namespace std;
int name1, age1, time1, fitness1;
int name2, age2, time2, fitness2;
int name3, age3, time3, fitness3;
int name4, age4, time4, fitness4;
int name5, age5, time5, fitness5;

[code].....

View 3 Replies View Related

C++ :: Template To Hold Two Dimensional Arrays - Automatic Indexing

Mar 6, 2015

For the past couple of weeks I have been working on a template to hold two-dimensional arrays. Right now I am puzzling over an indexing question.

There are many places in the template where I would like to use initializer_lists to refer to user-specified row and column ranges, particularly in member function arguments. A typical example would be a member function whose declaration would be along the lines of:

Code:
Array<Type>::some_function(std::initializer_list<int> columns, std::initializer_list<int> rows); which could get called via

Code:
arrayInstance.some_function({3:4}, {5:8});

It would be really nice to be able to use Matlab-style indexing to specify the last column, or the last row, in the Array object -- along the lines of

Code:
arrayInstance.some_function({3:4}, {5:END}); where END takes the value -1, and can be defined in Array, or somewhere else.

The way I have tackled this so far was to write myself an Indices PODS class with two elements to hold start and finish values, and a constructor-from-initializer_list that looks something like this:

Code:
Indices::Indices(std::initializer_list<int> range, int replace_value) {
int const *it = range.begin();

start = (*it == END) ? replace_value : *it ; ++it;
finish = (*it == END) ? replace_value : *it ;
...
}

So the elements of "range" give the values of Indices::start and Indices::finish -- but if either of them are entered as END by the user, they will be replaced by replace_value. (The default value of replace_value is END, so Indices::start and Indices::finish will never change if it is omitted.)

I also defined an Indices::endset(int) function to do the same thing for existing Indices objects:

Code:
Indices::endset(int replace_value) {
if (start == END) start = replace_value;
if (finish == END) finish = replace_value;
} Using Indices::endset, you can code up Array::some_function by modifying the above signature to something like

Code:
Array<Type>::some_function(Indices columns, Indices rows) {
columns.endset(this->M);
rows.endset(this->N);
...
}

This does work, and I've been able to use it in practice. However, it is klutzy. What I would really like to be able to do is have the Indices constructor handle value-replacements in "columns" and "rows", instead of needing to put calls to Indices::endset in every single Array<Type> member function that uses this approach.

The basic problem is that, when Array<Type>::some_function is called, there is no easy way of inserting Array<Type>::M and Array<Type>::N into the optional argument of the Indices constructor when "columns" and "rows" are being built.

The Indices class needs somehow to get access to these, and know which one is being used, M or N. So it needs to have some sort of deeper connection to Array<Type>, but I don't know what that connection should be.

View 2 Replies View Related

C :: Dynamically Create A New Memo Structure To Hold Memorized Fib

Jun 14, 2013

I need to dynamically create a new Memo structure to hold memorized fib #'s.I have two structures:

Code:

typedef struct HugeInteger
{
//array to hold the digits of a huge integer
int *digits;
//number of digits in the huge integer
int length;
}

[code]....

am having trouble with initializing the struct inside of the new Memo, I need the digit fields to null and the length field to 0 to indicate that F[i] has not yet been memoized...I have F->digits and F->length in the for loop but this just simply doesn't work..

View 2 Replies View Related

C :: Finding Positive / Negative Integers Unsigned Can Hold

Jan 25, 2013

Consider a new data type, the mikesint, which can hold 9 bits.

(a) What is the largest integer that an unsigned mikesint can hold?
(b) What is the largest positive integer that a signed mikesint can hold?
(c) What is the largest negative integer that a signed mikesint can hold?

Not sure how to determine this. I'm stuck.

View 5 Replies View Related

C :: Declare Two 1-dimensional Parallel Arrays Of Integers That Will Hold The Information Of Up To 10 People

Jul 30, 2014

In pseudocode and in C code, declare two 1-dimensional parallel arrays of Integers to store the age and weight of a group of people that will hold the information of up to 10 people. Use a For loop to iterate through the array and input the values. This is the code i have, but my array is showing up as 11 and not 10?

Code:
#include <stdio.h>
int main() {
//Define Variables
float Age_Data[10],Weight_Data[10];
float Age_Input,Weight_Input;

[Code] .....

View 3 Replies View Related

C# :: Get Item From List In Another Class

Jan 15, 2014

How can I get the items from a list into textboxes. I am doing an windows form application in visual studio.

I have the first form with a couple of textboxes.

I want the inputs be saved in a list. And then displayed in another form. How to move on. The class with the list looks like this:

public class Casecs {
public List<caseInformation> fallInformation = new List<caseInformation>();
public class caseInformation {
public string caseId { get; set; }
public string phoneNumber { get; set; }
public string description { get; set; }
}

Then the form with the inputs in textboxes looks like this:

Casecs Casecs = new Casecs();
Casecs.fallInformation.Add(new ERIS.Casecs.caseInformation {
caseId = txtCaseId.Text,
phoneNumber = txtPhonenumber.Text,
message = txtMessage.Text
});

Then where I am stuck a form with some textboxes where I want the list items to be displayed:

public partial class btnDispatch : Form {
public btnDispatch() { }
public btnDispatch(List<Casecs.caseInformation> fallInformation) {
InitializeComponent();

View 7 Replies View Related

C++ :: Splitting In A Linked List Class

Mar 9, 2013

I'm trying to develop a program that stores a list of hw assignments in the form of "Date,Assignment name,grade". I have functions that add,remove, and delete from a list, but I would also like to search and delete a particular date found in the list. From here, split - Splitting a string in C++ - Stack Overflow I've tested Evan Teran's method to split a string, but I'm unsure of how to implement it in my code.

List.h Code: #ifndef LIST_H
#define LIST_H
#include <string>
using namespace std;
class List{

[Code] .....

View 14 Replies View Related

C++ :: Creating Methods For Class List?

Jul 3, 2013

Creating the methods for class List

main.cpp Code: #include "List.h"
int main( )
{
List la; // create list la
la.push_front( "mom" );
la.push_back( "please" );
la.push_back( "send" );
la.push_back( "money" );
la.push_front( "hi" );
cout << "
la contains:
" << la << '

[code]...

View 12 Replies View Related

C++ :: Using Array Initialization List For Own Class

Jun 17, 2014

i'm currently working on a research project and i've been given some specifications

Is there a way i can access/use the array initialisation list i.e

{value,value,value}; .

For my own class? Like this

myclass foo={value,value,value};

View 3 Replies View Related







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