Error running classes [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello Im taking some C++ studies right now and Im seem to be stuck in Classes. I have made this program where I need to get the price of a slice of pizza. Simply I dont want to input data yet, just with static data in that is already in place.
Code is this :
#include <iostream>
#include <cmath>
using namespace std;
class Circle
{
public:
Circle()
{
radius = 0;
area = 0;
}
inline void setRadius(double r)
{
radius = r;
}
inline double getArea(double radius)
{
return 3.14 * pow(radius, 2);
}
private:
double radius;
double area;`
};
class Pizza
{
private:
double price;
double size;
double costperinch;
Circle Object;
public:
Pizza()
{
price = 0;
size = 0;
costperinch = 0;
}
~Pizza();
inline void setPrice(double p)
{
price = p;
}
inline void setSize(double radius)
{
size = Object.getArea(radius);
}
inline double costPeSqIn(double size, double price)
{
double costperinch = size * price;
}
};
int main()
{
Pizza myPizza;
myPizza.setPrice(5.0);
myPizza.setSize(3.14);
cout << "The cost per square inch of the pizza is ";
cout << myPizza.costPeSqIn(myPizza.setSize, myPizza.setPrice);
return 0;
}
I get the following errors:
Error 1 error C3867: 'Pizza::setSize': function call missing argument
list; use '&Pizza::setSize' to create a pointer to
member c:\users\jorge\documents\visual studio 2013\projects\object
composition\object composition\main 7.21.cpp 91 1 Object Composition
Error 2 error C3867: 'Pizza::setPrice': function call missing argument
list; use '&Pizza::setPrice' to create a pointer to
member c:\users\jorge\documents\visual studio 2013\projects\object
composition\object composition\main 7.21.cpp 91 1 Object Composition

Your problem is the line
cout << myPizza.costPeSqIn(myPizza.setSize, myPizza.setPrice);
at the end of your code. Your compiler complains because setSize and setPrice are functions but you didn't provide them with arguments. I doubt that you wanted to do this in the first place any-ways.
I actually think you wanted to have:
inline double costPeSqIn()
{
return size * price;
}
as implementation of your costPeSqIn function and the line above with the error should be
cout << myPizza.costPeSqIn();
it makes way more sense that way to me.
The way you currently pass the setter functions into costPeSqIn doesn't make sense, also your original costPeSqIn declared a double return type but didn't return anything.
As for the problem with the destructor, you got:
~Pizza();
and therefore got your destructor declared. Once you declare a destructor yourself the compiler won't auto generate one for you, but since you don't have a definition anywhere either your code is ill formed.
To fix this you either gotta provide a definition of it, e.g.:
~Pizza()
{
// whatever code you want
}
or delete the declaration so that the compiler will auto-generate a default constructor for you.

Related

Trying to delete a dynamically allocated object causes a run-time crash [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm working on optimizing my current CLI project. When looking through the code and debugging I've noticed that I haven't released some dynamically allocated memory in a class. Here's how my code looks:
"Coordinates.h"
#pragma once
#include <Windows.h>
#define LENGTH 40
#define WIDTH 15
struct Coordinate
{
int x = 1;
int y = 1;
};
"Laser.h"
#pragma once
#include "Coordinates.h"
class Laser
{
private:
Coordinate* initCoord;
char icon;
public:
Laser(int x, int y);
char getIcon() const;
Coordinate* getCoord();
void move();
};
"Laser.cpp"
#include "Laser.h"
Laser::Laser(int x, int y)
{
initCoord = new Coordinate;
initCoord->x = x;
initCoord->y = y;
icon = '~';
}
char Laser::getIcon() const { return icon; }
Coordinate* Laser::getCoord() { return initCoord; }
void Laser::move()
{
++initCoord->x;
}
I've tried adding a destructor (declaring it in the header file first of course) which clears up the memory allocated for initCoord which looked something like this:
Laser::~Laser()
{
if(initCoord != nullpr) delete initCoord;
}
After adding that it caused a run-time error. "ProgramName.exe has stopped working..." Objects of this class are stored in a simple vector which gets cleared at one time of the program. The problem is that the crash happens before it even reaches the lasers.clear() line. I honestly have no idea why this crash is happening and would appreciate some help. Thank you! :)
If you consider this code
int main() {
Laser one(0,0);
{
Laser two = one;
cout << two.getCoord()->x << endl;
}
return 0;
}
What do you expect to be written?
"0"
which mean that the coordinate points to the same structure as one which again means that when the first } happens and two gets destroyed one doesn't have a valid coordinate any more.
When you have a pointer member you need to either disable copying/assigning or implement them.
In this case you could also have been rescued if you had used a std::unique_ptr instead of the raw pointer (which also saves you the delete).

Errors with classes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am creating a program, these are the requirements:
We are going to write a program to figure out if several elements are boiling or melting.
Substance---Melting Point---- Boiling Point
Zinc(Zn)--- (787.15°F)--------(1665°F)
Barium(Ba)- (1341°F)----------(3353°F)
Mercury(Hg)-(-37.89°F)------- (674.11°F)
Uranium(U)--(2070°F)----------( 7468°F)
Design a class that stores a temperature in a temperature member variable and has the appropriate accessor and mutator functions.
In addition to appropriate constructors, the class should have the following member functions for each of the elements:
• isZincMelting. This function should return the bool value true if the temperature stored in the temperature field is at or above the melting point and below the boiling point of zinc. Otherwise, the function should return false.
• isZincBoiling. This function should return the bool value true if the temperature stored in the temperature field is at or above the boiling point of zinc. Otherwise, the function should return false.
• Similarly you should have isBariumMelting, isBariumBoiling
• Similarly you should have isMercuryMelting, isMercuryBoiling
• Similarly you should have isUraniumMelting, isUraniumBoiling
Write a program that demonstrates the class.
The program should ask the user to enter a temperature, and then display a list of the substances that will melt at that temperature and those that will boil at that temperature.
For example, if the temperature is 1764 the class should report:
Zinc boils, Barium melts, Mercury boils and Uranium is solid
--This is what I have so far and I am getting some errors.
*In function 'int main()':
69:21: error: no matching function for call to 'Elements::isZincMelting()'
69:21: note: candidate is:
51:6: note: bool Elements::isZincMelting(float)
51:6: note: candidate expects 1 argument, 0 provided
In member function 'bool Elements::isZincMelting(float)':
56:1: warning: control reaches end of non-void function [-Wreturn-type]
Any help is appreciated. Thank you
#include<iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
class Elements
{
private:
float temp;
public:
//float getTemp() const; //Get user temp
void setTemp(float);
bool isZincMelting(float);
bool isZincBoiling(float);
bool isBariumMelting(float);
bool isBariumBoiling(float);
bool isMercuryMelting(float);
bool isMercuryBoiling(float);
bool isUraniumMelting(float);
bool isUraniumBoiling(float);
};
//setTemp
void Elements::setTemp(float temp)
{
float t;
getTemp = t;
}
bool Elements::isZincMelting( float t)
{
if (t >= 787.15 && t < 1665)
return true;
}
int main()
{
Elements info; //Define an instance of Element class
float getTemp;
cout << "Enter a temperature: " << endl;
cin >> getTemp;
//Store in temp of element info object
info.setTemp(getTemp);
info.isZincMelting();
return 0;
}
isZincMelting expects a float as argument you called it without arguments. Also your isZinMelting function should return false in if the condition is not met:
bool Elements::isZincMelting( float t)
{
if (t >= 787.15 && t < 1665)
return true;
return false;
}

Code in .cpp file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am beginner in C++ in Eclipse environment. Could you explain whats wrong with this code.
#include "Shapes.h"
Shapes::Shapes(float l, float w)
{
length = l;
width = w;
float calculateArea()
{
float area = length * width;
return calculateArea;
}
}
Things wrong with your code:
You're not using the area variable in any way.
One of your } is in the wrong place.
Your calculateArea is probably a member function of Shapes and should as such be prefixed with Shapes::.
You aren't using the member initializer list.
Your indentation is inconsistent.
You forgot to add the class scope in the implementation of calculatateArea.
Your cpp code must look like
Shapes::Shapes(float l, float w)
{
length = l;
width = w;
}
float Shapes::calculateArea()
{
float area = length * width;
return area;
}
This is how your code should be:
#include "Shapes.h"
Shapes::Shape(float l, float w) : length(l), width(w) { }
float Shape::calculateArea()
{
return this->length * this->width;
}
I changed your constructor to use the constructor syntax. It is faster than the assignment you did before.
You should read this http://www.cplusplus.com/doc/tutorial/classes/
It even has the same exact example as you are doing.

How to create dynamic objects in member function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In a member function of Class Championship, I try to create dynamic objects and call the member function of Class Game, but I got the error message as error: expected primary-expression before '[' token. How to correct it?
class Championship
{
public:
Championship(string,int);
void CreateGame();
void display();
private:
int gamenum;
};
class Game
{
public:
friend class Championship;
void Getteam();
};
void Championship::CreateGame()
{
Game *game = new Game[gamenum];
for (int i = 0; i < gamenum; i++)
Game[i].Getteam();
}
The exact problem you are facing in your code is in this small bit
Game *game = new Game[gamenum];
for (int i = 0; i < gamenum; i++)
Game[i].Getteam();
The main issue here is that you have declare an array of type Game and call it game but then you try to access using Game which is the type, so simply swapping that back to game would fix that issue.
However, there is no need for using raw pointers in this way. std::vector is superior in so many ways here. It allows you to dynamically add more and more objects into the container in a safe way. I was about to show how std::vector could be used in your Championship::CreateGame() function... but I can't really work out what it is trying to do...
I also don't see why you have the friend line in your game class... that is used to give another class 'full' access to your class, ie the Championship class is given access to private members of Game.
Edit: While this answer doesn't directly solve the problem, it does provide a useful alternative syntax for allocating arrays of User-Defined Objects.
This can probably be solved not-so-elegantly using the classic C-style double-pointer array (i.e. argv from int main(int argc, char** argv). Such code would first allocate the space for the array, and then allocate the space for each individual object using a loop with an index.
#include <iostream>
class Foo
{
public:
//Constructor to ensure each object is unique with an int + loop
Foo(int k)
: i(k)
{}
int i;
int operator() () {return i;}
};
int main ()
{
//Arbitrary number for allocation; get this somehow
int i = 5;
//Although it can be unsafe, allocate an array of pointers with a pointer to pointer
Foo** array = new Foo*[i];
for (int j = 0; j < i; ++j)
{
array[j] = new Foo(j);
//Here, I use operator() to test that each object is unique.
std::cout << (*array[j])() << std::endl;
}
//Using Coliru, the program will work and print out this
std::cout << "this worked!\n";
return 0;
}
Coliru: http://coliru.stacked-crooked.com/a/84f7641e5c4fa2f3

Pointer Issues "cannot instantiate abstract class" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Doing a programming homework assignment and I am having some trouble with pointers. I'm not too sure what the issue is.
I have looked around and found a few solved issues but I can't seem to figure out how to implement the fixes in my own code. (noob)
in my main I call:
MotherShip* m1 = new MotherShip(5, 6);
I am getting the error "cannot instantiate abstract class" with this.
MotherShip.h:
#include "SpaceShip.h"
class MotherShip : public SpaceShip
{
public:
int capacity;
MotherShip();
MotherShip(int x, int y, int cap);
MotherShip(const MotherShip& ms);
void print();
};
MotherShip.cpp:
#include "stdafx.h"
#include "MotherShip.h"
MotherShip::MotherShip()
{
}
MotherShip::MotherShip(int x, int y, int cap)
{
}
MotherShip::MotherShip(const MotherShip& ms)
{
}
void MotherShip::print()
{
}
Here is my full main (I don't think it's important here so I thought I'd just pastie it)
http://pastie.org/pastes/8429256/text
You're passing two arguments to your class constructor, however you have not defined a constructor that takes two arguments.
One solution would be:
MotherShip* m1 = new MotherShip(5, 6, 7 /* passing third argument */);
Another solution is defining a constructor to take two arguments:
MotherShip(int x, int y);
you must set the cap parameter as your constructor requires it.
There's no constructor that takes two ints!
Use the default value in your declaration
MotherShip(int x, int y, int cap = 123);
or, as an alternative, declare and define another constructor that takes two ints:
MotherShip(int x, int y);
Could be guessed without looking. abstract class in C++ is implemented by adding a pure virtual function.
You sure have a pure virtual function in your base class SpaceShip which you need to override in MotherShip. Or else MotherShip too becomes abstract and cannot be instantiated.
class SpaceShip
{
public:
virtual void DoSomething() = 0; //override this with some implementation in MotherShip
};