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.
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.
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.
Related
I made a class in a .cpp and a .h file with in the same project, and then I tried to make a calculating code. I made an object for it, with the name "co". I then tried to build it and run the file, and it showed nothing. Why is that happening and how can I try to fix it?
The code:
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "CalculatorClass.h"
using namespace std;
int main()
{
calculatorClass co ();
}
calculatorClass.cpp
#include "calculatorClass.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
calculatorClass :: calculatorClass ()
{
int x = 25;
int y = 37;
int z = 51;
int a = 14;
int b = 63;
int c = 75;
cout << x * y * z * a * b * c ;
}
calculatorClass.h
#ifndef CALCULATORCLASS_H
#define CALCULATORCLASS_H
class calculatorClass
{
public:
calculatorClass (int hello);
calculatorClass();
protected:
private:
};
#endif // CALCULATORCLASS_H
Thanks!
change calculatorClass co (); to calculatorClass co. This will work. The explanation is as the following question - C++: warning: C4930: prototyped function not called (was a variable definition intended?).
By calling calculatorClass co (); you are not creating an object; you are declaring a function.
It looks like you are trying to create a constructor.
So the first thing to do is look at your calculatorClass.h. You need to change it to look like this because a constructor is a member function that has the same name as the class.
#ifndef CALCULATORCLASS_H
#define CALCULATORCLASS_H
class calculatorClass
{
public:
calculatorClass(); //Constructor.
protected:
private:
};
#endif // CALCULATORCLASS_H
Then in your calculatorClass.cpp you will need to change it to this.The function header of a constructor's external definitions takes a form like this:
Classname::Classname(Parameters)
#include "calculatorClass.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
calculatorClass::calculatorClass() // Changed.
{
int x = 25;
int y = 37;
int z = 51;
int a = 14;
int b = 63;
int c = 75;
cout << x * y * z * a * b * c;
}
Then in your main.cpp file you are going to define an instance of the class calculatorClass which I did and named it calc. Now, the function calculatorClass is automatically called in main().
#include <iostream>
#include <fstream>
#include <string>
#include "CalculatorClass.h"
using namespace std;
int main()
{
calculatorClass Calc; // Define a calculatorClass object
return 0;
}
Just a heads up, when you use ints they only hold 32 bits. So with all these large numbers you are trying to multiplying will roll over to a large negative value and you will not get the correct answer.
You might want to use long long int.
The main issue in the code, that the following line is interpreted differently than you think:
calculatorClass calc();
This line is interpreted as a declaration of a function prototype instead of creating a new object of calculatorClass. This is known as the most vexing parse as noted in the comments.
There is a way to declare and initialize an object in a similar fashion:
calculatorClass calc{};
Notice that I have used {} and not () ! This was introduced in order to avoid this vexing parsing of the code.
In addition, I want to comment about some other bad habits in the code:
Using using namespace std; is a bad habit, which causes name pollution and may cause bugs with name ambiguity, and in the worst case related to unexpected functions being called!
Class names always start with a capital letter - calculatorClass should be Calculator (we know that it is a class, thus class shouldn't appear in the name!).
There is no need in the protected and private modifiers here.
I'm writing my code on linux . But g++ always tells me"Use of undeclared identifier 'random'".I don't know why I have declare it in "Myvector.h"
my code is like :
Myvector.h
class MyVector {
private:
std::vector<double> data;
const int N;
static bool _bDim;
public:
MyVector(); //默认初始化
MyVector(int a); //设置维度初始化
MyVector(std::initializer_list<double> list);
~MyVector();
double &operator[](int);
MyVector &operator=(const MyVector a) {
MyVector b(outN(a));
this->data = a.data;
return *this;
};
friend MyVector random(int a);
}
#endif // MYVECTOR_H_
Myvector.cpp
#include "Myvector.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
bool MyVector::_bDim = true;
MyVector::MyVector() : N(3) {
data = vector<double>(N, 0.0);
_bDim = false;
};
MyVector::MyVector(int a) : N(a) {
data = vector<double>(N, 0.0);
_bDim = false;
};
MyVector::MyVector(std::initializer_list<double> list) : N(list.size()) {
for (auto i = list.begin(); i != list.end(); i++) {
data.push_back(*i);
}
};
MyVector::~MyVector(){
};
double &MyVector::operator[](int i) { return data[i]; }
MyVector random(int a){
MyVector u(a);
srand(time(NULL));
for(int i=0;i<a;i++){
u[i]=rand();
}
return u;
}
main.cpp
#include "Myvector.h"
#include <iostream>
#include<cstdlib>
#include<math.h>
#include <time.h>
using namespace std;
int main(){
MyVector z=random(1);
return 0;}
In fact ,I just know nothing about it. Is there someone going to help me?Thank you.
Below is nothing meaningful. I just need more words to ask this problem.
In the main function of the main.cpp file the following function is called:
MyVector z=random(1);
This appears to be a function which takes a single int argument. Additionally, there is such a function defined in the Myvector.cpp but not declared in Myvector.h (i.e., the main.cpp file does not see any function declaration for the definition).
Update the Myvector.h header to declare the MyVector random(int a) function. Also, the friend declaration is for a random function with 2 parameters, which doesn't look right.
You have to declare the function random somewhere, e.g.
#include "Myvector.h"
#include <iostream>
#include<cstdlib>
#include<math.h>
#include <time.h>
using namespace std;
MyVector random(int);
int main(){
MyVector z=random(1);
return 0;
}
The problem is random in your main function. That one is not declared.
Declaring a friend function means that function has access to the class as if it were a method. It doesn't declare the function at any time, just allows it inside the class.
Your random function is defined is some header file you have included. In your error message you see the return type of random is long int. And you have declared it as MyVector. I am not sure if math.h or time.h have it.
Solution 1: Change the name of your function.
Solution 2: Put your function in a namespace in order to avoid name ambiguity.
I am trying to print the first value from each vector shown below in the main function.
#include <iomanip>
#include <map>
using namespace std;
typedef unsigned int vect;
int main() {
std::vector<vect> p;
vector<vect> a = { 4,2,3,1 };
vector<vect> b = { 4,2,3,1 };
vector<vect> c = { 4,2,3,1 };
vector<vect> d = { 4,2,3,1 };
int i;
for (i=0; i<a.size(); i++)
cout << a[i];
}
Function first_preference() from my function.cpp shown below
#include "function.h"
#include <string>
#include <iostream>
using namespace std;
person test::first_preference() const {
const person& first = p.front();
return first; //current first pref
}
The function is declared in this header class
#ifndef FUNCTION_H
#define FUCNTION_H
#include <vector>
typedef unsigned int person;
typedef unsigned int vect;
std::vector<vect> p;
class test {
public:
person first_preference() const;
};
#endif
I want the function first_preference() to be called from main() where the function should print the first value of each vector, how would I go about this?
I want the function first_preference() to be called from main() where the function should print the first value of each vector
Some issues:
You have a global std::vector<vect> p in your header file (which is not a good idea to begin with) which is shadowed by std::vector<vect> p in main. What you put in the p in main will not be accessible from instances of test. Those instances only knows about the global p.
You don't #include "function.h" in main.cpp so you can't create test objects in main.
If you #include "function.h" in main.cpp there's no need to typedef unsigned int vect; since you did that in function.h already. It's not an error, but confusing and unnecessary.
The vector<vect> instances a, b, c and d have no connection with test or any of the ps whatsoever so what you put in those vectors can't possibly be printed by instances of test unless you pass them on to test somehow.
You declare vectors of vect but first_preference() returns a person by value. vect and person happen to be aliases of the same fundamental type, but it seems like there is something wrong with this interface.
In main.cpp you don't instantiate a test, you iterate over a and first_preference() is never called so there's no hope for it to be used.
Why is “using namespace std;” considered bad practice?
I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.
i have this error in the title:
here class declaration of variables and prototypes of function
#ifndef ROZKLADLICZBY_H
#define ROZKLADLICZBY_H
class RozkladLiczby{
public:
RozkladLiczby(int); //konstruktor
vector<int> CzynnikiPierwsze(int); //metoda
~RozkladLiczby();
};
#endif
And class body:
#include "RozkladLiczby.h"
using namespace std;
#include <iostream>
#include <vector>
RozkladLiczby::~RozkladLiczby() //destruktor
{}
RozkladLiczby::RozkladLiczby(int n){
int* tab = new int[n+1];
int i,j;
for( i=0;i<=n;i++)
tab[i]=0; //zerujemy tablice
for( i=2;i<=n;i+=2)
tab[i]=2; //zajmujemy sie liczbami parzystymi
for(i=3; i<=n;i+=2)
for(j=i;j<=n;j+=i) //sito erastotesa
if(tab[j]==0)
tab[j]=i;
}
vector<int> RozkladLiczby::CzynnikiPierwsze(int m){
vector<int> tablica;
while(m!=1){
tablica.push_back(tab[m]);
m=m/tab[m];
}
return tablica;
}
Whats wrong with the prototype of function in first block? Why vector is told to be not a type? I would be grateful if u could help me to find out this.
Your header file does not include the vector header. Add a #include <vector> to the beggining.
Besides, you should refer to it as std::vector<int> instead of vector<int>, since it belongs to the std namespace. Declaring using namespace x; in header files is not a good practice, as it will propagate to other files as well.
Change your header file to:
#ifndef ROZKLADLICZBY_H
#define ROZKLADLICZBY_H
#include <vector>
class RozkladLiczby{
public:
RozkladLiczby(int); //konstruktor
std::vector<int> CzynnikiPierwsze(int); //metoda
~RozkladLiczby();
private:
int* tab;
};
#endif