Have been stuck with multiple functions defined and linker errors while trying to compile the program, not too sure how I even defined everything properly in the header.
The purpose of the program is to check the validity of two entries from the main.cpp/test file. I made a program which checks the entries from the test file.
CarInventory.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <string.h>
#include "CarInventory.h"
using namespace std;
using namespace sdds;
void CarInventory::resetInfo()
{
m_type = nullptr;
m_brand = nullptr;
m_model = nullptr;
m_year = 0;
m_code = 0;
m_price = 0;
}
CarInventory::CarInventory()
{
resetInfo();
}
CarInventory::CarInventory(const char* type, const char* brand, const char* model, int year, int code, double price)
{
resetInfo();
setInfo(type, brand, model, year, code, price);
}
CarInventory::CarInventory(const char* type, const char* brand, const char* model)
{
resetInfo();
setInfo(type, brand, model, 2022, 100, 1);
}
CarInventory::~CarInventory()
{
delete[] m_type;
delete[] m_brand;
delete[] m_model;
}
CarInventory& CarInventory::setInfo(const char* type, const char* brand, const char* model, int year, int code, double price)
{
if (type == nullptr || brand == nullptr || model == nullptr || year < 1990 || code < 100 || price < 0)
{
resetInfo();
}
else
{
delete[] m_type;
delete[] m_brand;
delete[] m_model;
m_type = new char[strlen(type) + 1];
strcpy_s(m_type, sizeof(type), type);
m_brand = new char[strlen(brand) + 1];
strcpy_s(m_brand, sizeof(brand), brand);
m_model = new char[strlen(model) + 1];
strcpy_s(m_model, sizeof(m_model), model);
m_year = year;
m_code = code;
m_price = price;
}
return *this;
}
bool CarInventory::isValid() const
{
return m_type != nullptr && m_brand != nullptr && m_model != nullptr && m_year >= 1990 && m_code >= 100 && m_price >= 0;
}
void CarInventory::printInfo() const
{
std::cout << "|" << std::setw(12) << m_type << "|" << std::setw(18) << m_brand << "|" << std::setw(18) << m_model << "|" << std::setw(6) << m_year << "|" << std::setw(6) << m_code << "|" << std::setw(11) << m_price << "|" << std::endl;
}
bool CarInventory::isSimilarTo(const CarInventory& car) const
{
return m_type == car.m_type && m_brand == car.m_brand && m_model == car.m_model && m_year == car.m_year;
}
namespace sdds {
bool find_similar(CarInventory car[], const int num_cars)
{
for (int i = 0; i < num_cars; i++)
{
for (int j = i + 1; j < num_cars; j++)
{
if (car[i].isSimilarTo(car[j]))
{
return true;
}
}
}
return false;
}
}
Header File
#ifndef CARINVENTORY_H
#define CARINVENTORY_H
#define _CRT_SECURE_NO_WARNINGS
namespace sdds {
class CarInventory {
char* m_type;
char* m_brand;
char* m_model;
int m_year;
int m_code;
double m_price;
void resetInfo();
public:
CarInventory();
CarInventory(const char* type, const char* brand, const char* model, int year, int code, double price);
CarInventory(const char* type, const char* brand, const char* model);
bool isSimilarTo(const CarInventory& car) const;
~CarInventory();
CarInventory& setInfo(const char* type, const char* brand, const char* model, int year, int code, double price);
void printInfo() const;
bool isValid() const;
bool find_similar(CarInventory car[], const int num_cars);
};
}
#endif
Test/main file
#include<iostream>
#include<iomanip>
#include "CarInventory.h"
#include "CarInventory.cpp"
using namespace std;
using namespace sdds;
int main()
{
const int num_cars = 6;
bool invalid_data = false;
CarInventory cars[num_cars] = {
{},
{"suv", "volvo", "xc90"},
{"Truck", "Ford", "F 150", 2021, 105, 55000},
{"Sedan", "BMW", "M550i", 2022, 101, 91000},
{"Truck", "Tesla", "Cybertruck", 2021, 102, 65000},
{"Sedan", "BMW", "M550i"}
};
if (cars[2].setInfo("SUV", "Volvo", "XC90", 2019, 109, 80000).isValid()) {
cout << "Information was set correctly!" << endl;
}
else {
cout << "Information was set incorrectly!" << endl;
}
if (cars[1].setInfo("SUV", "Volvo", "XC90", 1234, 1, 1).isValid()) {
cout << "Information was set correctly!" << endl;
}
else {
cout << "Information was set incorrectly!" << endl;
}
cout << setw(60) << "----- Car Inventory Information -----" << endl << endl;;
cout << "| Type | Brand | Model | Year | Code | Price |" << endl;
cout << "+------------+------------------+------------------+------+------+-----------+" << endl;
for (int i = 0; i < num_cars; i++) {
if (cars[i].isValid())
cars[i].printInfo();
else
invalid_data = true;
}
if (invalid_data) {
cout << endl;
cout << setfill('*') << setw(60) << "*" << endl;
cout << "* WARNING: There are invalid data in the inventory! *" << endl;
cout << setfill('*') << setw(60) << "*" << endl;
}
if (find_similar(cars, num_cars)) {
cout << endl;
cout << setfill('+') << setw(60) << "+" << endl;
cout << "+ WARNING: There are similar entries in the inventory! +" << endl;
cout << setfill('+') << setw(60) << "+" << endl;
}
return 0;
}
Compiler Error I've been getting
You need to remove #include "CarInventory.cpp"
from your main.cpp
file. It does not belong there.
Your project is already compiling CarInventory.cpp
into CarInventory.obj
and then linking that .obj
into the final .exe
. But you are asking the compiler to include all of CarInventory.cpp
's code into main.cpp
's code, which is compiled into main.obj
. You are getting errors about all of CarInventory
's methods that are being duplicated in both .obj
files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.