Reputation: 3
I'm having a problem with my code. It's giving me the two errors 'a' and 'MAX' was not declared in this scope, in int main().
I am pretty sure I defined it in my header file. I've read countless forums and tutorials and I'm not really sure what I'm doing wrong.
//main.cpp
/* A test driver for the VectorMin class (VectorMin.cpp) */
#include <iostream>
#include<vector>
#include "VectorMin.h"
using namespace std;
int main() {
VectorMin c1;
std::vector <int> v1(a,a+MAX);
int min = c1.recursiveMinimumVector(v1,v1[0],1);
c1.printOut(min);
return 0;
}
Implementation file
//VectorMin.cpp
/* The VectorMin class Implementation (VectorMin.cpp) */
#include "VectorMin.h" // user-defined header in the same directory
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
// Constructor
VectorMin::VectorMin()
{
}
VectorMin::int recursiveMinimumVector(const std::vector<int>& b, int minVal, size_t subStart)
{
if(subStart+1 >= b.size())
return minVal;
else
return recursiveMinimumVector(b, std::min(minVal, b[subStart]), subStart+1);
}
VectorMin::void printOut(int m)
{
std::cout << "Min value= " << m;
}
Header file
//VectorMin.h
/* The VectorMin class Header (VectorMin.h) */
#include <string> // using string
// VectorMin class declaration
class VectorMin{
private:
static const int MAX = 10;
const int a[MAX] = {44, 83, -14, 1, 101, -92, 23, 2, 7, 100};
public:
// Declare prototype of member functions
// Constructor with default values
VectorMin();
// Public member Functionsvoid generateRand();
int recursiveMinimumVector(const std::vector<int>& b, int minVal, size_t subStart);
void printOut(int);
};
If someone could point out what I'm doing wrong and explain why it's wrong I would greatly appreciate it.
Upvotes: 0
Views: 163
Reputation: 117856
In main
, you have these two lines
VectorMin c1;
std::vector <int> v1(a,a+MAX);
First of all, a
and MAX
are member variables of the VectorMin
class, so you'd have to call them like
c1.a
VectorMin::MAX
Second of all you still can't do that because those members are private
.
Upvotes: 1
Reputation: 16215
Variables aren't global.
Just because you declared them in one class doesn't make them accessible to other classes. In your case, a
and MAX
belong to your VectorMin
class, and aren't accessible by your main
function.
Upvotes: 1