C++ :: Popping From STL Containers

Feb 21, 2014

I've been working on a project that involves storing pointers to dynamically allocated class objects in an STL list, but trying to run it something's going wrong.

list<Actor*>::iterator it;
for(it = m_actors.begin() ; it != m_actors.end() ; ++it)
{
delete *it;

[Code]....

But it seems like that has a memory leak. Does pop_front() just call the destructor for the object, or will it delete a dynamically allocated chunk of memory? If not, how can I do that deletion to avoid a memory leak?

View 4 Replies


ADVERTISEMENT

C/C++ :: Must Return A Value Error Keeps Popping Up

Apr 22, 2015

'RationalOperations::hcfCalculation' : must return a value
#include<iostream>
#include<cstdlib>
using namespace std;

[Code].....

View 2 Replies View Related

C :: Weird Characters Popping Up In Fwrite Function

Mar 13, 2013

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {

[Code]....

this code asks the user to input words/strings until he enters "end."after that, the program must copy the input to a text file named read.txt...I entered 'j' and then 'end' and after that I looked at the read.txt file and here's what's in it.

Output:

Code:
j
; end

some weird characters appeared!! the characters in the text file should only be.

View 6 Replies View Related

C++ :: Dynamic Containers With Arrays?

Feb 14, 2015

Im trying to create a function that searches my array for a specific string, and then displays that string and the other content associated with it. I'm basically searching for a keyword within an array that has multiple strings with in each element.

View 4 Replies View Related

C++ :: Create Function That Calculates Sum Between Two Containers

Jul 1, 2012

I need to create function Sum() that calculates sum between two containers. Code below work fine except function Sum between two containers...

How I should re - write my code that everything work fine.

Condition of exercise is : "Also create a Sum() function that calculates the sum between two iterators. The function then uses the template argument for the iterator type and accepts two iterators, the start- and end iterator"

1>------ Build started: Project: HP2_ex2_iter, Configuration: Debug Win32 ------
1> main.cpp
1>c:all myс++ha level 7solutionlevel 7 homework overview of the standard template libraryhp2_ex2_itermain.cpp(47): error C2275: 'C1' : illegal use of this type as an expression

[Code]...

View 5 Replies View Related

C++ :: Custom STL Compatible Containers And Iterators?

Jul 18, 2012

So i made an STL compatible container.And to make this work I had to make my own iterator (derived from std::iterator).

What is the portable (if any) and "well behaved" thing to do in case of usage anomalies.such as iterating an iterator too far, or passing an invalid index to a operator[]

Looking at how VC++ does things in something like std::array or std::vector.

Code:

iterator_type& operator+=(difference_type offset)
{// increment by integer
#if _ITERATOR_DEBUG_LEVEL == 2
if (size < index + offset)
{// report error

[Code] .....

lots of names starting with underscores, so it's implementation specific. Is there even a "well behaved" thing to do ? Or is any such work always going to be compiler specific?

View 2 Replies View Related

C++ :: Two Identical Storage Containers - Typecasting

Oct 27, 2012

I have a situation where I have two identical storage containers:

Code:
////////////// multiplatform version
union _SOVector3 {
struct { float x, y, z; };
struct { float r, g, b; };
float v[3];

[Code] .....

SOVector3 is part of a namespace with specialized functions that are generic and intended for multiplatform usage.

GLKVector3 is dedicated to the Mac and has its own set of functions.

But what I want to do is freely interchange the storage between these two namespaces. Such as like this:

Code:
start = clock();
SOVector4 myVec4 = SOVector4Make(1.0f, 3.0, 6.0f, 1.0);
SOMatrix4 myMat4 = SOMatrix4Identity;
for (uint i=0; i<100000; ++i ) {

[Code] ....

But I am getting errors when I typecast this.

View 1 Replies View Related

C++ :: Containers That Doesn't Change Member Address

Jun 25, 2013

#include <iostream>
#include <vector>
using namespace std;
int main() {
int * ptr;
vector<int> data;
data.resize( 1000 );

[Code] ....

So I need container that doesn't rellocate their address, It doesn't need to be like vector that everything is in a single pointer such as

int * a = new int[2000];

I have a pointer pointing to a member of a container and it needs to remain valid... doing

vector<int *> Array;
// allocating
for( int i = 0; i < 1000; ++ i ){

Code] ...

Waste of time, allocating them and deallocate them seem to take some time too

a List is also not efficient enough because I access them based on index

It is not a int it is pointing to but a texture, for the sake of simpler example I pick int

So is there a container that doesn't change member address and allocate when it needs to expand ?

View 3 Replies View Related

C++ :: Manipulating Set Of Containers - Vector Of Vectors Of Objects

Mar 5, 2013

I need manipulating a set of containers. I created a vector that contained vectors of objects:

std::vector< std::vector<Terrain> > mapArray(num1, std::vector<Terrain>(num2));

where num1 and num2 are arbitrary numbers. and Terrain is the class of objects I'm trying to store.

I want to be able to use push_back on both the main vector and the vectors within the mapArray vector but I'm unsure of how to target the inner vectors with push_back. How to dynamically store a 2D array of objects.

View 10 Replies View Related

Visual C++ :: Creating Containers That Respond To User Actions?

Oct 16, 2013

I am looking for direction on what topic I should be reading up on. I am new to C++ and Windows MFC.

This is my real world problem, in the context of the application user. (these term do not refer to OPP concepts)

I want to create shapes (containers) in an application that will respond and collect other objects;

Imagine a Windows frame, containing several 2 dimensional squares. I want to be able to drag and drop marbles into the squares,and have the square retain and display the marbles in the square, in the order that they were dropped in.

How do I create the shapes, and how will the square sense when a marble is over it?

How would I create irregular shapes (a combination of lines and curves) that would be responsive to the marbles?

View 2 Replies View Related

C++ :: Smart Accessor Function For Multiple Data Containers?

Mar 14, 2012

Suppose I'm writing a program designed to simulate a large company. I'm interested in tracking each company employee by the location where they work. This company has perhaps a thousand different locations:

class Employee {
public:
AccessorFunction1(); // does something
AccessorFunction2(); // does something different
AccessorFunction3(); // does something completely different
protected:
// Some data

[code]....

Once employees are created and pointers to them are saved in the proper Location vector, I write an accessor function, OrganizeLocation(), designed to do a number of operations on a given vector. The problem is, I have maybe a thousand vectors. How do I call this function and specify which vector I want?

Currently, I'm using this clunky solution:

void Company::OrganizeLocation(int a){
switch(a) {
case 1: {
for(unsigned int i=0; i<LocationA.size(); i++) {
LocationA[i]->AccessorFunction1();
LocationA[i]->AccessorFunction2();
LocationA[i]->AccessorFunction3();

[code]....

The key point here is that whichever vector I choose to operate upon, I'll do the exact same procedure every time. I hate this solution because it results in very long and repetitive code not to mention its very error-prone when you re-editing all those "LocationA 's into "LocationB/C/D/etc."

void Company::OrganizeLocation( string $WhichOne$ ){
for(unsigned int i=0; i<LocationA.size(); i++) {
Location$WhichOne$[i]->AccessorFunction1();
Location$WhichOne$[i]->AccessorFunction2();
Location$WhichOne$[i]->AccessorFunction3();
}

Or, if it can't be done in C++, is there a better design approach? Ultimately I need the Company object to hold multiple vectors but use one compact accessor function to perform operations on just one of them.

View 5 Replies View Related







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