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;
};
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have come across a rather unusual issue in my code. A struct needs to be able to access instances of itself.
Relavent portion of code:
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
vector<crtr> creatures[10];
Of course, this is nowhere close to working - crtr.foo() requires creatures, while creatures requires crtr. Is there some way to initialize creatures before crtr, perhaps changing the vectors' data type? (preferably with minimal pointers, if possible)
I must be missing something, what's wrong with this?
struct crtr {
char f;
void foo();
};
vector<crtr> creatures[10];
void crtr::foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
Also crtr::foo can be inline if that's required.
Use forward declaration of the struct:
struct crtr;
vector<crtr> creatures[10];
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
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 6 years ago.
Improve this question
I am getting error on data *c=it;. I need to retrieve only one value.
class X
{
string value4;
vector<data> *T1;
}
class data
{
string value1;
int value2;
}
void doTask(X V1)
{
vector<data> *tempdata=V1.getData();
for (std::vector<data>::iterator it = tempdata->begin() ; it !=tempdata->end(); ++it)
{
data *c=it;
sendData(value3,c);
}
}
void sendData(string s,data d)
{
}
I am getting this error:
error: cannot convert 'std::vector::iterator {aka
__gnu_cxx::__normal_iterator >}' to 'data*' in initialization
I am new in this vector usage. Can somebody help me in this?
You have to dereference the iterator to get a reference to the contained item type, then get its address to get a pointer:
data *c = &*it;
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
Constructor is here:
When I declare my setLeft() function, it tells me m_pLeft is not defined. I've tried moving it all over the place and can't get it to say it's anything other than undefined.
SetLeft is defined as
void setLeft(BookRecord *leftpointer){
*m_pLeft = *leftpointer;
}
#pragma once
class BookRecord
{
private:
char m_sName[100]; //unique names for each book
long m_lStockNum; //a stock number, similar to a barcode
int m_iClassification; //how a book should be classified, similar to a dewey decimal system
double m_dCost; //The price of the book
int m_iCount; //How many books are in stock
BookRecord *m_pLeft; //Left pointer for the tree
BookRecord *m_pRight; //right Pointer from the tree
public:
BookRecord(void);
BookRecord(char *name,long sn, int cl,double cost);
~BookRecord();
void getName(char *name);
void setName(char *Sname);
long getStockNum();
void setStockNum(long sn);
void getClassification(int& cl);
void setClassification(int cl);
double getCost();
void setCost(double c);
int getNumberInStock();
void setNumberInStock(int count);
void printRecord();
BookRecord getLeft();
BookRecord getRight();
void setLeft(BookRecord *leftpointer);
void setRight(BookRecord *rightpointer);
};
When I declare my setLeft() function, it tells me m_pLeft is not defined.
The error that you see is not coming from the declaration of setLeft() member function, but from its definition (the declaration is not referencing m_pLeft):
// This is incorrect - it will not compile
void setLeft(BookRecord *leftpointer) {
m_pLeft = leftpointer;
}
The problem with a definition like this is that the compiler treats it like a free-standing function, so m_pLeft member is not in scope. You need to tell the compiler that you are defining a member function, like this:
// This will compile
void BookRecord::setLeft(BookRecord *leftpointer) {
m_pLeft = leftpointer;
}