Beginner vector troubles

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// testApp.m

#include "testApp.h"

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

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

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 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 on
Sorry, maybe not enough of the code posted there, I tried to keep it brief. The execution process is handled, and first calls the setup() method of testApp.cpp, then the draw() consecutively.

Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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());
    
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 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;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// testApp.cpp

#include "testApp.h"

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

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

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 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);
    }
    
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// carouselSection.h

#ifndef carouselSection_h
#define carouselSection_h

#include "carouselItem.h"

class CarouselSection {
public:
    CarouselSection(); 
};

#endif


1
2
3
4
5
6
7
8
9
10
// carouselSection.cpp

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

CarouselSection::CarouselSection()
{

}
Last edited on
Line 10 in Carousel.m creates a local vector with size 20. If you want to set the size of Carousel::carouselSections to 20 you could do carouselSections.resize(20); or use the initializer syntax (or whatever it is called):
1
2
3
4
5
Carousel::Carousel()
:	carouselSections(20)
{
    activeSection = 0;
}

Cool, thanks, I have changed that. However, the size() call is still bad accessing on line 39 of Carousel.m... or Carousel.cpp as it's actually called, the comment is incorrect :)
Last edited on
You have a similar problem with carousel on line 8 in testApp.cpp
Hmm. So if i declare the carousel as a class member in testApp.h, i can't initialize it in a method of testApp.cpp? Where could I initialize a member variable of a class and keep it in scope?
Where could I initialize a member variable of a class and keep it in scope?

That's what constructors are for.

You could set the size of the vector in the constructor as Peter87 suggested:
Carousel::Carousel()
{ carouselSections.resize(20);
}


Or you could simply let the vector expand automaticaly as you add sections to it.
Great, thank you.
Topic archived. No new replies allowed.