Error with pointer when using dynamic_cast to detect derived class - c++

I'd like to have some help on an issue I face.
I made an inheritance with polymorph program with class Shape and Circle (derived from shape). So I have some code like this
main.cpp
Shape* shape = new (nothrow) Shape[size]();
input_circle(shape);
show_circle_area(shape);
and a procedure in main.cpp too
void show_circle_area(Shape *mshape){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "CIRCLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Circle*> (mshape[i]))
cout << mshape[i].getWidth() << " " << mshape[i].getArea() << " " << mshape[i].getPerimeter() << endl;
}
when I run this program, I always got this error :
main2.cpp: In function 'void output_circle(Shape*)':
main2.cpp:66:39: error: cannot dynamic_cast '*(mshape + ((sizetype)(((unsigned int)i) * 40u)))' (of type 'class Shape') to type 'class Circle*' (source is not a pointer)
if (dynamic_cast<Circle*> (mshape[i]))
^
Anyone can help what should I do to fix this?
main.cpp (updated)
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <limits>
#include <typeinfo>
using namespace std;
const int size = 200;
int totshape = 0;
// INPUT FROM FILE
void input_circle(Shape* mshape[]){
ifstream file;
int i;
double r;
file.open("circle.txt");
while (file >> r){
Circle* crl = new (nothrow) Circle(r);
mshape[totshape]=crl;
totshape++;
}
file.close();
}
void input_rectangle(Shape* mshape[]){
ifstream file;
int i;
double w,h;
file.open("rectangle.txt");
while (file >> w >> h){
Rectangle* rec = new (nothrow) Rectangle(w,h);
mshape[totshape]=rec;
totshape++;
}
file.close();
}
void input_square(Shape* mshape[]){
ifstream file;
int i;
double s;
file.open("square.txt");
while (file >> s){
Square* sqr = new (nothrow) Square(s);
mshape[totshape]=sqr;
totshape++;
}
file.close();
}
//OUTPUT TO FILE
void output_circle(Shape *mshape[]){
int i;
ofstream file;
file.open("outcircle.txt");
file << "radius\tarea\tperimeter" << endl;
for (i=0;i<totshape;i++){
if (dynamic_cast<Circle*> (mshape[i]))
file << mshape[i]->getWidth() << "\t" << mshape[i]->getArea() << "\t" << mshape[i]->getPerimeter() << endl;
}
file.close();
}
void output_rectangle(Shape *mshape[]){
int i;
ofstream file;
file.open("outrectangle.txt");
file << "width\theight\tarea\tperimeter" << endl;
for (i=0;i<totshape;i++){
if (dynamic_cast<Rectangle*> (mshape[i]))
file << mshape[i]->getWidth() << "\t" << mshape[i]->getHeight() << "\t" << mshape[i]->getArea() << "\t" << mshape[i]->getPerimeter() << endl;
}
file.close();
}
void output_square(Shape *mshape[]){
int i;
ofstream file;
file.open("outsquare.txt");
file << "sisi\tarea\tperimeter" << endl;
for (i=0;i<totshape;i++){
if (dynamic_cast<Square*> (mshape[i]))
file << mshape[i]->getWidth() << "\t" << mshape[i]->getArea() << "\t" << mshape[i]->getPerimeter() << endl;
}
file.close();
}
//SORTING STL FOR AREA AND PERIMETER
bool sortByArea(Shape lhs[], Shape rhs[]) {
return lhs->getArea() < rhs->getArea();
}
bool sortByPerimeter(Shape lhs[], Shape rhs[]){
return lhs->getArea() < rhs->getArea();
}
//SHOW DATA SORT BY AREA
void show_shape_area(Shape *shape[]){
int i;
sort(shape,shape+totshape,sortByArea);
cout << "ALL SHAPE" << endl;
for (i=0;i<totshape;i++)
cout << shape[i]->getWidth() << " " << shape[i]->getWidth() << " " << shape[i]->getArea() << " " << shape[i]->getPerimeter() << endl;
}
void show_circle_area(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "CIRCLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Circle*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
void show_rectangle_area(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "RECTANGLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Rectangle*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getHeight() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
void show_square_area(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByArea);
cout << "SQUARE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Square*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
//SHOW DATA SORT BY PERIMETER
void show_shape_perimeter(Shape *shape[]){
int i;
sort(shape,shape+totshape,sortByPerimeter);
cout << "ALL SHAPE" << endl;
for (i=0;i<totshape;i++)
cout << shape[i]->getWidth() << " " << shape[i]->getWidth() << " " << shape[i]->getArea() << " " << shape[i]->getPerimeter() << endl;
}
void show_circle_perimeter(Shape *mshape[]){
int i;
//Shape * tempshape;
sort(mshape,mshape+totshape,sortByPerimeter);
cout << "CIRCLE" << endl;
for (i=0;i<totshape;i++)
//cout << "masuk for" << endl;
//tempshape=&mshape[i];
if (dynamic_cast<Circle*> (mshape[i])){
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
//cout << "masuk" << endl;
}
}
void show_rectangle_perimeter(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByPerimeter);
cout << "RECTANGLE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Rectangle*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getHeight() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
void show_square_perimeter(Shape *mshape[]){
int i;
sort(mshape,mshape+totshape,sortByPerimeter);
cout << "SQUARE" << endl;
for (i=0;i<totshape;i++)
if (dynamic_cast<Square*> (mshape[i]))
cout << mshape[i]->getWidth() << " " << mshape[i]->getArea() << " " << mshape[i]->getPerimeter() << endl;
}
//ADD DATA
void add_circle(Shape *mshape[]){
int input;
cout << endl << "Masukkan jari-jari : ";
while (!(cin >> input) || input < 0) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan jari-jari : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
Circle* crl = new (nothrow) Circle(input);
mshape[totshape]=crl;
totshape++;
}
void add_rectangle(Shape *mshape[]){
int inwidth, inheight;
cout << endl << "Masukkan panjang : ";
while (!(cin >> inwidth) || inwidth < 0) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan panjang : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
cout << endl << "Masukkan lebar : ";
while (!(cin >> inheight) || inheight < 0 || !(inheight < inwidth)) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan lebar : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
Rectangle* rec = new (nothrow) Rectangle(inwidth,inheight);
mshape[totshape]=rec;
totshape++;
}
void add_square(Shape *mshape[]){
int input;
cout << endl << "Masukkan sisi : ";
while (!(cin >> input) || input < 0) // <<< note use of "short circuit" logical operation here
{
cout << "Input tidak valid" << endl;
cout << "Masukkan sisi : ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // NB: preferred method for flushing cin
}
Square* sqr = new (nothrow) Square(input);
mshape[totshape]=sqr;
totshape++;
}
//DELETE DATA
//MAIN PROGRAM
int main(){
Shape* shape[size];
input_circle(shape);
input_rectangle(shape);
input_square(shape);
show_shape_area(shape);
show_shape_perimeter(shape);
show_circle_area(shape);
show_circle_perimeter(shape);
show_rectangle_area(shape);
show_rectangle_perimeter(shape);
show_square_area(shape);
show_square_perimeter(shape);
//add_circle(shape);
//show_circle_area(shape);
//add_rectangle(shape);
//show_rectangle_area(shape);
//add_square(shape);
//show_square_area(shape);
output_circle(shape);
output_rectangle(shape);
output_square(shape);
return 0;
}

Your problem is here:
mshape[totshape]=crl;
This assignment just copies the "Shape part" inside crl to mshape[totshape] thus mshape[totshape] is still a shape, not a circle.
In order to fix your problem, please use an array of Shape* pointers instead of Shape values:
Shape* shape[size]; // we should write this as size is a const
And, your input_***() functions:
void input_circle(Shape* mshape[]){
ifstream file;
int i;
double r;
file.open("circle.txt");
while (file >> r){
Circle* crl = new Circle(r);
mshape[totshape]=crl;
totshape++;
}
file.close();
}
Note that the function prototype is changed, please do it for other functions, and use mshape[i]->foo instead of mshape[i].foo
The casting now is: if (dynamic_cast<Circle*> (mshape[i])){
Don't forget to free memory before ending since we are using pointers:
for (int i = 0; i < totshape; i++) delete mshape[i];
This deleting would force you to make Shape's destructor be virtual:
class Shape {
public:
virtual ~Shape() {...}
}
Otherwise, Circle, Rectangle ... 's destructors will not be called.

Related

memory leaks in c++

#pragma once
#ifndef SDDS_GIFT_H
#define SDDS_GIFT_H
#include <iostream>
namespace sdds
{
const int MAX_DESC = 15;
const double MAX_PRICE = 999.999;
const int MAX_WRAP = 20;
struct Gift
{
char m_description[MAX_DESC];
double m_price;
int m_units;
int m_wrapLayers;
struct Wrapping* m_wrap;
};
struct Wrapping
{
char* m_pattern;
};
void gifting(char*);
void gifting(double&);
void gifting(int&);
bool wrap(Gift& theGift);
bool unwrap(Gift& theGift);
void gifting(Gift& theGift);
void display(const Gift& theGift);
}
#endif
<pre><code>
#include <iostream>
#include "Gift.h"
using namespace std;
namespace sdds
{
void gifting(char* m_description) // sending info
{
cout << "Enter gift description: ";
cin.width(MAX_DESC + 1);
cin >> m_description;
}
void gifting(double& m_price)
{
cout << "Enter gift price: ";
cin >> m_price;
while (m_price > MAX_PRICE || m_price < 0)
{
cout << "Gift price must be between 0 and " << MAX_PRICE << std::endl;
cout << "Enter gift price: ";
cin >> m_price;
}
}
void gifting(int& m_units)// gifting function
{
cout << "Enter gift units: ";
cin >> m_units;
while (m_units < 1)
{
cout << "Gift units must be at least 1" << std::endl;
cout << "Enter gift units: ";
cin >> m_units;
};
}
bool wrap(Gift& m_wrap) {
if (m_wrap.m_wrapLayers > 0) {
cout << "Gift is already wrapped!" << endl;
return false;
}
else {
cout << "Wrapping gifts..." << endl;
cout << "Enter the number of wrapping layers for the Gift: ";
cin >> m_wrap.m_wrapLayers;
while (m_wrap.m_wrapLayers < 1) {
cout << "Layers at minimum must be 1, try again." << endl;
cout << "Enter the number of wrapping layers for the Gift: ";
cin >> m_wrap.m_wrapLayers;
}
int i = 0;
m_wrap.m_wrap = new Wrapping[MAX_WRAP + 1];
for (i = 0; i < m_wrap.m_wrapLayers; i++) {
m_wrap.m_wrap->m_pattern = new char[MAX_WRAP + 1];
cout << "Enter wrapping pattern #" << i + 1 << ": ";
cin >> m_wrap.m_wrap->m_pattern;
} // I put struct in a structure
return true;
}
delete[]m_wrap.m_wrap;
m_wrap.m_wrap = nullptr;
}
bool unwrap(Gift& g_unwrap) // unwrap function
{
if (g_unwrap.m_wrapLayers > 0) {
cout << "Gift being unwrapped." << endl;
g_unwrap.m_wrapLayers = 0;
g_unwrap.m_wrap->m_pattern = nullptr;
return true;
}
else
{
cout << "Gift isn't wrapped! Can't unwrap." << endl;
return false;
}
}
void display(const Gift& theGift)
{
cout << "Gift Details:" << endl;
cout << " Description: " << theGift.m_description << endl;
cout << " Price: " << theGift.m_price << endl;
cout << " Units: " << theGift.m_units << endl;
if (theGift.m_wrap == nullptr) // this part seems like a problem
{
cout << "Unwrapped" << endl;
}
else
{
int i = 0;
cout << "Wrap Layers: " << theGift.m_wrapLayers << endl;
for (i = 0; i < theGift.m_wrapLayers; i++) {
cout << "Wrap #" << i + 1 << ": " << theGift.m_wrap[i].m_pattern << endl;
}
}
}
void gifting(Gift& gift) //last function
{
cout << "Preparing a gift..." << endl;
gifting(gift.m_description);
gifting(gift.m_price);
gifting(gift.m_units);
wrap(gift);
}
}
</code></pre>
/***********************************************************************
// Workshop 2: Dynamic Memory & Function Overloading
// Version 2.0
// Date 2020/05/05
// Author Michael Huang
// Description
// Tests Gift module and provides a set of TODOs to complete
// which the main focuses are dynamic memory allocation
//
/////////////////////////////////////////////////////////////////
***********************************************************************/
#include <iostream>
#include "Gift.h"
#include "Gift.h" // intentional
using namespace std;
using namespace sdds;
void printHeader(const char* title)
{
char oldFill = cout.fill('-');
cout.width(40);
cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');
cout.width(40);
cout << "" << endl;
cout.fill(oldFill);
}
<pre><code>
int main() {
Gift g1; // Unwrapped Gift
{
printHeader("T1: Checking Constants");
cout << "MAX_DESC: " << sdds::MAX_DESC << endl;
cout << "MAX_PRICE: " << sdds::MAX_PRICE << endl;
cout << "MAX_WRAP: " << sdds::MAX_WRAP << endl;
cout << endl;
}
{
printHeader("T2: Display Wrapped Gift");
gifting(g1.m_description);
gifting(g1.m_price);
gifting(g1.m_units);
cout << endl;
g1.m_wrap = nullptr;
g1.m_wrapLayers = 0;
display(g1);
cout << endl;
}
{
printHeader("T3: Wrap a gift");
if (wrap(g1))
cout << "Test succeeded!";
else
cout << "Test failed: wrapping didn't happen!" << endl;
cout << endl << endl;
}
{
printHeader("T4: Re-wrap a gift");
cout << "Attempting to rewrap the previous Gift: "
<< g1.m_description << endl;
if (wrap(g1) == false)
cout << "Test succeeded!";
else
cout << "Test failed: gift it's already wrapped, cannot wrap again!";
cout << endl << endl;
}
{
printHeader("T5: Unwrap a gift");
cout << "Attempting to unwrap the previous gift: "
<< g1.m_description << endl;
if (unwrap(g1))
cout << "Test succeeded!";
else
cout << "Test failed: you should be able to unwrap!";
cout << endl << endl;
}
{
printHeader("T6: Unwrap again");
cout << "Attempting to un-unwrap the previous gift: "
<< g1.m_description << endl;
if (!unwrap(g1))
cout << "Test succeeded!";
else
cout << "Test failed: you should not be able to unwrap again!";
cout << endl << endl;
}
Gift g2; // Unwrapped Gift
{
printHeader("T7: Prepare another gift");
g2.m_wrap = nullptr;
g2.m_wrapLayers = 0;
gifting(g2);
cout << endl;
display(g2);
cout << endl;
}
{
printHeader("T8: Unwrap the second gift");
unwrap(g2);
}
return 0;
}
Output matches perfectly but I don't know why memory leaks.. please help me. I am doubting my wrap part but I think there must be something else since deallocation seems fine.
I tried my best but I still cannot see which part is wrong.
I cannot see why my deallocation does not work I tried changing it so many times but nothing works.
On this line:
m_wrap.m_wrap->m_pattern = new char[MAX_WRAP + 1];
You allocate memory, but later you only:
delete[]m_wrap.m_wrap;
Also in your for loop you allocate memory, then get some input and store that inside the pointer, as a memory address. Should you ever dereference that, you will invoke undefined behavior, in practice that may likely will a segfault. You should consider rewriting at least that part from scratch.

C++ syntax for interaction between classes and struct

I'm working on an assignment in my first semester of C++ and I just can't figure out working syntax for it. I need to pass a struct as a parameter to a class function using a pointer. This code I've copied is my best attempt, and it will compile but it crashes when it asks for the first name. When I try variations in the syntax, I get errors about incomplete struct, undefined variables (warrior was or invalid operators. What am I doing wrong?
#include <iostream>
using namespace std;
class StarWars
{
public:
int totalNumber;
struct data_clone
{
int ID, timeCounter;
string name;
};
data_clone *warrior;
void createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
}
void input(struct data_clone *pointer, int total)
{
for(int i = 1; i <= total; i++)
{
cout << "For warrior number " << i << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> pointer[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> pointer[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> pointer[i].timeCounter;
}
}
void lifeSpan(struct data_clone *pointer, int total)
{
for(int i = 1; i <= total; i++)
{
cout << "Warrior number " << pointer[i].name << ": " << endl;
while(pointer[i].timeCounter > 0)
{
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is alive." << endl;
pointer[i].timeCounter--;
}
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
};
int main(void)
{
StarWars clones;
clones.createClone();
clones.input(clones.warrior, clones.totalNumber);
clones.lifeSpan(clones.warrior, clones.totalNumber);
}
You don't initialise the memory warrior points to, so you're working with uninitiailised memory - always a bad thing. There's two things to keep in mind here:
When working with pointers, always initialise the memory behind them first. Prefer using RAII concepts like std::unique_ptr / std::shared_ptr
void DoStuff(Widget* w);
std::unique_ptr<Widget> pw = std::make_unique<Widget>();
DoStuff(w.get());
When working with normal variables, use the dereference / reference operators to take a pointer to the variable.
void DoStuff(Widget* w);
Widget widget;
DoStuff(&w);
struct data_clone
{
int ID, timeCounter;
string name;
};
class StarWars
{
private:
data_clone *warrior;
int totalNumber;
public:
StarWars()
{
}
~StarWars()
{
delete[] warrior; // clean up
}
void createClone();
void input();
void lifeSpan();
};
void StarWars::createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
// construct structure baased on input
warrior = new data_clone[totalNumber];
}
void StarWars::input()
{
for(int i = 0; i < totalNumber; i++)
{
cout << "For warrior number " << i+1 << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> warrior[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> warrior[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> warrior[i].timeCounter;
}
}
void StarWars::lifeSpan()
{
std::cout<<"**********Print data**********\n";
for(int i = 0; i < totalNumber; i++)
{
cout << "Warrior number " << warrior[i].name << ": " << endl;
while(warrior[i].timeCounter > 0)
{
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is alive." << endl;
warrior[i].timeCounter--;
}
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
StarWars clones;
clones.createClone();
clones.input();
clones.lifeSpan();
return 0;
}
You never created your clones, you only read how many there should be.
Allocate space for them:
void createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
warrior = new data_clone[totalNumber];
}
Your array indexes are also off - an array of size N is indexed from 0 to N - 1.
But the more common way of handling this is to pass the amount to the constructor and let the object handle its own members:
class StarWars
{
public:
StarWars(int clones)
: totalNumber(clones),
warrior(new data_clone[clones])
{
}
void input()
{
for(int i = 0; i < totalNumber; i++)
{
cout << "For warrior number " << i << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> warrior[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> warrior[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> warrior[i].timeCounter;
}
}
void lifeSpan()
{
for(int i = 0; i < totalNumber; i++)
{
cout << "Warrior number " << i << ": " << endl;
while(warrior[i].timeCounter > 0)
{
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is alive." << endl;
warrior[i].timeCounter--;
}
cout << "Warrior name: " << warrior[i].name << endl;
cout << "Warrior ID number: " << warrior[i].ID << endl;
cout << "Warrior time counter: " << warrior[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
private:
int totalNumber;
struct data_clone
{
int ID, timeCounter;
string name;
};
data_clone *warrior;
};
int main()
{
int number = 0;
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> number;
StarWars clones(number);
clones.input();
clones.lifeSpan();
}
Note that you need to handle the destructor, the copy constructor, and the assignment operator properly as well (left as an exercise).
class StarWars
{
private:
struct data_clone
{
int ID, timeCounter;
string name;
};
public:
StarWars()
{
warrior = new data_clone[4];
}
~StarWars()
{
delete[] warrior;
}
int totalNumber;
data_clone *warrior;
void createClone()
{
cout << "How many clone warriors do you want to create in total?" << endl;
cin >> totalNumber;
}
void input(struct data_clone *pointer, int total)
{
for(int i = 0; i < total; i++)
{
cout << "For warrior number " << i << ":" << endl;
cout << "What is the warrior's name?" << endl;
cin >> pointer[i].name;
cout << "What is the warrior's ID number?" << endl;
cin >> pointer[i].ID;
cout << "What is the warrior's time counter?" << endl;
cin >> pointer[i].timeCounter;
}
}
void lifeSpan(struct data_clone *pointer, int total)
{
for(int i = 0; i < total; i++)
{
cout << "Warrior number " << pointer[i].name << ": " << endl;
while(pointer[i].timeCounter > 0)
{
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is alive." << endl;
pointer[i].timeCounter--;
}
cout << "Warrior name: " << pointer[i].name << endl;
cout << "Warrior ID number: " << pointer[i].ID << endl;
cout << "Warrior time counter: " << pointer[i].timeCounter << endl;
cout << "Clone is dead." << endl;
}
}
};
Note: I just hardcoded it which is not the correct way, it force you to enter only 4 tyeps of dataclone
"warrior = new data_clone[4];"
you will at least get your result

Must have pointer to object type c++ (Array)

I have a pointer to object error type for the variable "carrierTime" i have created. If i make this an array, carrierTime becomes an error in the first if statement, however if i leave it without any array i get an error on the last line of the code where i have used carrierTime in a multiplication.
can anyone help??
platform used:visual studios
#include "AMcore.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout << "Amplitude Modulation Coursework" << endl;
cout << "Name: Mohammad Faizan Shah" << endl;
cout << "Student ID: 5526734 \n\n\n" << endl;
std::ifstream file,file2;
string filename1,filename2;
int rowCounter = 0;
double informationTime;
double informationAmplitudeAmount[361];
long double carrierTime;
double carrierAmplitudeAmount[361];
double totalAmplitudeAmount[1000];
int plotPoint;
cout << "Please enter the filename of the Carrier wave \n" << endl;
cin >> filename1;
file.open("carrier.txt");
if (file.is_open())
{
file >> carrierTime;
while (!file.fail())
{
cout << "row" << setw(3) << rowCounter;
cout << " Time = " << setw(5) << carrierTime;
file >> carrierAmplitudeAmount[rowCounter];
rowCounter++;
if (!file.fail())
{
cout << " Carrier signal= " << setw(5) << carrierAmplitudeAmount;
file >> carrierTime;
}
cout << endl;
}
if (file.eof())
cout << "Reached the end of file marker" << endl;
else
cout << "Error whilst reading input file" << endl;
}
else
{
cout << "Error opening input file, ";
cout << "check carrier.txt exists in the current directory." << endl;
}
file.close();
cout << "\n\n" << endl;
cout << "Please enter the filename of the information wave \n\n\n" << endl;
cin >> filename2;
file2.open("information.txt");
if (file2.is_open())
{
file2 >> informationTime;
while (!file2.fail())
{
cout << "row" << setw(3) << rowCounter;
cout << " Time = " << setw(5) << informationTime;
file2 >> informationAmplitudeAmount[361];
rowCounter++;
if (!file2.fail())
{
cout << " Carrier signal= " << setw(5) << informationAmplitudeAmount;
file2 >> informationTime;
}
cout << endl;
}
if (file2.eof())
cout << "Reached the end of file marker" << endl;
else
cout << "Error whilst reading input file" << endl;
}
else
{
cout << "Error opening input file, ";
cout << "check carrier.txt exists in the current directory." << endl;
}
file.close();
cout << "Reading from txt file has completed" << endl << endl;
cout << "\n\n" << endl;
cout << "\n\n" << endl;
cout << "please enter number of sample points to plot:| \n" << endl;
do{
cin >> plotPoint;
if (plotPoint <= 361)
{
cout << "\n plotting the graph.\n" << endl;
}
else if (plotPoint > 361)
{
cout << "Value is too high.. Try value lower than 361\n" << endl;
}
} while (plotPoint > 361);
cout << "row" << setw(3) << rowCounter;
file >> carrierAmplitudeAmount[361];
rowCounter++;
plotPoint = 361 / plotPoint;
cout << " Time \| Amplitude Modulation plot\n------------+--------------------------------------------------\n";
totalAmplitudeAmount[0] = carrierAmplitudeAmount[0] * informationAmplitudeAmount[0];
cout << setw(6) << carrierTime << setw(4) << "\|" << setw(48) << "*" << totalAmplitudeAmount[0] << endl;
for (int i = 1; i <= 361; i = i + plotPoint) {
totalAmplitudeAmount[i] = informationAmplitudeAmount[i] * carrierAmplitudeAmount[i];
int y = totalAmplitudeAmount[i] * 22;
cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl;
}
cout << "End of program" << endl;
system("pause");
return 0;
}
cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl;
carrierTime[i++] does not look correct. The variable is not defined as a pointer.
Also, proper debugging would help you catch these errors for yourself.

Why I cannot access dynamic allocated memory in my for loop?

I new a memory for my child class type stock which is inherited from base class instrument, when I try to access the second element of my array, it throws error. Things are fine when I my new array size is 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Instrument{
public:
virtual void display(){}
virtual void output(){}
virtual void readFile(){}
virtual ~Instrument(){}
};
class Stock :
public Instrument{
public:
Stock(){
}
virtual void input(){
cout << "This is stock, please input its information: ";
cin >> name >> bidPrice >> askPrice >> lastPrice >> issueExchange;
}
virtual void display(){
cout <<"This is to display stock: "<< name << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< issueExchange << " "
<< endl;
}
virtual void output(){
ofstream myfile;
myfile.open("Stock.txt", ios::out | ios::app);
if (myfile.is_open()){
myfile << "This is a stock: "
<< name << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< issueExchange << " "
<< endl;
}
else cout << "Unable to open file";
}
virtual void readFile(){
string line;
ifstream myfile("Stock.txt");
cout << "\nThis is file stored\n";
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
}
virtual ~Stock(){}
private:
char name[13];
double bidPrice;
double askPrice;
double lastPrice;
int issueExchange;
};
int main(){
const int N = 5;//it works fine if I use N=1;
Instrument *pBase = NULL;
pBase = new Stock[N];
for (int i = 0; i < N; i++){
pBase[i].input();// here throws an exception and ends the program
pBase[i].display();
pBase[i].output();
}
pBase[N - 1].readFile();
delete[] pBase;
system("pause");
return 0;
}
Polymorphism and pointer arithmetic do not mix, because the arrangement of objects within an array depends on the most-derived size, and polymorphism loses that information. The dynamic allocation is a red herring, you can see the same problem with:
Derived array[2];
Base* p = array;
printf("%p\n", &array[0]);
printf("%p\n", p);
printf("%p\n", &array[1]);
printf("%p\n", p + 1);
printf("%z\n", sizeof (array[0]));
printf("%z\n", sizeof (*p));
Note that the pointer values using array are moving forward by sizeof (Derived), but pointer arithmetic using p is moving forward by sizeof (Base) and not finding the real objects.
Generally you would fix this using an array of Base*, instead of a single Base* combined with pointer arithmetic.
Base* pp[2];
for( auto& elem : array ) pp[&elem - array] = &elem;
printf("%p\n", &array[1]);
printf("%p\n", pp[1]);
// use (*pp[1]) or pp[1]->whatever
Another option is to use an object that remembers the original type:
Derived* allocated = new Derived[N];
std::function<Base& (int)> poly = [allocated](int i){ return allocated[i]; };
and use poly(i) instead of p[i]
But warning, you CANNOT do delete [] &poly(0); because delete[] is not polymorphic either.
Using std::unique_ptr<Derived[]> and std::bind, one could arrange for automatic deallocation when the accessor object finally goes out of scope.
Although Mr.Ben's method is absolutely right, but I totally feel that his C++ and my C++ are not the same language, his is mixing some strange things here, thus according to his idea, I tried to modify my code like this.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class Instrument{
public:
virtual void display() = 0;
virtual void output() = 0;
virtual void readFile() = 0;
virtual ~Instrument(){};
};
class Stock :
public Instrument{
public:
Stock(){
cout << "This is stock, please input its information: ";
cin >> name >> bidPrice >> askPrice >> lastPrice >> issueExchange;
}
virtual void display(){
cout << "This is to display stock: " << name << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< issueExchange << " "
<< endl;
}
virtual void output(){
ofstream myfile;
myfile.open("Stock.txt", ios::out | ios::app);
if (myfile.is_open()){
myfile << "This is a stock: "
<< name << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< issueExchange << " "
<< endl;
}
else cout << "Unable to open file";
}
virtual void readFile(){
string line;
ifstream myfile("Stock.txt");
cout << "\nThis is file stored\n";
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
}
virtual ~Stock(){}
private:
string name;
double bidPrice;
double askPrice;
double lastPrice;
int issueExchange;
};
class Option :
public Instrument{
public:
Option(){
cout << "This is option, please input its information: ";
cin >> name >> uname >> bidPrice >> askPrice >> lastPrice >> contractSize >> exp;
}
virtual void display(){
cout << "This is to display option: "
<< name << " "
<< uname << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< contractSize << " "
<< exp << " "
<< endl;
}
virtual void output(){
ofstream myfile;
myfile.open("Option.txt", ios::out | ios::app);
if (myfile.is_open()){
myfile << "This is an option: "
<< name << " "
<< uname << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< contractSize << " "
<< exp << " "
<< endl;
}
else cout << "Unable to open file";
}
virtual void readFile(){
string line;
ifstream myfile("Option.txt");
cout << "\nThis is file stored\n";
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
}
virtual ~Option(){}
private:
string name;
string uname;
double bidPrice;
double askPrice;
double lastPrice;
int contractSize;
double exp;
};
class Future :
public Instrument{
public:
Future(){
cout << "This is option, please input its information: ";
cin >> name >> uname >> bidPrice >> askPrice >> lastPrice >> contractSize >> tickSize >> contractMonth;
}
virtual void display(){
cout << "This is to display option: "
<< name << " "
<< uname << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< contractSize << " "
<< tickSize << " "
<< contractMonth << " "
<< endl;
}
virtual void output(){
ofstream myfile;
myfile.open("Future.txt", ios::out | ios::app);
if (myfile.is_open()){
myfile << "This is a future: "
<< name << " "
<< uname << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< contractSize << " "
<< tickSize << " "
<< contractMonth << " "
<< endl;
}
else cout << "Unable to open file";
}
virtual void readFile(){
string line;
ifstream myfile("Future.txt");
cout << "\nThis is file stored\n";
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
}
virtual ~Future(){}
private:
string name;
string uname;
double bidPrice;
double askPrice;
double lastPrice;
int contractSize;
int tickSize;
int contractMonth;
};
int main(){
int N = 20;
//shared_ptr<Instrument> pBase[N];
vector<shared_ptr<Instrument>> pBase(N);
int i = 5;
for (i = 0; i < N; i++) pBase[i] = make_shared<Stock>();
for (i = 0; i < N; i++){
pBase[i]->display();
pBase[i]->output();
}
pBase[N - 1]->readFile();
for (i = 0; i < N; i++) pBase[i] = make_shared<Option>();
for (i = 0; i < N; i++){
pBase[i]->display();
pBase[i]->output();
}
pBase[N - 1]->readFile();
for (i = 0; i < N; i++) pBase[i] = make_shared<Future>();
for (i = 0; i < N; i++){
pBase[i]->display();
pBase[i]->output();
}
pBase[N - 1]->readFile();
system("pause");
return 0;
}
tl;dr answer: don't convert subclass array (Stock[N]) to a base class pointer (pBase). Either directly use Stock*, or create an array of pointers instead:
auto arr = new Stock*[N];
for (int i = 0; i < N; i++) {
arr[i] = new Stock();
}
// unrelated but suggested: C++11 unique_ptr is recommended:
vector<unique_ptr<Stock>> v(N);
Detailed reasons:
1) Array bracket operator is a syntactic sugar: a[b] -> *(a + b);
2) Pointer arithmetic is not polymorphic, it's always based on static types:
pBase[i] -> *(pBase+i) -> *(pBase*)((char*)pBase + sizeof(Instrument) * i);
3) This is, however, what you want:
pBase[i] -> *(pBase+i) -> *(pBase*)((char*)pBase + sizeof(Stock) * i);
4) As long as sizeof(Instrument) != sizeof(Stock), you are in trouble.

voids not being read

I'm working on an Arena-ish console game. I want it so that I can have a void to make you fight certain monsters, instead of having to the monster fight out every time. Here's my code:
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;
char select;
int randnum;
int level=1;
int maxhp=10;
int hp=maxhp;
int mhp;
int exp;
int dmg;
int m_maxhp;
int req_exp=10;
int day;
char boot;
int a;
ifstream leveli;
ofstream levelo;
ifstream playerhpi;
ofstream playerhpo;
ifstream expi;
ofstream expo;
ifstream maxhpi;
ofstream maxhpo;
ifstream req_expi;
ofstream req_expo;
ifstream dayi;
ofstream dayo;
void wait( time_t delay )
{
time_t timer0, timer1;
time( &timer0 );
do {
time( &timer1 );
} while (( timer1 - timer0 ) < delay );
}
// This is the void that's not being called
void bat()
{
m_maxhp=10;
mhp=m_maxhp;
do{
dmg= rand() % 4 + 1;
cout << "Batt attacks you for " << dmg;
hp=hp-dmg;
cout << ", leaving you with [" << hp << "/" << maxhp << "]" << endl;
wait(1);
if(hp<=0)
{
cout << "You've lose. Try harder next time.";
exp=exp/2;
day=day+1;
dayo.open("daynumber.dat");
dayo << day;
dayo.close();
}
else if(mhp<=0)
{
cout << "You have killed the Bat. You gain 6 EXP." << endl;
exp=exp+6;
day=day+1;
dayo.open("daynumber.dat");
dayo << day;
dayo.close();
expo.open("experience.dat");
expo << exp;
expo.close();
}
}while(mhp>0);
}
int main()
{
leveli.open("level.dat");
leveli >> level;
leveli.close();
playerhpi.open("health.dat");
playerhpi >> hp;
playerhpi.close();
expi.open("experience.dat");
expi >> exp;
expi.close();
maxhpi.open("maxhealth.dat");
maxhpi >> maxhp;
maxhpi.close();
req_expi.open("req_exp.dat");
req_expi >> req_exp;
req_expi.close();
cout << "Data successfully loaded. Start the game now? y/n" << endl;
boot=_getch();
if(boot=='n')
{
cout << "Exiting..." << endl;
wait(3);
exit(0);
}
else if(boot=='y'){
do{
if (exp==req_exp)
{
level=level+1;
cout << "Level Up! You are now level " << level << "!" << endl;
exp=0;
req_exp=req_exp*1.5;
req_expo.open("req_exp.dat");
req_expo << req_exp;
req_expo.close();
levelo.open("level.dat");
levelo << level;
levelo.close();
}
else{
cout << endl << "Day " << day << " in The Arena." << endl << "1. Fight" << endl << "2. Stats" << endl << "3. Full Heal" << endl << "4. Half Heal" << endl;
select=_getch();
if(select=='1')
{
srand((unsigned)time(0));
randnum = rand() % 500 + 1;
// cout << "*DEBUG* " << randnum << endl;
// This part doesn't work
if(randnum<300 && level<4)
{
cout << "You've been chosen to fight a Bat!" << endl;
wait(1.5);
void bat();
}
else
{
}
}
else if(select=='2')
{
cout << endl << "Health: [" << hp << "/" << maxhp << "]" << endl << "Level: [" << level << "]" << endl << "Experience: " << "[" << exp << "/" << req_exp << "]" << endl;
wait(0.5);
}
else
{
cout << "Invalid Command";
}
}
}while (boot=='y');
}
return 0;
}
Am I using voids incorrectly? If so, could someone point out what I should change?
if(randnum<300 && level<4)
{
cout << "You've been chosen to fight a Bat!" << endl;
wait(1.5);
void bat(); // ?
}
What is expected behavior here? void bat(); is a function declaration. This just introduces the name bat and does nothing else. It doesn't call a function named bat. In case you need just to call bat, you should write:
bat(); // a call to "bat"
When you call the function, you omit the return type, e.g. your code:
// This part doesn't work
if(randnum<300 && level<4)
{
cout << "You've been chosen to fight a Bat!" << endl;
wait(1.5);
void bat();
...
Should read:
// This part doesn't work
if(randnum<300 && level<4)
{
cout << "You've been chosen to fight a Bat!" << endl;
wait(1.5);
bat();
...