I encountered a problem when I was using Eigen objects in my code. There is a class defined in my code:
class MyClass
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
MyClass();
/*other member functions*/
private:
float3 member_var;
Eigen::Matrix4f pose;
int id;
/*......*/
};
As the code is shown above, if pose is not the first member variable in MyClass, e.g. after float3 member_var, the program got segmentation fault ( core dumped) error.
If I put Eigen::Matrix4f pose as the first member variable, there is no segfault error.
I checked the documentation of Eigen library about memory alignment issues, see here.
It says that Eigen member variables are not required to be put at the beginning of the class.
I am wondering whether it is a bug from Eigen or my code?
Related
This is the code I'm trying to run:
class poly {
public:
int vnum;
vrtx vrts[this->vnum];
};
(Note: The class name "poly" and other class "vrtx" are named as such to approximate the purpose of the problematic snippet. Vrtx is a class with int x, y, z;)
At first, the code didn't contain the "this->" pointer at all. I was confused why it wasn't working, and then realized that "vnum" doesn't mean anything. I needed an object.poly.vnum sort of thing so that I'm referencing a specific value. I tried "this.," "this.poly.," and the displayed "this->," but none of them work. I'm not great with pointers, so any advice would be appreciated!
I've looked at similar questions, but none of them address this issue in such a way that I could make the necessary fix with the information provided.
Here's a code fragment that should help.
class Poly
{
public:
int vnum;
std::vector<vrtx> vrts;
Poly(int capacity)
: vnum(capacity)
{ vrts.resize(vnum);}
};
The above fragment uses std::vector since the std::vector can expand dynamically (at run-time). The constructor uses the resize method to expand the std::vector to the given capacity.
Arrays are a pain to resize during run-time, so use std::vector.
I have two classes called Renderable and Triangle. The latter is supposed to be deriving from the former. I plan to be passing an array and its length into the Triangle constructor, which in turn will pass it to the constructor in the Renderable class that will handle the corresponding parameters. Here are the header files:
Renderable.h
class Renderable {
protected:
const int dataLength = 0;
const float* data[0] ;
public:
Renderable();
Renderable(int dataLength, float* data[]);
virtual void render() const =0 ;
};
Triangle.h
#pragma once
#include <glad/glad.h>
#include "Renderable.h"
class Triangle : public Renderable {
public:
void render() const;
};
So the problem is that the error check gave me invalid base class. I did some research and saw something about arrays' not being able to be an instance variable without specified size but this is still quite confusing to me. Could someone please enlighten me on this?
Also is there a more efficient solution to problems like this?
Thank you!
const float* data[0] ;
This array member declaration is ill-formed. The size of an array variable may not be 0.
Otherwise the example is well-formed (assuming the functions are defined in some translation unit).
Also is there a more efficient solution to problems like this?
Solution is to not declare an array variable of size 0.
So the problem here is that the code design that I was going for is undesirable and I should either use std::vector or making another subtype of the Renderable class with a fixed array length and then extend from it.
I'm new to C++ and trying to figure this out. When I compile, I get the error a nonstatic member reference must be relative to a specific object. What's the correct way of writing the code? This is what I have where numShapes is giving me the error.
class Application
private:
int numShapes;
public:
Shapes * shapes[numShapes];
I then have this in another header as my virtual base class, if that's the correct term.
class Shapes
{
virtual void draw(char letter);
virtual ~Shapes() {}
};
The code Shapes * shapes[numShapes]; is requesting the compiler to reserve numShapes amount of space. The problem is that it does not have a known value at compile time. So either make numshapes a constant, or look into dynamic memory allocation.
Instead of
Shapes * shapes[numShapes];
I suggest the use of:
std::vector<Shapes*> shapes;
Remove numShapes altogether since you can get the size from shapes.
Initialize shapes in the constructor. Something along the lines of the following should work.
Application::Application(std::size_t numShapes) : shapes(numShapes, nullptr) {}
I am trying to have a declaration of 2d array in private part of my class and then latter on, the functions of this class will use and modify the values in the array, but I am getting an error which says "I-value specified const object". What is wrong with this code and how can I fix it?
Here is a simple code demonstrating my problem
in someClass.h________________
class someClass
{
public:
//Some code here
private:
char grid[20][20];
//Some code here
}
in someClass.cpp______________
lets say one of the functions is trying to reassign the value of item in position (0,0) like so.
grid[0][0]='*';
This gives me an error saying, expression must be modifiable lvalue.
Move it from private to public so you can access it outside the class. If it is private then only functions inside the class can use it.
I have C# background and been working with C# for so many years.. Recently, I'm learning C++ and having some difficulties..
Basically, I'm trying to create the linked link class as below. I want to use my class as a data in struct node.
How can I fix this in C++? Thanks.
But it said that i can't use like that.
class Polynomial{
public:
Polynomial(pair<double, int>);
void add(Polynomial);
Polynomial multiply(Polynomial);
void print();
private:
struct node
{
Polynomial data;
node *link;
}*p;
};
Your node struct contains a member variable of type Polynominal, but since node itself is declared inside Polynominal, the declaration of Polynominal isn't complete at that point.
I get the impression that you assume classes in C++ to work just like C#, but they don't. C++ isn't garbage-collected, and it doesn't automatically manage references for you when you use classes. A class in C++ behaves more like a struct in C#, and when you pass or declare it like in your example, it gets copied by value.
Another thing: C++ comes with STL, which contains a range of templates for all sorts of things, including a nice linked list (std::list).
Couple of issues:
Polynomial doesn't have a default constructor, so the only way to create it is by using that custom constructor you have. However, your inner struct contains an object of type Polynomial. How is that supposed to be created? You can't embed objects that don't have a default constructor in classes unless you initialize them specifically in the container's constructor.
Your struct contains an object of the type of the parent class, which you're still in the process of defining! If anything, you need to make that struct its own class.
In general, you seem to do a lot by-value operations. This is very inefficient - you should always pass Polynomial by reference or pointer.
To fix it just use Polynomial &data; instead of Polynomial data; in the struct
Change that to Polynomial *data; and it will work just fine. And therein lies your clue as to what's wrong. Understanding that will bring great enlightenment.
One way of explaining it is that in C++ (unlike C#) a Polynomial and a float behave in exactly the same way with regards to how storage is allocated with them. In C# you can't do new float; (not to be confused with new Float();) and in C++ you can.
The points raised by EboMike are all valid, but just to make it compile (it's still unusable due to the constructability issue):
class Polynomial{
public:
Polynomial(pair<double, int>);
void add(Polynomial);
Polynomial multiply(Polynomial);
void print();
private:
struct node; // forward declaration creates incomplete type
node *p; // OK to have pointer to incomplete type
};
struct Polynomial::node
{
Polynomial data; // class Polynomial is complete now
node *link;
};