PDA

View Full Version : Beginner C++ vector woes




leftblank
May 29, 2012, 05:31 PM
Hi. I'm having a problem getting started with C++.

The setup() method in testApp.m is called first, which initializes the application, where I create my base carousel, then attempt to add a new section to it. After this, draw() is repeatedly called, or it should be... I get an EXC_BAD_ACCESS when I try to find the size of my vector and I can't for the life of me see why. The debugger indicates that there is an objects in the vector, so why can't I access the size of it?

So confused!




// testApp.m

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

Carousel *carousel = new Carousel;
carousel->addSection(CarouselSection());
}

//--------------------------------------------------------------
void testApp::draw() {
carousel->draw();
}





// Carousel.m

#include <iostream>
#include "ofMain.h"
#include "carousel.h"

Carousel::Carousel()
{
activeSection = 0;
vector<CarouselSection> carouselSections (20);
}

void Carousel::addSection(CarouselSection section)
{
carouselSections.push_back(section);
}

void Carousel::moveLeft()
{
if (activeSection > 0) {
activeSection--;
}
}

void Carousel::moveRight()
{
if (activeSection < carouselSections.size()) {
activeSection++;
}
}

int Carousel::getActiveSection()
{
return activeSection;
}

void Carousel::draw()
{
for(int i=0; i<carouselSections.size(); i++)
{
cout << "number of sections: " << carouselSections.size() << endl;
CarouselSection section = carouselSections.at(i);
section.draw(i * 220);
}

}



lloyddean
May 29, 2012, 05:47 PM
"Carousel.m", do you mean "Carousel.mm"


Carousel::Carousel()
{
activeSection = 0;
vector<CarouselSection> carouselSections(20); << --- Disappears upon exit of constructor
}


No reason to believe any of this code even compiles.

Perhaps post a full, compilable example.

leftblank
May 29, 2012, 06:05 PM
Hi sorry, I tried to keep it brief. The execution process is handled, and first calls the setup() method of testApp.cpp, then the draw() consecutively.

Also the file was named carousel.cpp - I was in Objective C frame of thinking when I made that carousel.m comment!


// main.cpp

#include "ofMain.h"
#include "testApp.h"
#include "ofAppGlutWindow.h"

//========================================================================
int main( ){

ofAppGlutWindow window;
ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context

// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new testApp());

}



// testApp.h

#pragma once

#include "ofMain.h"
#include "oscReceiver.h"
#include "ofxUI.h"
#include "carousel.h"

class testApp : public ofBaseApp{

public:
void setup();
void draw();

Carousel *carousel;
};




// testApp.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

Carousel *carousel = new Carousel;
carousel->addSection(CarouselSection());
}

//--------------------------------------------------------------
void testApp::draw() {
carousel->draw();
}




// carousel.h

#ifndef carousel_h
#define carousel_h

#include <vector>
#include "ofMain.h"
#include "carouselSection.h"

class Carousel {

public:
Carousel();

void addSection(CarouselSection section);
void update();
void moveLeft();
void moveRight();

vector<CarouselSection> carouselSections;
private:
int activeSection;
};

#endif




// Carousel.m

#include <iostream>
#include "ofMain.h"
#include "carousel.h"

Carousel::Carousel()
{
activeSection = 0;
vector<CarouselSection> carouselSections (20);
}

void Carousel::addSection(CarouselSection section)
{
carouselSections.push_back(section);
}

void Carousel::moveLeft()
{
if (activeSection > 0) {
activeSection--;
}
}

void Carousel::moveRight()
{
if (activeSection < carouselSections.size()) {
activeSection++;
}
}

int Carousel::getActiveSection()
{
return activeSection;
}

void Carousel::draw()
{
for(int i=0; i<carouselSections.size(); i++)
{
cout << "number of sections: " << carouselSections.size() << endl;
CarouselSection section = carouselSections.at(i);
section.draw(i * 220);
}

}



// carouselSection.h

#ifndef carouselSection_h
#define carouselSection_h

#include "carouselItem.h"

class CarouselSection {
public:
CarouselSection();
};

#endif




// carouselSection.cpp

#include <iostream>
#include "ofMain.h"
#include "CarouselSection.h"

CarouselSection::CarouselSection()
{

}

lloyddean
May 29, 2012, 06:52 PM
Carousel::Carousel() : activeSection(0)
{}




void testApp::setup()
{
carousel = new Carousel;
if ( carousel )
{
carousel->addSection(CarouselSection());
}
}

leftblank
May 29, 2012, 07:10 PM
Cool, thanks for looking at this.

If i declare the carousel as a class member in testApp.h, can initialize it in the setup() method of testApp.cpp and would it remain in scope? Same with the initialization of the CarouselSection in the Carousel constructor...

lloyddean
May 29, 2012, 07:35 PM
Isn't that what was just done?

EDIT: You said class member my mistake.

leftblank
May 29, 2012, 07:43 PM
Thanks, that works. :)