I have a problem with my code. In main(automoto.cpp) I have created a function that I would like to transfer to the file vehicles.cpp
This is how it looks:
automoto.cpp
#include "vehicles.h"
#include <iostream>
using namespace std;
void add_vehicle(Vehicles**&, int&);
void Loop(Vehicles**& v, int &size) { ////// is responsible for adding
char e_add;// = 'N';
do {
add_vehicle(v, size);
v[size - 1]->add();
cout << "Do you want to finish adding? Click Y/N: ";
cin >> e_add;
} while (e_add == 'N'|| e_add == 'n'); //while (p[size]->e_dod == "N" ||p[size]->e_dod == "n");
}
void print(Vehicles** vehicles, int size) {
std::cout << "====================================" << std::endl;
for (int i{ 0 }; i < size; i++)
std::cout << "Type: " << vehicles[i]->get_type() << " Brand: " << vehicles[i]->get_brand() << " Model: " << vehicles[i]->get_model() << " Price: " << vehicles[i]->get_price() << std::endl;
}
void delete_v(Vehicles**& vehicles, int& size) {
for (size_t i{ 0 }; i < size; i++)
delete vehicles[i];
delete[] vehicles;
size = 0;
vehicles = NULL;
}
void delete_v(Vehicles**& vehicles, int& size, int index) {
if (index < size) {
Vehicles** temp = new Vehicles * [size - 1];
short int j{ -1 };
for (size_t i{ 0 }; i < size; i++) {
if (i != index) {
j++;
temp[j] = vehicles[i];
}
}
delete[] vehicles;
--size;
vehicles = temp;
}
else
std::cout << "Memory free!" << std::endl;
}
int main()
{
Vehicles** veh;
int size{ 0 }, index{ 0 };
Loop(veh, size);
print(veh, size);
delete_v(veh,size,0);
print(veh, size);
delete_v(veh,size);
return 0;
}
void add_vehicle(Vehicles**& vehicle, int& size) {
Vehicles** temp = new Vehicles * [size + 1];
if (size == 0)
temp[size] = new Vehicles;
else {
for (int i{ 0 }; i < size; i++)
temp[i] = vehicle[i];
delete[] vehicle;
temp[size] = new Vehicles;
}
++size;
vehicle = temp;
}
vehicles.cpp
#include "vehicles.h"
#include <iostream>
using namespace std;
void Vehicles::set_amount(int x) { amount = x; }
void Vehicles::add()
{
cout << "ADDING VEHICLE..." << endl;
cout << "Enter the vehicle type:";
cin >> type;
cout << "Enter the vehicle brand: ";
cin >> brand;
cout << "Enter the vehicle model: ";
cin >> model;
cout << "Enter the vehicle price: ";
cin >> price;
}
vehicles.h
#include <iostream>
#include <cstdlib>
using namespace std;
class Vehicles
{
public:
string type;
string brand;
string model;
string e_add;
int amount;
int price;
void add();
int get_amount() { return amount; }
string get_type() { return type; }
string get_brand() { return brand; }
string get_model() { return model; }
int get_price() { return price; }
void set_amount(int x);
};
Getting to the point - how can I transfer all the created main function to the vehicles.cpp file so that the program compiles? I would be grateful for the ready code, as tomorrow I have a deadline to submit the code, and the lecturer wants to have each of these functions in the class file.
EDIT
What exactly do I mean - these are my files, after moving the function:
automoto.cpp(main)
#include <iostream>
using namespace std;
int main()
{
Vehicles** veh;
int size{ 0 }, index{ 0 };
Loop(veh, size);
print(veh, size);
delete_v(veh,size,0);
print(veh, size);
delete_v(veh,size);
return 0;
}
and my vehicles.cpp - where all the functions should be:
#include <iostream>
using namespace std;
void Vehicles::set_amount(int x) { amount = x; }
void Vehicles::add()
{
cout << "ADDING VEHICLE..." << endl;
cout << "Enter the vehicle type:";
cin >> type;
cout << "Enter the vehicle brand: ";
cin >> brand;
cout << "Enter the vehicle model: ";
cin >> model;
cout << "Enter the vehicle price: ";
cin >> price;
}
void Loop(Vehicles**& v, int& size) { ////// is responsible for adding
char e_add;// = 'N';
do {
add_vehicle(v, size);
v[size - 1]->add();
cout << "Do you want to finish adding? Click Y/N: ";
cin >> e_add;
} while (e_add == 'N' || e_add == 'n'); //while (p[size]->e_dod == "N" ||p[size]->e_dod == "n");
}
void print(Vehicles** vehicles, int size) {
std::cout << "====================================" << std::endl;
for (int i{ 0 }; i < size; i++)
std::cout << "Type: " << vehicles[i]->get_type() << " Brand: " << vehicles[i]->get_brand() << " Model: " << vehicles[i]->get_model() << " Price: " << vehicles[i]->get_price() << std::endl;
}
void delete_v(Vehicles**& vehicles, int& size) {
for (size_t i{ 0 }; i < size; i++)
delete vehicles[i];
delete[] vehicles;
size = 0;
vehicles = NULL;
}
void delete_v(Vehicles**& vehicles, int& size, int index) {
if (index < size) {
Vehicles** temp = new Vehicles * [size - 1];
short int j{ -1 };
for (size_t i{ 0 }; i < size; i++) {
if (i != index) {
j++;
temp[j] = vehicles[i];
}
}
delete[] vehicles;
--size;
vehicles = temp;
}
else
std::cout << "Memory free!" << std::endl;
}
void add_vehicle(Vehicles**& vehicle, int& size) {
Vehicles** temp = new Vehicles * [size + 1];
if (size == 0)
temp[size] = new Vehicles;
else {
for (int i{ 0 }; i < size; i++)
temp[i] = vehicle[i];
delete[] vehicle;
temp[size] = new Vehicles;
}
++size;
vehicle = temp;
}
As you can see, I am trying to call the specified function in main (automoto.cpp), but I get the error "identifier not found", error C3861 in Visual Studio
Aucun commentaire:
Enregistrer un commentaire