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.
Related
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 4 years ago.
Improve this question
I am kind a beginner to c++ and i dont really understand much about pointers.
There is an error in the code below. Soldier is a class in my program. The error states that 'targetsoldier was not declared in this scope'.
void level::battle(soldier *soldier, int targetx, int targety)
{
int x, y;
int enemyarmy;
soldier->getposition(x, y);
soldier *targetsoldier = getsoldier(targetx, targety);//THE ERROR OCCURS IN
THIS LINE
if(targetsoldier == nullptr){
return;
}
enemyarmy = targetsoldier->getarmy();
if(enemyarmy == soldier->getarmy()){
return;
}
int result = targetsoldier->takedamage(soldier->attack());
if(result ==1){
for(int h=0; h < _armies[enemyarmy].size(); h++){
if(_armies[enemyarmy][h] == targetsoldier) {
_armies[enemyarmy][h] = _armies[enemyarmy].back();
_armies[enemyarmy].pop_back();
delete *targetsoldier;
settile(targetx, targety, ' ', nullptr);
break;
}
}
}
}
The problem is that your function has a parameter named soldier; the name of that parameter then hides the name of the class soldier when it's in scope (i.e. inside the function). There are two possible solutions:
The sane one: rename the parameter (or the class)
The alternative: use class soldier instead of just solider to refer to the type when the parameter is in scope.
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).
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
In my program I have structure:
struct point {
float x;
float y;
};
Edit: I need to create
struct Path{
Point array[];
}
Initialize it with function init_path(Path *p, int size).
My question is, how to define the function?
Thanks in advance.
Your Path may be like:
struct Path {
point* points;
};
void init_path(Path *path, int size) {
path->points = new point[size]();
}
but why your professor wants a function instead of proper constructor/destructor remains a mystery. Here you still need to call delete[] on points somewhere. With following structure you don't need any init function and object will properly delete its resources.
struct Path {
Path(unsigned size) : points{ new point[size] } {}
~Path() { delete[] points; }
point* points;
};
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.
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
};