Beginner C++ vector woes

Discussion in 'Mac Programming' started by leftblank, May 29, 2012.

  1. leftblank, May 29, 2012
    Last edited: Jun 4, 2012

    macrumors newbie

    Joined:
    May 29, 2012
    #1
    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);
        }
        
    }
    
     
  2. lloyddean, May 29, 2012
    Last edited: May 29, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    "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.
     
  3. leftblank, May 29, 2012
    Last edited: Jun 4, 2012

    thread starter macrumors newbie

    Joined:
    May 29, 2012
    #3
    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()
    {
    
    }
    
     
  4. lloyddean, May 29, 2012
    Last edited: May 29, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Code:
    Carousel::Carousel() : activeSection(0)
    {}
    

    Code:
    void testApp::setup()
    {
        carousel = new Carousel;
        if ( carousel )
        {
    	    carousel->addSection(CarouselSection());
        }
    }
    
     
  5. thread starter macrumors newbie

    Joined:
    May 29, 2012
    #5
    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...
     
  6. lloyddean, May 29, 2012
    Last edited: May 30, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Isn't that what was just done?

    EDIT: You said class member my mistake.
     
  7. thread starter macrumors newbie

    Joined:
    May 29, 2012

Share This Page