0

I am not sure why my function is not working. It should be printing out something out (an error message after the user goes out of bounds)I have set the array index at 3 index slots. I'm also getting an error "unused variable 'yourArray' I am not sure where to go from here. Still trying to learn c++ so and advice or help will be greatly appreciated.

#include <iostream>
using namespace std;

class safeArray{
public:
  void outofBounds(int,int);
  int yourArray[3];
  int i;
};

  void outofBounds(int,int);
  int yourArray[3];
  int i;

  void outofBounds(int yourArray[],int sizeofArray) {       
  for (i=0;i<sizeofArray;i++){
  cout<<"Please enter integer";
  cin >>yourArray[i];
  yourArray[i]++;

 for (i=0;i>sizeofArray;){
 cout<<"safeArray yourArray (" <<yourArray[0]<<","<<yourArray[3]<<")"
 <<endl;
  }}}

 int main() {
 void outofBounds(int,int);
 int yourArray[3];    //Error: Used variable "yourArray"
 };
Jen
  • 91
  • 1
  • 3
  • 12
  • 2
    `void outofBounds(int,int);` does not call the function. It just declares the function again. – R Sahu May 11 '17 at 20:11
  • 2
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver May 11 '17 at 20:12
  • Also note that `cout<<"safeArray yourArray (" < – NathanOliver May 11 '17 at 20:12
  • It does not look like you need your class, or even a function for that matter, since all the work that is being done by outofBounds could be done right in the main function. – didiz May 11 '17 at 20:38

1 Answers1

0

Your Program is running fine. Unless you added the "-Werror" flag to the compiler, which would treat the "unused variable"-Warning as an Error. The code compiles fine as seen on here: http://coliru.stacked-crooked.com/a/d648b94f205b51dc

Though your Program does not do what you want it to do, because of the following reasons:


1.) You have 3 redefinitions of outofBounds inside different namespaces:

  • one inside the classes namespace SafeArray which is a member function of it
  • then inside the global space
  • and then inside the main-function (the entry point)

But the one being actually defined is the one in the global space (2nd one)


2.) You are not passing anything to the function inside main. define your Array there first then call the function by doing:

int yourArray[3];
outofBounds(yourArray, 3);

3.) You probably wanted to define the member method "outofBounds" inside SafeArray-class. This can be done by writing the scope operator:: which specifies the class to which the member function belongs to:

class SafeArray {   // is a class, can also be struct since everything is public anyways
public:
    void outofBounds(int,int); // a member of the class SafeArray
// private:
    int yourArray[3];
    int i;
};

void SafeArray::outofBounds(int yourArray[],int sizeofArray) { 
    // do something...
}

but then again you need some constructor that initializes the members of your class. Some work needs to be done to make it work, like you want. Good Luck :)

Robert
  • 127
  • 1
  • 10