Go Down

Topic: Issues with StackArray library when passing object to function (Read 526 times) previous topic - next topic

dmayman

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: [Select]
#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!

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

pYro_65

#2
Mar 16, 2013, 11:23 am Last Edit: Mar 16, 2013, 11:25 am by pYro_65 Reason: 1
You need to specify a reference in the formal parameter list ( function arguments ),
Code: [Select]
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: [Select]
#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  
}

pYro_65

On inspection of the stack:

Code: [Select]
// 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.


dmayman


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!


PaulS

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.

Go Up