Pages: [1]   Go Down
Author Topic: Issues with StackArray library when passing object to function  (Read 466 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

I'm using the StackArray library to create a few StackArray objects that I'd like to pass by reference to a function. Here's my code:

Code:
#include <StackArray.h>

StackArray <int> BD_rgb_stack;
StackArray <int> SD_rgb_stack;
StackArray <int> ST_rgb_stack;
StackArray <int> FT_rgb_stack;

void setup(){
 Serial.begin(9600);
}


void loop(){
  build_simple(BD_rgb_stack);
}


void build_simple(StackArray<int> drumArray){
  //builds a simple stack
    drumArray.push(1);
    //end stack 
}

And here's the error:

Quote
test.ino: In function 'void loop()':
test:33: error: 'build_simple' was not declared in this scope

It seems to be an issue with how the StackArray object is being passed to the build_simple() function. Am I declaring this function incorrectly?

Thanks!
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4242
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What are you trying to push onto a stack and which stack is it going on ?

At first sight you seem to be trying to push the BD_rgb_stack stack onto a stack called drumArray that has not even been declared but bear in mind I know nothing about the StackArray library.

Incidentally, I would be interested to know what you are going to use the stacks for and why ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2166
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to specify a reference in the formal parameter list ( function arguments ),
Code:
void build_simple( StackArray<int> &drumArray );

Also place that line at the top of your sketch to declare the function ( or above its first use ). Here is a fixed sketch:
Code:
#include <StackArray.h>

StackArray <int> BD_rgb_stack;
StackArray <int> SD_rgb_stack;
StackArray <int> ST_rgb_stack;
StackArray <int> FT_rgb_stack;

void build_simple(StackArray<int> &drumArray);

void setup(){
 Serial.begin(9600);
}


void loop(){
  build_simple(BD_rgb_stack);
}


void build_simple(StackArray<int> &drumArray){
  //builds a simple stack
    drumArray.push(1);
    //end stack  
}
« Last Edit: March 16, 2013, 05:25:27 am by pYro_65 » Logged


North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 69
Posts: 2166
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

On inspection of the stack:

Code:
// push an item to the stack.
template<typename T>
void StackArray<T>::push (const T i) {
  // check if the stack is full.
  if (isFull ())
    // double size of array.
    resize (size * 2);

  // store the item to the array.
  contents[top++] = i;
}

It effectively chews double the memory it needs, that's less than 512 ints on an Uno which could easily support an array of close to 1000 ints.

Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also place that line at the top of your sketch to declare the function ( or above its first use ).

Oh wow, I had no idea I had to declare a function before using it. You don't usually have to do that for functions - why for this one? For anyone else confused, I found more info on this here: http://www.cplusplus.com/doc/tutorial/functions2/ – scroll down to Declaring Functions.

Obviously these were just placeholders for the real code I'm building within these functions. I just couldn't get the build_simple function to even accept the StackArray object, so I simplified it for the sake of this post. I will start to put my real code in and see how it takes it. Thanks for the help!

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49083
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Oh wow, I had no idea I had to declare a function before using it. You don't usually have to do that for functions - why for this one?
Because the IDE usually does that for you. It does not understand how to do that for reference variables or templates.
Logged

Pages: [1]   Go Up
Jump to: