'AMA::MyProduct': cannot instantiate abstract class - c++

I am working on my school project that has a base class and a derived class with some other classes.
Product.h
#ifndef AMA_PRODUCT_H
#define AMA_PRODUCT_H
#include <iostream>
#include <fstream>
namespace AMA
{
const int max_sku_length = 7;
const int max_unit_length = 10;
const int max_name_length = 75;
const double TRate = 0.13;
class Product
{
private:
char m_type;
char m_sku[max_sku_length +1];
char m_unit[max_unit_length + 1];
char* m_name;
int m_Cquantity;
int m_Nquantity;
double m_price;
bool m_status;
protected:
void sku(const char* setSku) { strncpy(m_sku, setSku, max_sku_length); }
const char* name() const { return m_name; }
bool taxed() const { return m_status; }
const char* sku() const { return m_sku;}
void name(const char*);
const char* unit() const;
double price() const;
void message(const char*);
bool isClear() const;
public:
double cost() const;
bool operator==(const char* src) { return strcmp(m_sku, src) == 0; }
bool isEmpty() const { return ((m_sku[0] == '\0') && (m_name == nullptr) && (m_price == 0) && (m_Cquantity == 0)); }
int qtyNeeded() const { return m_Nquantity; }
int quantity() const { return m_Cquantity; }
int operator+=(int src) { return m_Cquantity += src; }
Product();
Product(const char* sku, const char* name, const char* unit, int qty = 0,
bool taxed = true, double price = 0.0, int qtyNeeded = 0);
Product(const Product&);
Product& operator=(const Product&);
~Product();
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
double total_cost() const;
void quantity(int);
bool operator>(const char*) const;
bool operator>(const Product&) const;
};
std::ostream& operator<<(std::ostream&, const Product&);
std::istream& operator>>(std::istream&, Product&);
double operator+=(double&, const Product&);
}
#endif
MyProduct.h
#ifndef AMA_MY_PRODUCT_H
#define AMA_MY_PRODUCT_H
#include <fstream>
#include "Product.h"
#include "ErrorState.h"
namespace AMA {
class MyProduct : public Product {
public:
MyProduct();
MyProduct(const char* sku, const char* name, const char* unit, int qty = 0,
bool isTaxed = true, double price = 0.0, int qtyNeeded = 0);
const char* sku() const;
const char* name() const;
const char* unit() const;
bool taxed() const;
double price() const;
double cost() const;
};
class Test {
MyProduct product; // Error
const char* filename;
public:
Test(const char* file);
Test(const char* file, const char* theSku, const char* theName);
std::fstream& store(std::fstream& file, bool addNewLine = true) const;
std::fstream& load(std::fstream& file);
std::ostream& write(std::ostream& os, bool linear) const;
std::istream& read(std::istream& is);
int operator+=(int value);
bool operator==(const char* sku) const;
friend std::ostream& operator<<(std::ostream& os, const Test& test);
friend double operator+=(double& d, const Test& test);
friend std::istream& operator>>(std::istream& is, Test& test);
};
}
#endif
MyProduct.cpp
#include <iomanip>
#include <fstream>
#include <cstring>
#include "MyProduct.h"
#ifdef TAB
#undef TAB
#endif
#define TAB '\t'
using namespace std;
namespace AMA {
MyProduct::MyProduct() : Product("", "", "") {}
MyProduct::MyProduct(const char* sku, const char* name, const char* unit, int qty,
bool isTaxed, double price, int qtyNeeded) :
Product(sku, name, unit, qty, isTaxed, price, qtyNeeded) {}
const char* MyProduct::sku() const { return Product::sku(); }
const char* MyProduct::name() const { return Product::name(); }
const char* MyProduct::unit() const { return Product::unit(); }
bool MyProduct::taxed() const { return Product::taxed(); }
double MyProduct::price() const { return Product::price(); }
double MyProduct::cost() const { return Product::cost(); }
Test::Test(const char* file) : filename(file) { }
Test::Test(const char* file, const char* theSku, const char* theName) :
product(theSku, theName, ""), filename(file) { }
std::fstream& Test::store(std::fstream& file, bool addNewLine) const {
if (!product.isEmpty()) {
file.open(filename, ios::out | ios::app);
file << product.sku() << TAB << product.name() << TAB << product.unit() << TAB <<
(product.taxed() ? 1 : 0) << TAB << product.price() << TAB << product.quantity() << TAB <<
product.qtyNeeded() << endl;
file.clear();
file.close();
}
return file;
}
std::fstream& Test::load(std::fstream& file) {
char sku_[max_sku_length + 1];
char name[max_name_length + 1];
char unit[max_unit_length + 1];
int quantity, qtyNeeded;
double price_;
char taxed_;
file.open(filename, ios::in);
file >> sku_;
file >> name;
file >> unit;
file >> taxed_;
file >> price_;
file >> quantity;
file >> qtyNeeded;
file.clear();
file.close();
product = MyProduct(sku_, name, unit, quantity, taxed_ != 0, price_, qtyNeeded); //ERROR
return file;
}
std::ostream& Test::write(std::ostream& os, bool linear) const {
return product.isEmpty() ? os : (os << product.sku() << ": " << product.name() << ", quantity: "
<< product.quantity() << ", quantity needed:" << product.qtyNeeded()
<< ", Cost: " << fixed << setprecision(2) << product.cost());
}
std::istream& Test::read(std::istream& is) {
char sku_[max_sku_length + 1];
char name[max_name_length + 1];
char unit[max_unit_length + 1];
int quantity, qtyNeeded;
double price_;
char taxed_;
cout << " Sku: ";
is >> sku_;
cout << " Name (no spaces): ";
is >> name;
cout << " Unit: ";
is >> unit;
cout << " Taxed? (y/n): ";
is >> taxed_;
cout << " Price: ";
is >> price_;
cout << " Quantity On hand: ";
is >> quantity;
cout << " Quantity Needed: ";
is >> qtyNeeded;
product = MyProduct(sku_, name, unit, quantity, taxed_ != 0, price_, qtyNeeded); //ERROR
return is;
}
int Test::operator+=(int value) {
product.quantity(product += value);
return product.quantity();
}
bool Test::operator==(const char* sku) const {
return !std::strcmp(product.sku(), sku);
}
std::ostream& operator<<(std::ostream& os, const Test& test) {
return test.product.write(os, true);
}
double operator+=(double& d, const Test& test) {
return d += test.product.total_cost();
}
std::istream& operator>>(std::istream& is, Test& test) {
return test.product.read(is);
}
}
I am getting errors that I don't really know how to fix
object of abstract class type "AMA::MyProduct" is not allowed
'AMA::MyProduct': cannot instantiate abstract class
a cast to abstract class "AMA::MyProduct" is not allowed
Can someone help me out please?

The errors are self explanatory.
AMA::MyProduct is an abstract class, and such it cannot be instantiated directly.
A class is abstract when it has at least 1 abstract method (virtual and = 0 keywords on it). An abstract method MUST be overridden in a derived class, and you MUST instantiate that derived class, not the abstract class.
AMA::MyProduct is abstract because it does not override the 4 abstract methods it inherits from AMA::Product:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
You implemented them in AMA::Test instead of in AMA::MyProduct.

Related

Set and get methods in a class for objects

I have a class Result which takes two other classes UNIT and Date as object parameters, so that I can store the values in them for use in those classes. I will put down what Ive got so far, and a few of the errors I get are as follows.
error: prototype for 'int Result::GetUnit() const' does not match any in class 'Result'|
lab2\Result.h|17|error: candidate is: Result Result::GetUnit() const|
lab2\Result.cpp|66|error: prototype for 'int Result::GetDate()' does not match any in class 'Result'|
lab2\Result.h|18|error: candidate is: Result Result::GetDate() const|
lab2\Result.cpp|73|error: 'SetResult' was not declared in this scope|
error: 'SetResult' was not declared in this scope|
For the error not declared in this scope, after researching I understand that I need to define the functions before the first call is made. I have done that in the .cpp file but I still get the error. What am I doing wrong?
Result.h:
#ifndef RESULT_H
#define RESULT_H
#include <iostream>
#include <string>
#include "UNIT.h"
#include "Date.h"
//const unsigned ResultSize = 10;
using namespace std;
class Result
{
public:
Result(){};
Result(UNIT unitobj1, unsigned marks1, Date dateobj1);
Result GetUnit() const;
Result GetDate() const;
void SetDate(Date dateobj1);
void SetUnit(UNIT unitonj1);
void SetMarks( unsigned marks1 );
unsigned GetMarks() const;
void SetCredits( unsigned cred );
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetResult(istream & input);
void GetResult(ostream & os);//unsigned GetUnit() const;
private:
UNIT unitobj;
Date dateobj;
string id;
int credits;
unsigned marks;
};
inline unsigned Result::GetCredits() const
{
return credits;
}
ostream & operator <<( ostream & os, const Result & S);
istream & operator >>( istream & input, Result & S);
#endif // RESULT_H
Result.cpp:
#include "Result.h"
#include "UNIT.h"
#include "Date.h"
Result::Result(UNIT unitobj1, unsigned marks1, Date dateobj1)
{
unitobj = unitobj1;
marks = marks1;
dateobj = dateobj1;
}
void Result::SetResult(istream &input){
UNIT unitobj1;
unsigned marks1;
Date date1;
input >> unitobj1 >> marks1 >> date1;
SetUnit(unitobj1);
SetMarks(marks1);
SetDate(date1);
}
void Result::GetResult(ostream &os){
os << GetUnit() < " Marks: " << GetMarks() << '\n' << GetDate() << '\n';
}
void Result::SetUnit(UNIT unitobj1){
unitobj = unitobj1;
}
void Result::SetMarks(unsigned marks1){
marks = marks1;
}
void Result::SetDate(Date dateobj1){
dateobj = dateobj1;
}
Result::GetUnit() const{
return unitobj;
}
inline unsigned Result::GetMarks() const{
return marks;
}
Result::GetDate(){
return dateobj;
}
istream & operator >>( istream & input, Result & S)
{
SetResult(input);
return input;
}
ostream & operator <<( ostream & os, const Result & S)
{
GetResult(os);
return os;
}
Date.h:
#if !defined(_DATE_H)
#define _DATE_H
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date();
Date(unsigned day1, string month1, unsigned year1);
void SetDay(unsigned day1);
void SetMonth(string month1);
void SetYear(unsigned year1);
unsigned GetDay() const;
string GetMonth() const;
unsigned GetYear() const;
void SetDate(istream &input);
void GetDate(ostream & os);
private:
unsigned day;
string month;
unsigned year;
};
ostream & operator <<(ostream & os, const Date & D);
istream & operator >>(istream & input, Date & D);
#endif //_DATE_H
Date.cpp:
//
//
// Generated by StarUML(tm) C++ Add-In
#include "Date.h"
Date::Date(unsigned day1, string month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
void Date::SetDay(unsigned day1) {
day = day1;
}
void Date::SetMonth(string month1) {
month = month1;
}
void Date::SetYear(unsigned year1) {
year = year1;
}
inline unsigned Date::GetDay() const {
return day;
}
string Date::GetMonth() const {
return month;
}
inline unsigned Date::GetYear() const {
return year;
}
void Date::SetDate(istream &input){
unsigned day1;
string month1;
unsigned year1;
input >> day1 >> month1 >> year1;
SetDay(day1);
SetMonth(month1);
SetYear(year1);
}
void Date::GetDate(ostream &os){
os << " Date: " << GetDay() << " " << GetMonth() << " " << GetYear();
}
istream & operator >>( istream & input, Date & D) {
SetDate(input);
return input;
}
ostream & operator <<( ostream & os, const Date & D) {
GetDate(os);
return os;
}
UNIT.h:
#ifndef UNIT_H
#define UNIT_H
#include <iostream>
#include <string> // C string library
using namespace std;
const unsigned UnitNameSize = 10;
class UNIT
{
public:
UNIT();
UNIT( string nam, string idd, unsigned cred);
void SetName(string nam);
string GetName() const;
void SetMarks(unsigned marks1);
unsigned GetMarks();
void SetCredits(unsigned cred);
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetUnit(istream & input);
void GetUnit(ostream & os);
private:
string name;
string id;
unsigned marks;
int credits;
};
ostream & operator <<( ostream & os, const UNIT & U);
istream & operator >>( istream & input, UNIT & U);
#endif // UNIT_H
UNIT.cpp:
#include "UNIT.h"
#include <string>
UNIT::UNIT()
{
name[0] = '\0';
}
void UNIT::SetName(string nam)
{
name = nam;
}
string UNIT::GetName() const
{
return name;
}
void UNIT::SetID(string idd)
{
id = idd;
}
string UNIT::GetID() const
{
return id;
}
void UNIT::SetCredits(unsigned cred){
credits = cred;
}
inline unsigned UNIT::GetCredits() const{
return credits;
}
UNIT::UNIT( string nam, string idd,
unsigned cred)
{
name.replace(0, 10, nam );
id = idd;
credits = cred;
}
void UNIT::SetUnit(istream &input){
string nam;
string idd;
unsigned cred;
getline(input,nam, '\n');
getline(input,idd,'\n');
input >> cred;
SetName(nam);
SetID(idd);
SetCredits(cred);
}
void UNIT::GetUnit(ostream & os){
os << " Unit ID: " << GetID() << '\n'
<< " Unit Name: " << GetName() << '\n'
<< " Credits: " << GetCredits() << '\n';
}
istream & operator >>( istream & input, UNIT & U)
{
SetUnit(input);
return input;
}
ostream & operator <<( ostream & os, const UNIT & U)
{
GetUnit(os);
return os;
}
Note: I know that I can just make the overloaded operators methods of the class and declare them as friends, but I am trying to achieve this by using set and get methods. Any help would be very appreciated!
So, this is pretty straightforward, look at Result::GetUnit
Result::GetUnit() const {
return unitobj;
}
This method is missing a return type. Now look at unitobj
UNIT unitobj;
So it's clear that the return type should be UNIT, so the above method should be defined as
UINT Result::GetUnit() const {
return unitobj;
}
Now look at how you declared this method in your class
class Result
{
...
Result GetUnit() const;
Here you gave it a return type of Result where we've already seen that the return type should be UINT, so change the above to
class Result
{
...
UNIT GetUnit() const;
Result::GetDate has similar problems, here the return type should be Date but again you specified it as Result.
For the SetResult error you simply need to specify that you are calling the SetResult method on a Result object. The compiler thinks you are calling a global SetResult function (which doesn't exist)
Like this
istream & operator >>( istream & input, Result & S)
{
S.SetResult(input);
return input;
}
S.SetResult(input); tells the compiler that you want to call the SetResult method using the Result object refered to by S.
You can tell this by reading the error message carefully, it said 'SetResult' was not declared in this scope, not 'Result::SetResult' was not declared in this scope. As you said you have declared Result::SetResult, but your code wasn't calling that, it was trying to call a different function called SetResult and the compiler correctly reported that no such function was declared.

Is this approach change the object of the name only?

I want Swap the name (i.e. string in cName[]) of the two cats using the pointer approach.
However I want to Only swap the name, NOT the object.
Am I correct?
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>
using namespace std;
class CAT
{
public:
CAT(char * firstname) { strncpy(cName, firstname, 79); }
~CAT() { ; }
char * getName() { return cName; }
void setName(char *nameinput) { strncpy(cName, nameinput, 79); }
private:
char cName[80];
};
void nameSwap(CAT *CatA, CAT *CatB)
{
char testing[] = "testing";
CAT temp =CAT(testing);
temp = *CatA;
*CatA = *CatB;
*CatB = temp;
}
int main()
{
char Taby[] = "Taby";
char Felix[] = "Felix";
CAT pA = CAT(Taby);
CAT pB = CAT(Felix);
cout << "The inital name pA is " << pA.getName() << " and pA is" << pB.getName() << endl;
nameSwap(&pA, &pB);
cout << "After approach" << endl;
cout << "The name pA is " << pA.getName() << " and " << pB.getName() << endl;
system("PAUSE");
return 0;
}
You are actually swapping the whole objects, not only the name of the CAT.
If you only want to swap the name, you need to access the cName member in a similar way as you are doing for the objects. You'd also need permission to access to the cName member in such a swap function, which a function outside won't have since cName is private. Make the swap function a member of your class:
class CAT
{
public:
CAT(const char* firstname) { strncpy(cName, firstname, 80); }
~CAT() {}
const char* getName() const { return cName; }
void setName(const char *nameinput) { strncpy(cName, nameinput, 80); }
void swapName(CAT& CatB)
{
char tmp[80];
strncpy(tmp, CatB.cName, 80);
strncpy(CatB.cName, cName, 80);
strncpy(cName, tmp, 80);
}
private:
char cName[80];
// other CAT attributes won't be affected by the name swap
};
And call it like this
pA.swapName(pB); // or pB.swapName(pA); - same result
But consider using std::string instead of char[]. You'll soon find C++ strings much easier to work with and also, when swapping those, only the pointers to the underlying memory is swapped, so it's more effective.
std::string one;
std::string two;
one.swap(two);
Edit: As per request, I added a version using pointers.
I made it in a haste and haven't debugged it so I've probably made a lot of mistakes. First, I made a new class called wong_string that will hold the name and any other attributes suiteable for strings.
#include <stdexcept>
#include <cstring>
class wong_string {
char* m_data;
static char* duplicate(const char* str) {
size_t len = std::strlen(str)+1;
char* rv = new char[len];
std::memcpy(rv, str, len);
return rv;
}
public:
// default constructor: wong_string howdy1;
wong_string() : m_data(nullptr) {}
// conversion constructor: wong_string howdy2("value2");
wong_string(const char* cstr) :
m_data(duplicate(cstr))
{}
// copy constructor: wong_string howdy3 = howdy2;
wong_string(const wong_string& rhs) : wong_string(rhs.m_data) {}
// move constructor: wong_string howdy4 = wong_string("value4");
wong_string(wong_string&& rhs) : m_data(rhs.m_data) {
rhs.m_data = nullptr;
}
// copy assignment operator: (wong_string howdy5;) howdy5 = howdy4;
wong_string& operator=(const wong_string& rhs) {
if(this!=&rhs) {
char* tmp = duplicate(rhs.m_data);
if(m_data) delete []m_data;
m_data = tmp;
}
return *this;
}
// copy assignment operator from c string
wong_string& operator=(const char* rhs) {
*this = wong_string(rhs);
return *this;
}
// move assignment operator: (wong_string howdy6;) howdy6 = wong_string("value6");
wong_string& operator=(wong_string&& rhs) {
if(this!=&rhs) {
m_data = rhs.m_data;
rhs.m_data = nullptr;
}
return *this;
}
// destructor, free memory allocated by duplicate(), if any
~wong_string() {
if(m_data) delete []m_data;
}
// comparisons
bool operator==(const wong_string& rhs) const {
return strcmp(m_data, rhs.m_data)==0;
}
bool operator!=(const wong_string& rhs) const {
return !(*this==rhs);
}
// conversion to a normal c string
operator char const* () const { return m_data; }
// output stream operator
friend std::ostream& operator<<(std::ostream&, const wong_string&);
// input stream operator - not implemented yet
};
with that in place, your CAT can be made into something like this:
class CAT
{
public:
CAT(const char* firstname, const char* nickname=nullptr) :
cName(firstname),
cNickName(nickname?nickname:firstname)
{}
~CAT() {}
const char* getName() const { return cName; }
void setName(const char *nameinput) { cName=nameinput; }
void swapName(CAT& CatB)
{
std::swap(cName, CatB.cName);
}
private:
wong_string cName; // Madame Florence Jenkins III
// other CAT attributes won't be affected by the name swap
wong_string cNickName; // Ms. Miao
};
So, there you have it. Pointers galore...

Operator overloading >> cin

I need to overload the << and >> operators, but I don't see any errors.
When i overloaded only operator << and initialize the object in main directly.
I don't receive any error. But when i overloaded operator >>, I don't see any pop up to enter the variables for the object.
Here is the code and I don't seem to find what's wrong with it
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
class vehicle
{
protected:
int number, year, cost;
char *make, *model, *bodyStyle, *color;
public:
vehicle(int = 0, int = 0, char* = NULL, char* = NULL, char* = NULL, char* = NULL, int = 0);
~vehicle();
friend istream &operator>>(istream& stream, vehicle& v);
friend ostream &operator<<(ostream& stream, const vehicle& v);
};
vehicle::vehicle(int a, int b, char* ma1, char* mod1, char* bs1, char* c1, int cos)
{
number = a;
year = b;
make = new char[strlen(ma1) + 1];
assert(make);
strcpy (make,ma1);
model = new char[strlen(mod1) + 1];
assert(model);
strcpy(model,mod1);
bodyStyle = new char[strlen(bs1) + 1];
assert(bodyStyle);
strcpy(bodyStyle,bs1);
color = new char[strlen(c1) + 1];
assert(color);
strcpy(color,c1);
cost = cos ;
}
vehicle::~vehicle()
{
delete[] make;
delete[] model;
delete[] bodyStyle;
delete[] color;
}
istream& operator>>(istream& stream, vehicle& v)
{
stream>>v.number;
stream>>v.year;
stream>>v.cost;
stream>>v.make;
stream>>v.model;
stream>>v.bodyStyle;
stream>>v.color;
return stream;
}
ostream& operator<<(ostream& stream, const vehicle& v)
{
stream<<v.number<<endl;
stream<<v.year<<endl;
stream<<v.cost<<endl;
stream<<v.make<<endl;
stream<<v.model<<endl;
stream<<v.bodyStyle<<endl;
stream<<v.color<<endl;
return stream;
}
class Truckvehicle : public vehicle
{
int passengers, mileage, grossWeight, tempGross;
char *poweredBy;
public:
Truckvehicle(int = 0, int = 0, char* = NULL, char* = NULL, char* = NULL, char* = NULL, int = 0, int = 0, int = 0, int = 0, char * = NULL, int = 0);
~Truckvehicle();
friend ostream & operator<<(ostream& stream, const Truckvehicle& tv);
friend istream & operator>>(istream& stream, Truckvehicle& tv);
};
Truckvehicle::Truckvehicle(int a, int b, char* ma1, char* mod1, char* bs1, char* c1, int cos, int pass1, int mil, int gross, char* pb, int tg) :
vehicle(a, b, ma1, mod1, bs1, c1, cos), passengers(pass1), mileage(mil), grossWeight(gross), poweredBy(pb), tempGross(tg)
{
passengers = pass1;
mileage = mil;
grossWeight = gross;
poweredBy = new char[strlen(pb) + 1];
assert(poweredBy);
strcpy(poweredBy, pb);
tempGross = tg;
}
Truckvehicle :: ~Truckvehicle()
{
delete[] poweredBy;
}
istream& operator>>(istream& stream, Truckvehicle& tv)
{
stream>>tv.number;
stream>>tv.year;
stream>>tv.make;
stream>>tv.model;
stream>>tv.bodyStyle;
stream>>tv.color;
stream>>tv.cost;
stream>>tv.passengers;
stream>>tv.mileage;
stream>>tv.grossWeight;
stream>>tv.poweredBy;
stream>>tv.tempGross;
return stream;
}
ostream & operator<<(ostream& stream, const Truckvehicle& tv)
{
stream<<tv.number<<endl;
stream<<tv.year<<endl;
stream<<tv.make<<endl;
stream<<tv.model<<endl;
stream<<tv.bodyStyle<<endl;
stream<<tv.color<<endl;
stream<<tv.cost<<endl;
stream<<tv.passengers<<endl;
stream<<tv.mileage<<endl;
stream<<tv.grossWeight<<endl;
stream<<tv.poweredBy<<endl;
stream<<tv.tempGross<<endl;
return stream;
}
int main()
{
Truckvehicle TV;
//Read in the following values for TV (111111, 2014,"Toyota","Tacoma", "4X4", "Black",25000, 2, 15000,333333,"Gas", 222222);
cin>>TV;
cout<<TV;
return 0;
}
Hi there you might want to study this code, which compiles without errors and warnings in Wandbox using GCC HEAD 8 compiler. I will answer any questions about my code in a timely fashion.
Regards, Micha
#include <iostream>
// Don't use <blah.h>, prefer using <cblah>.
#include <cstring>
#include <cassert>
// extra includes.
#include <string>
#include <sstream>
using namespace std;
class vehicle
{
friend ostream& operator<< (ostream& stream, vehicle const& v);
friend istream& operator>> (istream& stream, vehicle& v);
protected:
int number, year, cost;
char *make, *model, *bodyStyle, *color;
public:
vehicle(int = 0,
int = 0,
char const* = "-", // Don't choose NULL here - prevents segmentation fault in main!
char const* = "-",
char const* = "-",
char const* = "-",
int = 0);
~vehicle();
};
vehicle::vehicle(int a,
int b,
char const* ma1,
char const* mod1,
char const* bs1,
char const* c1,
int cos)
{
number = a;
year = b;
make = new char[strlen(ma1) + 1];
assert(make);
strcpy(make, ma1);
model = new char[strlen(mod1) + 1];
assert(model);
strcpy(model, mod1);
bodyStyle = new char[strlen(bs1) + 1];
assert(bodyStyle);
strcpy(bodyStyle, bs1);
color = new char[strlen(c1) + 1];
assert(color);
strcpy(color, c1);
cost = cos;
}
vehicle::~vehicle()
{
if (make) delete[] make;
if (model) delete[] model;
if (bodyStyle) delete[] bodyStyle;
if (color) delete[] color;
}
ostream& operator<< (ostream& stream, vehicle const& v)
{
stream << v.number << endl;
stream << v.year << endl;
stream << v.cost << endl;
stream << v.make << endl;
stream << v.model << endl;
stream << v.bodyStyle << endl;
stream << v.color << endl;
return stream;
}
istream& operator>> (istream& stream, vehicle& v)
{
stream >> v.number;
stream >> v.year;
stream >> v.cost;
stream >> v.make;
stream >> v.model;
stream >> v.bodyStyle;
stream >> v.color;
return stream;
}
class Truckvehicle : public vehicle
{
friend ostream& operator<< (ostream& stream, Truckvehicle const& tv);
friend istream& operator>> (istream& stream, Truckvehicle& tv);
int passengers, mileage, grossWeight, tempGross;
char *poweredBy;
public:
Truckvehicle(int = 0,
int = 0,
char const* = "-",
char const* = "-",
char const* = "-",
char const* = "-",
int = 0,
int = 0,
int = 0,
int = 0,
char const* = "-",
int = 0);
~Truckvehicle();
};
Truckvehicle::Truckvehicle(int a,
int b,
char const* ma1,
char const* mod1,
char const* bs1,
char const* c1,
int cos,
int pass1,
int mil,
int gross,
char const* pb,
int tg)
:
vehicle(a, b, ma1, mod1, bs1, c1, cos)
{
passengers = pass1;
mileage = mil;
grossWeight = gross;
poweredBy = new char[strlen(pb) + 1];
assert(poweredBy);
strcpy(poweredBy, pb);
tempGross = tg;
}
Truckvehicle::~Truckvehicle()
{
delete[] poweredBy;
}
ostream& operator<< (ostream& stream, Truckvehicle const& tv)
{
// vehicle part.
stream <<tv.number <<endl;
stream <<tv.year <<endl;
stream <<tv.make <<endl;
stream <<tv.model <<endl;
stream <<tv.bodyStyle <<endl;
stream <<tv.color <<endl;
stream <<tv.cost <<endl;
// extra truck part.
stream <<tv.passengers <<endl;
stream <<tv.mileage <<endl;
stream <<tv.grossWeight <<endl;
stream <<tv.poweredBy <<endl;
stream <<tv.tempGross <<endl;
return stream;
}
istream& operator>> (istream& stream, Truckvehicle& tv)
{
// vehicle part.
stream >>tv.number;
stream >>tv.year;
stream >>tv.make;
stream >>tv.model;
stream >>tv.bodyStyle;
stream >>tv.color;
stream >>tv.cost;
// extra truck part.
stream >>tv.passengers;
stream >>tv.mileage;
stream >>tv.grossWeight;
stream >>tv.poweredBy;
stream >>tv.tempGross;
return stream;
}
// Simulates usage of std::operator>> ().
void initializeTruckvehicle(Truckvehicle& TV,
int a,
int b,
char const* ma1,
char const* mod1,
char const* bs1,
char const* c1,
int cos,
int pass1,
int mil,
int gross,
char const* pb,
int tg)
{
string s;
s += static_cast <ostringstream *> (&( ostringstream() << a )) -> str();
s += " ";
s += static_cast <ostringstream *> (&( ostringstream() << b )) -> str();
s += " ";
s += ma1;
s += " ";
s += mod1;
s += " ";
s += bs1;
s += " ";
s += c1;
s += " ";
s += static_cast <ostringstream *> (&( ostringstream() << cos )) -> str();
s += " ";
s += static_cast <ostringstream *> (&( ostringstream() << pass1 )) -> str();
s += " ";
s += static_cast <ostringstream *> (&( ostringstream() << mil )) -> str();
s += " ";
s += static_cast <ostringstream *> (&( ostringstream() << gross )) -> str();
s += " ";
s += pb;
s += " ";
s += static_cast <ostringstream *> (&( ostringstream() << tg )) -> str();
s += " ";
istringstream in(s);
in >> TV;
}
int main()
{
cout << "Truck 1>" << endl;
Truckvehicle TV1 (111111,
2014,
"Toyota",
"Tacoma",
"4X4",
"Black",
25000,
2,
15000,
333333,
"Gas",
222222);
cout << TV1;
cout << "Truck 2>" << endl;
Truckvehicle TV2;
cout << TV2;
cout << "Truck 3>" << endl;
Truckvehicle TV3;
initializeTruckvehicle (TV3,
111111,
2014,
"Toyota",
"Tacoma",
"4X4",
"Black",
25000,
2,
15000,
333333,
"Gas",
222222);
cout << TV3;
}

0xC0000005: Access violation writing location 0xCCCCCCCC caused by trying to make safe empty chars

I've been pulling my hair out over a certain error that seems to be plaguing my program. I've attempted to search online for cases similar to mine but I can't seem to find a way to apply the other solutions to this problem. My issue is as follows: When I initially open the program it immediately stops responding and crashes. Debugging led me to find the error in question is "0xC0000005: Access violation writing location 0xCCCCCCCC". It seems to be tied to me assigning two attributes (sku_ and name_ ) as '\0'. If I change these values to anything else such as "" or even "\0" the program runs in visual studio, but will fail to compile elsewhere. Could someone help me understand where I am going wrong?
Product.h
#ifndef SICT_Product_H__
#define SICT_Product_H__
#include "general.h"
#include "Streamable.h"
#include <cstring>
namespace sict {
class Product : public Streamable {
char sku_ [MAX_SKU_LEN + 1];
char* name_;
double price_;
bool taxed_;
int quantity_;
int qtyNeeded_;
public:
//Constructors
Product();
Product(const char* sku, const char* name1, bool taxed = true, double price = 0, int qtyNeeded =0);
Product(Product& g);
~Product();
//Putter Functions
void sku(const char* sku) { strcpy(sku_,sku); };
void price(double price) {price_ = price;};
void name(const char* name);
void taxed(bool taxed) { taxed_ = taxed; };
void quantity(int quantity) { quantity_ = quantity; };
void qtyNeeded(int qtyNeeded) { qtyNeeded_ = qtyNeeded; };
//Getter functions
const char* sku() const { return sku_; };
double price() const { return price_; };
const char* name() const { return name_; };
bool taxed() const { return taxed_; };
int quantity() const { return quantity_; };
int qtyNeeded() const { return qtyNeeded_; };
double cost() const;
bool isEmpty() const;
Product& operator=(const Product& );
bool operator==(const char* );
int operator+=(int );
int operator-=(int );
};
double operator+=(double& , const Product& );
std::ostream& operator<<(std::ostream& os, const Product& );
std::istream& operator>>(std::istream& is, Product& );
}
#endif
Product.cpp
#include <iostream>
#include <cstring>
#include "Product.h"
namespace sict {
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0';
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
Product::Product(const char* sku, const char* name1, bool taxed1, double price1, int qtyNeeded1) {
strncpy(sku_, sku, MAX_SKU_LEN);
name(name1);
quantity_ = 0;
taxed(taxed1);
price(price1);
qtyNeeded(qtyNeeded1);
}
double Product::cost() const {
if (taxed_ == true) {
return (price_ * TAX) + price_;
}
else
return price_;
}
bool Product::isEmpty() const{
if (sku_ == nullptr && name_ == nullptr && quantity_ == 0 && price_ == 0 && qtyNeeded_ == 0) {
return true;
}
else
return false;
}
Product::Product(Product& ex) {
sku(ex.sku_);
price(ex.price_);
name(ex.name_);
taxed(ex.taxed_);
quantity(ex.quantity_);
qtyNeeded(ex.qtyNeeded_);
}
Product& Product::operator=(const Product& g) {
sku(g.sku_);
price(g.price_);
name(g.name_);
taxed(g.taxed_);
quantity(g.quantity_);
qtyNeeded(g.qtyNeeded_);
return *this;
}
Product::~Product() {
delete [] name_;
}
void Product::name(const char* name) {
name_ = new char [strlen(name) + 1];
strcpy(name_, name);
}
bool Product::operator==(const char* right) {
if (sku_ == right) {
return true;
}
else
return false;
}
int Product::operator+=(int g) {
quantity_ = quantity_ + g;
return quantity_;
}
int Product::operator-=(int g) {
quantity_ = quantity_ - g;
return quantity_;
}
double operator+=(double& p, const Product& right) {
p = p + (right.cost() * right.quantity());
return p;
}
std::ostream& operator<<(std::ostream& os, const Product& g) {
return g.write(os, true);
}
std::istream& operator>>(std::istream& is, Product& g) {
return g.read(is);
}
}
The General.h file referenced in the header is just a list of constant values such as the "TAX" and "MAX_SKU_LEN" values.
The Streamable header contains pure virtual functions. I will list it here in case it is needed.
Streamable.h
#ifndef SICT__Streamable_H_
#define SICT__Streamable_H_
#include <iostream>
#include <fstream>
#include "Product.h"
namespace sict {
class Streamable {
public:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
};
}
#endif
Thank you very much in advance.
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0'; // <---- writing via unitialized pointer
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
There's one possible source of your problems. You have defined a char pointer (a dynamic array or a C-string, that is) name_ in your class but you never allocate any memory for it in your constructor, before attempting to record a value via the pointer. Naturally, you get a write access violation.
Before assigning the value to char at index [0] you need to first allocate space for at least one element in your string, e.g. by doing name_ = new char [1]. Alternatively, you may choose to initialize the pointer itself to NULL (or nullptr) and use that to indicate that name_ has not yet been set.

Segmentation fault: 11 and malloc errors in C++ code

Ok, so I know there are probably a lot of errors in this code. I'm pretty new to dynamic memory allocation, pointers, etc.
The header file, account.h, is given to us by our professor. We were told not to make any changes to the .h file.
The implementation file is written by me. The main function is included just for basic initial testing. We were given another file to actually test the implementation of the account class.
If I don't comment out the cout name line, I get a seg fault 11 error.
If I do, it'll print the account number, but throw this error:
Test(29976) malloc: * error for object 0x62c1aa18c9d8374: pointer being freed was not allocated* set a breakpoint in malloc_error_break to debug
Abort trap: 6
Any help at all would be greatly appreciated!
Here's the header file:
class account
{
public:
typedef char* string;
static const size_t MAX_NAME_SIZE = 15;
// CONSTRUCTOR
account (char* i_name, size_t i_acnum, size_t i_hsize);
account (const account& ac);
// DESTRUCTOR
~account ( );
// MODIFICATION MEMBER FUNCTIONS
void set_name(char* new_name);
void set_account_number(size_t new_acnum);
void set_balance(double new_balance);
void add_history(char* new_history);
// CONSTANT MEMBER FUNCTIONS
char* get_name ( ) const;
size_t get_account_number ( ) const;
double get_balance( ) const;
size_t get_max_history_size( ) const;
size_t get_current_history_size ( ) const;
string* get_history( ) const;
friend ostream& operator <<(ostream& outs, const account& target);
private:
char name[MAX_NAME_SIZE+1]; //name of the account holder
size_t ac_number; //account number
double balance; //current account balance
string *history; //Array to store history of transactions
size_t history_size; //Maximum size of transaction history
size_t history_count; //Current size of transaction history
};
Here is the implementation file:
// File: account.cxx
// Author: Mike Travis
// Last Modified: Mar 3, 2012
// Description: implementation of Account class as prescribed by the file account.h
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "account.h"
using namespace std;
//Constructor
account::account(char* i_name, size_t i_acnum, size_t i_hsize){
string *d_history;
d_history = new string[i_hsize];
for(int i = 0; i<i_hsize; i++){
name[i] = i_name[i];
}
ac_number = i_acnum;
history_size = i_hsize;
history_count = 0;
}
account::account(const account& ac){
string *d_history;
d_history = new string[ac.history_size];
for( int i=0; i<ac.get_current_history_size(); i++){
strcpy(d_history[i], history[i]);
}
strcpy(name,ac.get_name());
ac_number = ac.get_account_number();
history_size = ac.get_max_history_size();
history_count = ac.get_current_history_size();
}
account::~account(){ delete [] history; }
void account::set_name(char* new_name){ strcpy(name, new_name); }
void account::set_account_number(size_t new_acnum){ ac_number = new_acnum; }
void account::set_balance(double new_balance){ balance = new_balance; }
void account::add_history(char* new_history){
strcpy(history[history_count], new_history);
history_count++;
}
char* account::get_name() const {
char* name_cpy;
strcpy(name_cpy, name);
return name_cpy;
}
size_t account::get_account_number() const{ return ac_number; }
double account::get_balance() const{ return balance; }
size_t account::get_max_history_size() const{ return history_size; }
size_t account::get_current_history_size() const{ return history_count; }
//string* account::get_history() const{ return *history; }
int main(){
account test1("mike travis", 12345, 20);
//cout<<"\nname: "<< test1.get_name();
cout<<"\n\nacnum: "<<test1.get_account_number()<<"\n\n";
return 0;
}
In the destructor of account, you delete the history array. However, in the constructor, you allocate (and leak) an array which is stored in the local variable d_history. You presumably wanted to assign that to the member variable history instead - since you haven't, if you get to the destructor it gives you an error saying that you're freeing history but have never allocated it.
There's a similar error in the copy constructor as well.
There are also other errors in your code as well, which I assume you'll find as you go - get_name(), for example, is not going to work. I suspect the header file is not helping here, but there's not much to be done if you're not supposed to change that.
I've written a little bit code for you and corrected the epic mistakes (even in the header file, sorry ;)). It is still extremely ugly and c-ish, but maybe you can learn something reading it:
#include <cstddef>
#include <ostream>
class account
{
// The whole class makes no sense, since it has no useful
// member function or anything like this.
// Furthermore, the class provides almost full access to all its member variables.
// At this point one could just make everything public.
// This is not even exception safe when the constructor throws.
// A good implementation would use history_entry and history classes,
// together with std::string and std::vector/std::deque
// And it would provide some sort of functionality. ;)
public:
account(const char* name, unsigned number, std::size_t history_max_size);
account(const account& other);
~account();
const char* name() const;
unsigned number() const;
double balance() const;
const char* const* history() const;
std::size_t history_size() const;
unsigned history_max_size() const;
void set_name(const char* new_name);
void set_number(unsigned new_number);
void set_balance(double new_balance);
void add_history(const char* new_history);
private:
char* name_;
unsigned number_;
double balance_;
char** history_;
std::size_t history_size_;
const std::size_t history_max_size_;
};
std::ostream& operator << (std::ostream& stream, const account& a);
#include <cassert>
#include <cstring>
account::account(const char* name, unsigned number, std::size_t history_max_size)
: name_(0)
, number_(number)
, balance_(0.0)
, history_(new char*[history_max_size])
, history_size_(0)
, history_max_size_(history_max_size)
{
assert(name != 0);
assert(history_max_size != 0);
set_name(name);
}
account::account(const account& other)
: name_(0)
, number_(other.number_)
, balance_(other.balance_)
, history_(new char*[other.history_max_size_])
, history_size_(other.history_size_)
, history_max_size_(other.history_max_size_)
{
set_name(other.name_);
for (std::size_t i = 0; i != other.history_size_; ++i)
{
history_[i] = new char[std::strlen(other.history_[i]) + 1];
strcpy(history_[i], other.history_[i]);
}
}
account::~account()
{
delete[] name_;
for (std::size_t i = 0; i != history_size_; ++i)
delete[] history_[i];
delete[] history_;
}
const char* account::name() const
{
return name_;
}
unsigned account::number() const
{
return number_;
}
double account::balance() const
{
return balance_;
}
const char* const* account::history() const
{
return history_;
}
std::size_t account::history_size() const
{
return history_size_;
}
unsigned account::history_max_size() const
{
return history_max_size_;
}
void account::set_name(const char* new_name)
{
if (name_)
delete[] name_;
name_ = new char[std::strlen(new_name) + 1];
std::strcpy(name_, new_name);
}
void account::set_number(unsigned new_number)
{
number_ = new_number;
}
void account::set_balance(double new_balance)
{
balance_ = new_balance;
}
void account::add_history(const char* new_history)
{
if (history_size_ == history_max_size_)
{
delete[] history_[0]; // delete oldest entry
for (std::size_t i = 0; i != history_size_ - 1; ++i)
history_[i] = history_[i + 1];
--history_size_;
}
history_[history_size_] = new char[strlen(new_history) + 1];
std::strcpy(history_[history_size_], new_history);
++history_size_;
}
std::ostream& operator << (std::ostream& stream, const account& a)
{
return stream << "account [name: " << a.name() << ", number: "
<< a.number() << ", balance: " << a.balance() << ']';
}
#include <iostream>
int main()
{
account a("Hello!", 500, 5);
a.set_balance(12.546);
for (int i = 50; i--; )
a.add_history("Yaaay..");
//account b = a;
std::cout << a << '\n';
}