Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 29, 2012, 06:31 PM   #1
leftblank
macrumors newbie
 
Join Date: May 2012
Beginner C++ vector woes

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 by leftblank; Jun 4, 2012 at 04:10 PM.
leftblank is offline   0 Reply With Quote
Old May 29, 2012, 06:47 PM   #2
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: 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 by lloyddean; May 29, 2012 at 07:04 PM.
lloyddean is online now   0 Reply With Quote
Old May 29, 2012, 07:05 PM   #3
leftblank
Thread Starter
macrumors newbie
 
Join Date: May 2012
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 by leftblank; Jun 4, 2012 at 04:10 PM.
leftblank is offline   0 Reply With Quote
Old May 29, 2012, 07:52 PM   #4
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Code:
Carousel::Carousel() : activeSection(0)
{}

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

Last edited by lloyddean; May 29, 2012 at 08:13 PM.
lloyddean is online now   1 Reply With Quote
Old May 29, 2012, 08:10 PM   #5
leftblank
Thread Starter
macrumors newbie
 
Join Date: May 2012
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...
leftblank is offline   0 Reply With Quote
Old May 29, 2012, 08:35 PM   #6
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Isn't that what was just done?

EDIT: You said class member my mistake.

Last edited by lloyddean; May 30, 2012 at 02:52 PM.
lloyddean is online now   0 Reply With Quote
Old May 29, 2012, 08:43 PM   #7
leftblank
Thread Starter
macrumors newbie
 
Join Date: May 2012
Thanks, that works.
leftblank is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Which Vector Program? samo1215 Mac Applications and Mac App Store 0 Feb 10, 2014 04:10 PM
All iPads: Vector Scans? Reddkryten iPad 0 Oct 17, 2013 06:00 AM
Fern Vector Brushes..? mikebatho Design and Graphics 0 Aug 12, 2013 07:34 PM
Vector Images? bumblebritches5 OS X Mavericks (10.9) 4 Jun 10, 2013 11:41 PM
Vector based maps cclloyd iOS 6 11 Sep 29, 2012 06:36 PM

Forum Jump

All times are GMT -5. The time now is 04:19 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC