Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

leftblank

macrumors newbie
Original poster
May 29, 2012
5
0
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!


Code:
// testApp.m

#include "testApp.h"

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

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

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


Code:
// 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);
    }
    
}
 
Last edited:

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
"Carousel.m", do you mean "Carousel.mm"

Code:
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.
 
Last edited:

leftblank

macrumors newbie
Original poster
May 29, 2012
5
0
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!

Code:
// 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());
    
}

Code:
// 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;
};

Code:
// testApp.cpp

#include "testApp.h"

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

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

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


Code:
// 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

Code:
// 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);
    }
    
}

Code:
// carouselSection.h

#ifndef carouselSection_h
#define carouselSection_h

#include "carouselItem.h"

class CarouselSection {
public:
    CarouselSection(); 
};

#endif

Code:
// carouselSection.cpp

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

CarouselSection::CarouselSection()
{

}
 
Last edited:

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
Code:
Carousel::Carousel() : activeSection(0)
{}


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

leftblank

macrumors newbie
Original poster
May 29, 2012
5
0
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.