I'm getting no matching function for call to error how can I fix this without using vectors.
This is my header, I'm getting the error at fList method part in cpp.
class Flight{
public:
Flight(int fly,int rw,int st);
~Flight();
Flight* fList(int& sizeOfArray);
private:
int FlightNumber;
int row;
int seat;
int* flightArray;
};
This is my cpp:
#include <iostream>
#include <string>
using namespace std;
#include "Flight.h"
Flight::Flight(int fly,int rw,int st){
FlightNumber = fly;
row = rw;
seat = st;
}
Flight::~Flight(){};
Flight* Flight::fList(int& sizeOfArray){
flightArray = new Flight[sizeOfArray];
return flightArray;
}
So
int* flightArray;
should be
Flight* flightArray;
Your code is allocating an array of Flights but your variable is declared for integers not Flights.
Plus as pointed out by #MikeCAT you need to default constructor for Flight if you are going to allocate them with new[].
Related
I'm new to C++ and I have no idea why I can't send parking as a parameter. It's the first time I use struct, so maybe I'm forgetting something. I've tried sending the adress to parking but I get an error.
const int MAX_TAB = 10;
struct bike
{
int Number;
string Type;
};
bike ReadFile(bike parking[]);
int void main()
{
bike parking[MAX_TAB];
ReadFile(parking[MAX_TAB]); // This line is incorrect
}
bike ReadFile(bike parking[])
{
...
return parking[MAX_TAB];
}
Why does this not work? And how can I make it works?
Thanks you
I have no idea why I can't send parking as a parameter.
You can't do this because parking array decays to pointer, and it doesn't take the size of the array.
ReadFile(parking[MAX_TAB]); // MAX_TAB is not taken as input param
Instead you should do this:
bike ReadFile(bike parking[], size_t size);
and to call that function:
ReadFile(parking, MAX_TAB);
When passing arrays, don't add [].
Compiled using C++14 on CPP.SH
// dependecies
#include <iostream>
#include <string>
#include <fstream>
// namespace for string
using std::string;
// defined max tab for array
const int MAX_TAB = 50;
// structure definition
struct bike
{
int Number;
string Type;
string Test;
int Km;
int Stage;
};
// ReadFile(...) signature
bike ReadFile(bike *, size_t);
int main()
{
bike parking[MAX_TAB]; //instantiate array of bike object
ReadFile(parking, MAX_TAB); //only pass the array itself, not the array[
}
// parameters are array* and size
bike ReadFile(bike parking[], size_t size)
{
return *parking; //reference the array
}
I have 2 classes.
First Class - Midgam - The constructor has the following line:
midgam = new Vector[20];
The second class - Vector - where I create an array named array.
The program works great just that I have a little problem.
At the end of the program I try to print in alphabetical order, I use the BubbleSort sorting. The sorting works fine but something in the Swap function stops.
This is how it looks:
void Midgam::Swap(Vector *xp, Vector *yp) {
Vector temp = *xp;
cout << temp.getName() << endl;
*xp = *yp;
*yp = temp;
}
void Midgam::bubbleSort() {
int i, j;
for (i = 0; i < iterator - 1; i++) {
for (j = 0; j < iterator - i - 1; j++) {
if (midgam[j].getName().compare(midgam[j+1].getName()) > 0) {
Swap(&midgam[j], &midgam[j+1]);
}
}
}
}
I work with Visual Studio, the program stops and the program shows me the following code snippet in the Vector class:
Vector::~Vector() {
if (array)
delete[] array;
}
full definitions of Midgam:
#include <iostream>
#include <string>
#include "Vector.h"
using namespace std;
#ifndef MIDGAM_H_
#define MIDGAM_H_
class Midgam {
private:
int boxNum;
int maxParties;
int iterator;
Vector *midgam;
public:
Midgam(int num_of_boxes, int num_of_parties);
virtual ~Midgam();
void Start();
void Menurmal();
void SumOfEzor();
double SumOfParty(string name);
int SumAllVotes();
void AddParty();
void Swap(Vector *xp, Vector *yp);
void bubbleSort();
void Histograma();
void PrintStars(int num);
int FindPartyByName(string party);
void PrintAll();
};
#endif /* MIDGAM_H_ */
full definitions of Vector:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#ifndef VECTOR_H_
#define VECTOR_H_
class Vector {
private:
string name;
int size;
unsigned int *array;
bool Bool;
public:
Vector(string name, int size);
Vector();
Vector & operator=(const Vector &);
virtual ~Vector();
bool StringToArray(string str);
bool getBool();
string getName();
unsigned int getAddress();
int getSize();
unsigned int getValueFromArray(int index);
double sumOfArray();
void PrintArray();
};
#endif /* VECTOR_H_ */
Does anyone know why it does not work? Thank you
Your Vector lacks a proper copy constructor.
Vector temp = *xp;
//NOT EQUAL TO:
//Vector temp;
//temp=*xp;
The above statement won't call operator=(const Vector &) even though there's an equal sign. The following line is correct and equivalent:
Vector temp(*xp);
The reason is that this is a copy initialization - temp is created and so a constructor must be called - in particular the copy constructor Vector(const Vector &). Which you did not explicitly declared and so a default one was used.
Then a shallow copy is made, temp and *xp then share the same array and when both their destructors get called the second one will try to delete already deleted memory - undefined behavior which triggers Visual Studio's debugger (at least in debug mode).
The solution is to do a proper deep copy - create a new array and copy its contents:
#include <algorithm> #Contains std::copy_n
Vector::Vector(const Vector& other)
{
name=other.name;
size=other.size;
//Creates a new array
array= new unsigned int[size];
//Copies the array contents
std::copy_n(other.array,size,array);
Boo=other.Bool;
}
Also this is a prime example of why not to use raw memory. I get that you are implementing custom vector and don't want to use std::vector for the array but at least use std::unique_ptr. If you would have just done that you wouldn't have to ask this question in the first place as the compiler would have complained and the debugger wouldn't have to do the compiler's job.
im new in c++ (and not to old in programming...) and i have problem with handling vectors and strucs in class.
basically i have a vector and a array of pointers to struct members in the class and i want work on the in my methos but im doing something worng/
here is my movement.h
#pragma once
using namespace std;
class movement
{
private:
static const int MAX_ROW_PER_TRACKER = 100;
static const int MIN_TO_START_CALC = 30;
static const int MAX_TRACKERS = 20;
struct tracker
{
int id;
double a[MAX_ROW_PER_TRACKER];
double b[MAX_ROW_PER_TRACKER];
double c;
};
vector<int> trackersOrder[MAX_TRACKERS] = {};
tracker* trackersArr[MAX_TRACKERS];
public:
movement();
void addRow(int a, int b, int c);
~movement();
};
and my movement.cpp
#include "stdafx.h"
#include "movement.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
movement::movement()
{
}
void movement::addRow(int id, int a, int b)
{
int index;
vector<int>::iterator searchID = find(trackersOrder.begin(), trackersOrder.end(), ID);
if (searchID == trackersOrder.end())
{
vector<int>::iterator freeLocation = find(trackersOrder.begin(), trackersOrder.end(), 0);
index = freeLocation - trackersOrder.begin();
trackersOrder.insert(trackersOrder.begin + index, id);
structArr[index] = new tracker;
structArr[index]->id = id;
structArr[index]->a[0] = a;
structArr[index]->b[0] = b;
structArr[index]->c = 0;
}
}
movement::~movement()
{
}
so when i send to method "addRow" id, and b i want to first check if i allready have this id in my vector (the vector just give me the index for the structs array) and if not then if put the id in the first empty place in the vector and on the structs array/
but from some reasin its look to me that the methid dont reconized the vector and the structs. can you help me understand why?
p.s - i can bet that i have more mistakes in my code, its my firs try with pointers and ect. (im comming from the good life in Matlab) so i will be happy to learn on them also
thank you very much!
The main problem
The problem is that in your code, trackersOrder is not a vector but an array of vectors:
vector<int> trackersOrder[MAX_TRACKERS] = {}; // array of MAXTRACKERS vectors !!
The solution
If you define it as simple vector, it should work better:
vector<int> trackersOrder;
If you want to set its size do it in the movement constructor:
movement::movement() : trackersOrder(MAX_TRACKERS)
{
}
Other issues
There is a case typo with an ID that should be id.
auto searchID = find(trackersOrder.begin(), trackersOrder.end(), id); // by the way auto is easier + ID corrected
There are a missing () after a begin whicn transforms unfortunately your iterator arithmetic into function pointer arithmetic (sic!!):
trackersOrder.insert(trackersOrder.begin() + index, id); // corrected
Finally, there are a couple of structArr that should be replaced by trackersArr.
The result does finally compile (online demo)
I'm a beginner (in C++, I'm coming from C (6 months experience)) and I'm trying to create a priority queue, but something is not working.
When I start the program and compile it, there are no errors. But there is nothing printed on the screen and the program crashes.
So here is the code:
PriorityQueue.h
using namespace std;
class PriorityQueue{
private:
struct pqentry_t{
string value;
float priority;
};
pqentry_t **_pqentry;
int _size;
int _next;
public:
PriorityQueue();
~PriorityQueue();
void insert(string value, float priority);
void printQueue();
};
PriorityQueue.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
#define SIZE 8
using namespace std;
PriorityQueue::PriorityQueue(){
_size = SIZE;
_next = 0;
_pqentry = new pqentry_t*[_size];
}
PriorityQueue::~PriorityQueue(){
delete[] _pqentry;
}
void PriorityQueue::insert(string value, float priority){
_pqentry[_next]->value = value; // this is probably not working
_pqentry[_next]->priority = priority;
_next++;
}
void PriorityQueue::printQueue(){
for (int i = 0; i < _next; i++){
cout << "Value: " << _pqentry[i]->value << endl
<< "Priority: " << _pqentry[i]->priority << endl;
}
cout << endl;
}
main.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
using namespace std;
int main()
{
PriorityQueue pq;
pq.insert("Banana", 23);
pq.printQueue();
}
I think, I know where the error is, in the PriorityQueue.cpp, here:
_pqentry[_next]->value = value;
_pqentry[_next]->priority = priority;
But I don't know what's wrong and I can't fix it. The compiler says there are no errors.
I hope, you can help me. Thanks in advance!
You did allocate the _pqentry member but you need to allocate each entry of this array as well, for example:
_pqentry[_next] = new pqentry_t;
before writing to it.
And do not forget to delete those :)
It looks like you are creating an array of pointers to pqentry_t in your constructor, but your insert method is expecting it to be an array of _pqentry structures themselves. You are not allocating space for the pqentry_t elements themselves and so when you try to dereference them in your insert method, the program crashes.
Try changing the definition of _pqentry in the class to pqentry_t *_pqentry and the allocation in the constructor to new pqentry_t[size]. This will allow your insert and printQueue methods to access the entries of _pqentry as they are written.
I have to make a class that will make arrays act like vectors. When I try and pass the class into the method into my main I get an error telling me that "[" and "]" are incorrect operators. I was wondering if I'm just completely doing this wrong or if it's just a simple mistake. Help is greatly appreciated. Here is my header file:
#ifndef PROGRAM5HEADER_H
#ifndef PROGRAM5HEADER_H
#define PROGRAM5HEADER_H
#include <string>
using namespace std;
class FloatArray
{
int *rep;
int _size;
public:
FloatArray(int sz=100):_size(sz)
{
rep=new int[sz];
}
~FloatArray()
{
delete [] rep;
}
int size() const
{
return _size;
}
FloatArray(const FloatArray& x)
{
copy(x);
}
void copy(const FloatArray& x)
{
_size == x.size();
rep=new int[_size];
for(int k=0;k<_size;k++)
rep[k]=x.rep[k];
}
};
#endif
and here is my main program
#include <iostream>
#include <string>
#include <cstdlib>
#include "program5header.h"
#include <cmath>
using namespace std;
int meanstd(FloatArray x, int& std)
{
int sx=0,sx2=0,mean;
for(int i=0;i<x.size();i++)
{
sx+=x[i];
sx2+=x[i]*x[i];
}
mean=sx/x.size();
std=sqrt(sx2/x.size()-mean*mean);
return mean;
}
int main()
{ int f;
cout<<"How big of an array would you like: "<<endl;
cin>>f;
FloatArray x(f);
}
There are a lot of issues with a lot of your implementation, I'd suggest doing some research on the subject. I'll touch on a few.
Firstly, you should make your FloatArray a templated class and allow for different types other than just int.
When you initialize a FloatArray x and then try to access it's underlying array through "[]" you are actually invoking the following:
x.operator[](...)
You haven't defined the '[]' operator on your FloatArray class so you are getting an error.
You need something similar to this:
int FloatArray.operator[](int index) {
assert(index < _size);
return _rep[index]
}
Your copy isn't doing what you want, it's not copying the size over to "this". It should look something similar to this:
void copy(const FloatArray& x)
{
_size = x._size;
rep=new int[_size];
for(int k=0;k<_size;k++)
rep[k]=x.rep[k];
}
However I would suggest not having a copy method and instead implement everything in your copy constructor.