Code that implements Vectors as Class and inputs and outputs them as txt files.
I was temporarily implementing a function of Class, but I have a question because there is an error.
I tried to add vectorA and vectorB as Add functions in the main function and replace them with vectorO to printf.
However, in the Vector output part of the Add function, the error No default creator of the Vector class continues to appear. How should we solve this problem?
#include<stdio.h>
class Vector
{
public: // private?
double x, y, z;
public:
Vector(int x, int y, int z) {
x = x;
y = y;
z = z;
}
double Magnitude(void);
Vector Add(Vector v) {
Vector output;
output.x = x + v.x;
output.y = y + v.y;
output.z = z + v.z;
return output;
}
Vector Subract(Vector v);
double DotProduct(Vector v);
Vector CrossProduct(Vector v);
};
int main()
{
Vector vectorA(1, 2, 3);
Vector vectorB(4, 5, 6);
Vector vectorO = vectorA.Add(vectorB);
printf("(%d, %d, %d)\n", vectorO.x, vectorO.y, vectorO.z); // (5, 7, 9)
return 0;
}
Even if I put this code in the Vector class, I get a strange value.
Vector() {
x = x;
y = y;
z = z;
}
This should fix your errors. have a basic and an overrided contructors, also be careful of the variable names they shouldn't be the same
#include<stdio.h>
class Vector
{
public: // private?
double x, y, z;
public:
Vector() {
x = 0;
y = 0;
z = 0;
}
Vector(int _x, int _y, int _z) {
x = _x;
y = _y;
z = _z;
}
double Magnitude(void);
Vector Add(Vector v) {
Vector output;
output.x = x + v.x;
output.y = y + v.y;
output.z = z + v.z;
return output;
}
Vector Subract(Vector v);
double DotProduct(Vector v);
Vector CrossProduct(Vector v);
};
int main()
{
Vector vectorA(1, 2, 3);
Vector vectorB(4, 5, 6);
Vector vectorO = vectorA.Add(vectorB);
printf("(%d, %d, %d)\n", vectorO.x, vectorO.y, vectorO.z); // (5, 7, 9)
return 0;
}
Read More Here
Related
I have a C# project which uses C++ classes from a library. C# classes are actually wrappers for C++ classes, they expose C++ functionality to C# client code.
In many places C++ value classes are converted to C# wrappers and backwards.
During code review I've found two ways how the classes are converted: via reinterpret_cast ( see operator * ) and via pin_ptr ( see MultiplyBy );
As you can see, both native and managed class has three 'double' fields, this is why someone was using reinterpret_cast;
In many places classes are copied from C# to C++ using memcpy:
memcpy(&NativePointInstance, &ManagedPointIntance, sizeof(double)*3);
I've heard from one developer that reinterpret_cast can be safe in some cases, when we work with C# value classes.
The question is:
When it is safe to use reinterpret_cast on C# value classes and when it is not?
What is the most correct way of converting the pointers in this case - like in operator * or like in MultiplyBy, or another alternative?
Can someone explain in details what is happening in MultiplyBy(), how these trick work?
As far as I understood, the issue may be caused by that optimizer may change fields order, GC may reorganize heap, and alignment of fields may be different between managed and native code.
// this is C++ native class
class NativePoint
{
public:
double x;
double y;
double z;
NativePoint(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
NativePoint operator * (int value)
{
return NativePoint(x * value, y * value, z * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint
{
internal:
double x;
double y;
double z;
ManagedPoint(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
public:
static ManagedPoint operator * (ManagedPoint a, double value)
{
return ManagedPoint((*reinterpret_cast<NativePoint*>(&(a))) * value);
}
ManagedPoint MultiplyBy(double value)
{
pin_ptr<ManagedPoint> pThisTmp = &*this;
NativePoint* pThis = reinterpret_cast<NativePoint*>(&*pThisTmp);
return ManagedPoint(*pThis * value);
}
};
// this should be called from C# code, or another .NET app
int main(array<System::String ^> ^args)
{
NativePoint p_native = NativePoint(1, 1, 1);
ManagedPoint p = ManagedPoint(p_native);
Console::WriteLine("p is {" + p.x + ", " + p.y + ", " + p.z + "}");
ManagedPoint p1 = p * 5;
Console::WriteLine("p1 is {" + p1.x + ", " + p1.y + ", " + p1.z + "}");
ManagedPoint p2 = p.MultiplyBy(5);
Console::WriteLine("p2 is {" + p2.x + ", " + p2.y + ", " + p2.z + "}");
Console::ReadLine();
return 0;
}
Well, I have ended up using usual constructors of native classes. It looks for me absolutely safe, and fastest from remaining variants. The idea from comments with Marshal::PtrToStructure() was good, but for my test example gave slower execution, than the same with using constructors. Pointer casts are the fastest solution, but after very scary example from comments, I will not risk to use it anymore (except if we really need to optimize it, then LayoutKind::Explicit should do the thing).
Here is code i used for testing:
// this is C++ native class
class NativePoint
{
public:
double x;
double y;
double z;
NativePoint()
{
}
NativePoint(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
NativePoint operator * (int value)
{
return NativePoint(x * value, y * value, z * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint
{
internal:
double x;
double y;
double z;
ManagedPoint(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint operator * (ManagedPoint a, double value)
{
return ManagedPoint((*reinterpret_cast<NativePoint*>(&(a))) * value);
}
ManagedPoint MultiplyBy(double value)
{
pin_ptr<ManagedPoint> pThisTmp = &*this;
NativePoint* pThis = reinterpret_cast<NativePoint*>(&*pThisTmp);
return ManagedPoint(*pThis * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint2
{
internal:
double x;
double y;
double z;
ManagedPoint2(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint2(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint2 operator * (ManagedPoint2 a, double value)
{
return ManagedPoint2((NativePoint(a.x, a.y, a.z)) * value);
}
ManagedPoint2 MultiplyBy(double value)
{
return ManagedPoint2((NativePoint(this->x, this->y, this->z)) * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint3
{
internal:
double x;
double y;
double z;
ManagedPoint3(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint3(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint3 operator * (ManagedPoint3 a, double value)
{
NativePoint p;
Marshal::StructureToPtr(a, IntPtr(&p), false);
return ManagedPoint3(p * value);
}
ManagedPoint3 MultiplyBy(double value)
{
NativePoint p;
Marshal::StructureToPtr(*this, IntPtr(&p), false);
return ManagedPoint3(p * value);
}
};
// this class managed C++ class
[StructLayout(LayoutKind::Sequential)]
public value class ManagedPoint4
{
internal:
double x;
double y;
double z;
ManagedPoint4(const NativePoint& p)
{
x = p.x;
y = p.y;
z = p.z;
}
ManagedPoint4(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
public:
static ManagedPoint4 operator * (ManagedPoint4 a, double value)
{
return ManagedPoint4(ManagedPoint4::ToNative(a) * value);
}
ManagedPoint4 MultiplyBy(double value)
{
return ManagedPoint4(ManagedPoint4::ToNative(*this) * value);
}
static NativePoint ToNative(const ManagedPoint4& pp)
{
NativePoint p;
Marshal::StructureToPtr(pp, IntPtr(&p), false);
return p;
}
};
// this should be called from C# code, or another .NET app
int main(array<System::String ^> ^args)
{
Stopwatch time;
time.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint a = ManagedPoint(1, 2, 3) * 4;
}
time.Stop();
Console::WriteLine("time: " + time.ElapsedMilliseconds);
Stopwatch time2;
time2.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint2 a2 = ManagedPoint2(1, 2, 3) * 4;
}
time2.Stop();
Console::WriteLine("time2: " + time2.ElapsedMilliseconds);
Stopwatch time3;
time3.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint3 a3 = ManagedPoint3(1, 2, 3) * 4;
}
time3.Stop();
Console::WriteLine("time3: " + time3.ElapsedMilliseconds);
Stopwatch time4;
time4.Start();
for (int i = 0; i < 10000000; i++)
{
ManagedPoint4 a3 = ManagedPoint4(1, 2, 3) * 4;
}
time4.Stop();
Console::WriteLine("time4: " + time4.ElapsedMilliseconds);
Console::ReadLine();
Console::WriteLine("======================================================");
Console::WriteLine();
return 0;
}
And this is output:
time: 374
time2: 382
time3: 857
time4: 961
time: 395
time2: 413
time3: 900
time4: 968
time: 376
time2: 378
time3: 840
time4: 909
I'm writing a program, where you input triangle point coordinates, the program checks if the triangle exists and outputs the area of the triangle. I have to use pointers in the program.
class Vertex
{
private:
int x, y;
public:
Vertex(int x, int y) : x(x), y(y) {}
int getX() {
return x;
}
int getY() {
return y;
}
float getDistance(Vertex *anotherVertex)
{
float dist;
int tempx = 0, tempy = 0;
tempx = anotherVertex->getX();
tempy = anotherVertex->getY();
dist = ((tempx - x) * (tempx - x) + (tempy - y) * (tempy - y));
return dist;
}
void setCoord(int x, int y)
{
this->x = x;
this->y = y;
}
};
class Triangle
{
private:
Vertex *a, *b, *c;
public:
Triangle()
{
a = new Vertex(0, 0);
b = new Vertex(0, 0);
c = new Vertex(0, 0);
}
void Set_coord()
{
int x1, y1, x2, y2, x3, y3;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
a->setCoord(x1, y1);
b->setCoord(x2, y2);
c->setCoord(x3, y3);
}
bool existTriangle() {
float ab = a->getDistance(b);
float bc = b->getDistance(c);
float ca = c->getDistance(a);
if (ab + bc > ca && ab + ca > bc && bc + ca > ab) {
return true;
}
else {
return false;
}
}
float getArea() {
float p;
float ab = a->getDistance(b);
float bc = b->getDistance(c);
float ca = c->getDistance(a);
p = (ab + bc + ca) / 2;
return sqrt(p * ((p - ab)*(p - bc)*(p - ca)));
}
};
I'm struggling to make the getDistance function working as I'm inexperienced with using pointers, when debugging i'm getting this error in the getX() function.
Exception thrown: read access violation.
this was 0xDDDDDDDD.
EDIT:
here is my main()
int main() {
int n = 0;
cin >> n;
vector<Triangle*> vertices;
for (int i = 0; i < n; i++) {
Triangle* newVertices = new Triangle();
newVertices->Set_coord();
vertices.push_back(newVertices);
delete newVertices;
}
for (int i = 0; i < n; i++)
{
if (vertices[i]->existTriangle())
{
cout << vertices[i]->getArea();
}
}
}
The problem is in your main function ( that's why I asked you to post it:) ):
Triangle* newVertices = new Triangle();
vertices.push_back(newVertices);
delete newVertices;
You dynamically allocate memory, that is pointed to by newVertices.
You store the pointer into the vector.
You delete the memory pointed by newVertices.
As a result, now that pointer is a dangling pointer.
So, you must not delete newVertices into the loop.
Do your thing (computer areas, check it a triangle exists, etc.), and then, when you are done, start deleting your dynamically allocating memory...
I have to use template because the requirement is that x, y, z can be of any types (int, float, double, long, etc).
#include <conio.h>
#include <iostream>
using namespace std;
template <class T>
class TVector //this class is for creating 3d vectors
{
T x, y, z;
public:
TVector() //default value for x, y, z
{
x = 0;
y = 0;
z = 0;
}
TVector(T a, T b, T c)
{
x = a; y = b; z = c;
}
void output()
{
cout << x << endl << y << endl << z;
}
//overloading operator + to calculate the sum of 2 vectors (2 objects)
TVector operator + (TVector vec)
{
TVector vec1;
vec1.x = this->x + vec.x;
vec1.y = this->y + vec.y;
vec1.z = this->z + vec.z;
return vec1;
}
};
int main()
{
TVector<int> v1(5, 1, 33);
TVector<float> v2(6.11, 6.1, 5.1);
TVector<float> v3;
v3 = v1 + v2;
v3.output();
system("pause");
return 0;
}
If the object v1 was float then the above code would run perfectly. However the requirement is that vector v1 has int as its data type. How do i solve this?
I already tried to use template for overloading + operator, my code looks like this:
template <typename U>
TVector operator+(TVector<U> vec)
{
TVector vec1;
vec1.x = this->x + vec.x;
vec1.y = this->y + vec.y;
vec1.z = this->z + vec.z;
return vec1;
};
^ Still doesn't work:
Your problem has nothing (or very little) to do with operator+ overloading. The compiler error says it all: v1 + v2 produces a vector of type TVector<int> (since that's how you defined the operator+), and you trying to assign it to v3 of type TVector<float>. But you haven't defined assignment operator for TVectors of different types (and this is exactly what compiler tells you in the error message)!
I got an object of type ball which is a subclass of the class Particle. This class has three members position, velocity and acceleration of type Vector.
On each frame, when myball.update is called, its velocity is summed to the position and the acceleration is summed to the velocity. Then the ball is drawn on the screen. Anyway for some unclear motive whatever value I give as a velocity the ball doesn't move, but it moves in non - accelerated uniform movement if I give a value as the acceleration.
Here are my classes Vector:
class Vector {
private:
void updateC() {
x = cos(a) * l;
y = sin(a) * l;
}
void updateT() {
a = atan2(y, x);
l = sqrt(x * x + y * y);
}
public:
float x = 0, y = 0;
float a = 0, l = 0;
Vector() {
}
Vector(float nx, float ny): x(nx), y(ny) {
updateT();
}
void X(float nx) {
x = nx;
updateT();
}
void Y(float ny) {
y = ny;
updateT();
}
void A(float na) {
a = na;
updateC();
}
void L(float nl) {
l = nl;
updateC();
}
Vector operator +(Vector other) {
return Vector(x + other.x, y + other.y);
}
Vector operator -(Vector other) {
return Vector(x - other.x, y - other.y);
}
Vector operator *(float m) {
Vector result(x, y);
result.L(l * m);
return result;
}
void operator +=(Vector other) {
x += other.x;
y += other.y;
updateT();
}
void operator -=(Vector other) {
x -= other.x;
y -= other.y;
updateT();
}
void operator *=(float m) {
l *= m;
updateC();
}
};
Particle:
class Particle {
public:
Vector position;
Vector velocity;
Vector gravity;
Particle() {}
Particle(Vector np, Vector nv = Vector(0, 0), Vector na = Vector(0, 0)): position(np), velocity(na), gravity(na) {}
void accelerate(Vector a) {
velocity += a;
}
void update() {
position += velocity;
velocity += gravity;
}
};
And ball:
class ball: public Particle {
public:
ball(Vector p, Vector v, Vector a): Particle(p, v, a) {}
void update() {
Particle::update();
graphics.circle("fill", position.x, position.y, 10);
}
};
So, as I said before, if I initialize myball with a velocity different than 0, the ball still won't move, but it will move if I inititialize it with an acceleration different than 0 using acceleration as velocity.
What did I do wrong?
You have a typo in the Particle constructor:
Particle(Vector np, Vector nv = Vector(0, 0), Vector na = Vector(0, 0)): position(np), velocity(na), gravity(na) {}
It must be:
Particle(Vector np, Vector nv = Vector(0, 0), Vector na = Vector(0, 0)): position(np), velocity(nv), gravity(na) {}
How can i use the x and y values of lastLoc object in another function like in the following code. I get no errors but when i print the values of lastLoc in the getPosLoc function I get a long number(possibly address):
class solveMaze {
private:
maze maze;
mouse m;
stack<coords> coordStack;
int x;
int y;
int posLocCount = 0;
coords lastLoc;
};
solveMaze::solveMaze() {
x = m.x;
y = m.y;
coords c(x, y);
coords lastLoc(c.x, c.y);
coordStack.push(c);
}
void solveMaze::getPosLoc() {
if((mazeLayout[x][y-1] == 0) && (x != lastLoc.x) && (y-1 != lastLoc.y)) {
posLocCount++;
putUp();
}
this is the coords.h removed irrelevant functions to shorten the code:
class coords {
public:
coords(){};
coords(int, int);
int x;
int y;
friend ostream &operator<<(ostream &output, const coords &c);
bool operator==(coords);
void operator=(const coords &b);
};
coords::coords(int a, int b) {
x = a;
y = b;
}
this is mouse.h:
class mouse {
private:
maze maze;
public:
mouse();
int x;
int y;
};
mouse::mouse() {
for (int i=0; i<12; i++) {
for (int j=0; j<29; j++) {
if (mazeLayout[i][j] == 8){
x = j;
y = i;
}
}
}
}
There are a couple of evident problems:
coords lastLoc(c.x, c.y);
This statement declares and initialize a local variable named lastLoc... it is not referring to the member lastLoc. For that the code needs to be
lastLoc = coords(c.x, c.y);
x = m.x; and y = m.y;
These statements use m that has not been explicitly initialized, how is that class defined?
You should make getters and setters for x and y, because it's better practise. But if you want refer to coord's x or y. You should write:
lastLoc->x