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.
Related
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'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.
For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>
using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;
int main()
{
map<mpz_t, double>J;
mpz_t a,b,c,n;
string tempstring;
int xrange=5,yrange=5,component=5;
mpz_set_str(n,"11", 10);
J[n]=-1;
return 0;
}
The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)
Here's the detail error page:
I don't know the details of mpz_t. However, it appears to be an array.
You can get around the problem by defining a class to be used as the key in your map.
I am able to create an executable using the following code with g++ 4.8.2.
#include <map>
using namespace std;
typedef int (mpz_t)[2];
struct MyKey
{
// Add a proper implementation of a constructor
// with mpz_t.
MyKey(mpz_t in) {}
// Add a proper implementation of copy constructor.
MyKey(MyKey const& copy) {}
// Add a proper implementation of assignment operator.
MyKey& operator=(MyKey const& rhs)
{
return *this;
}
bool operator<(MyKey const& rhs) const
{
// Add a proper implementation.
return false;
}
mpz_t n;
};
int main()
{
map<MyKey, double> J;
mpz_t n;
J[n] = 1.0;
return 0;
}
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