I am experimenting with lambda functions and managed to recreate a "get" functionality in C++. I can get the return value of a function without using parentheses. This is an example class, where I implement this:
using namespace std;
struct Vector2 {
float x;
float y;
float length = [&]()-> float {return sqrt(x * x + y * y); }();
float angle = [&]()-> float {return atan2(y, x); }();
Vector2() : x(0), y(0) {}
Vector2(float a, float b) : x(a), y(b) {}
~Vector2() {}
Vector2(Vector2& other) : x(other.x), y(other.y) {}
Vector2(Vector2&& other) = delete;
void operator =(Vector2&& other) noexcept{
x = other.x;
y = other.y;
}
};
int main()
{
Vector2 vec = Vector2(10, 17);
printf("%f\n%f\n%f\n%f\n", vec.x, vec.y, vec.length, vec.angle);
}
However, I am currently trying to also recreate the "set" functionality that C# has. But I'm failing. I tried to add this:
void angle = [&](float a)->void {
float l = length;
x = cos(a) * l;
y = sin(a) * l;
};
But am getting "Incomplete type is not allowed" error. I'm not sure if that's how it should look, even if I wasn't getting the error. Is it even possible to recreate the "set" functionality C# has in C++?
I know that I can just use a method SetAngle(float a){...}, but that's not really the point.
TL;DR: Don't
What you have isn't a getter, it's just a normal data member that's calculated once when the object is initialized.
In general, C++ doesn't support C#-style properties. The usual C++-style solution is to just use a pair of member functions (and maybe a data member, if you need to save the value separately), i.e.
struct Vector2 {
// ...
float length() const { return sqrt(x * x + y * y); }
void length(float l) {
float angle = angle();
float new_x = l * cos(angle);
float new_y = l * sin(angle);
x = new_x;
y = new_y;
}
// ...
};
You can get something close to a C#-style property, but you'll always run into edge-cases where they don't work perfectly. For example, here's something that will work in many cases:
template <typename T>
class Property
{
private:
std::function<T()> getter_;
std::function<void(const T&)> setter_;
public:
Property(std::function<T()> getter, std::function<void(const T&)> setter)
: getter_{getter},
setter_{setter}
{}
operator T()
{
return getter_();
}
const T& operator=(const T& val)
{
setter_(val);
return val;
}
};
struct Vector2
{
float x;
float y;
Property<float> length{
[this]() { return sqrt(x * x + y * y); },
[this](float l) {
float new_x = l * cos(angle);
float new_y = l * sin(angle);
x = new_x;
y = new_y;
}
}
Property<float> angle{
[this]() { return atan2(y, x); },
[this](float a) {
float l = length;
x = cos(a) * l;
y = sin(a) * l;
}
}
// ...
};
int main() {
Vector2 v;
v.x = 1;
v.y = 1;
v.angle = std::numbers::pi / 2;
std::cout << "(" << v.x << ", " << v.y << ")\n";
}
But this still falls apart in the edge cases, especially when you mix it with templates and/or auto type-deduction. For instance:
Vector2 v;
v.x = 1;
v.y = 1;
auto old_angle = v.angle;
v.angle = std::numbers::pi / 2;
// oops, this prints pi/2, not pi/4 like you probably expected
// because old_angle isn't a float, it's a Property<float> that
// references v
std::cout << old_angle << '\n';
Note also that there's a bug here. Consider this:
int main() {
Vector2 v1;
v1.x = 1;
v1.y = 1;
Vector2 v2 = v1;
v2.angle = std::numbers::pi / 2;
// Oops, assigning to v2.angle modified v1
std::cout << "(" << v1.x << ", " << v1.y << ")\n";
}
You could work around these issues by making Property non-copyable, but then you force any class that uses it to implement a custom copy-constructor. Also, while that would make the auto case "safe", it does so by turning it into a compile error. Still not ideal.
I agree with Miles. This is not the greatest idea, because it's unnatural for C++ developers, and you should write code that is first and foremost easy to read.
However, as an engineering challenge, here's a possible implementation:
#include <math.h>
#include <iostream>
template <typename T>
class Member
{
public:
operator T() const { return _value; }
void operator =(const T& value) const { _value = value; } void operator =(T&& value) { _value = std::move(value); }
private:
T _value;
};
class Angle
{
public:
Angle(const Member<float>& x, const Member<float>& y) :
_x(x), _y(y) {}
operator float() const { return atan2(_y, _x); }
private:
const Member<float>& _x, _y;
};
class Obj
{
public:
Member<float> x, y;
Angle angle;
Obj() : angle(this->x, this->y) {}
};
int main()
{
Obj o;
o.x = 3;
o.y = 5;
std::cout << o.x << ", " << o.y << " -> " << o.angle << std::endl;
}
While other solutions also seem to be possible, this one seems to be the most elegant :P
using namespace std;
struct Vector2 {
float x;
float y;
float init_length = [&]()-> float {return sqrt(x * x + y * y); }();
float init_angle = [&]()-> float {return atan2(y, x); }();
__declspec(property(get = GetAngle, put = SetAngle)) float angle;
__declspec(property(get = GetLength, put = SetLength)) float length;
Vector2() : x(0), y(0) {}
Vector2(float a, float b) : x(a), y(b) {}
~Vector2() {}
Vector2(Vector2& other) : x(other.x), y(other.y) {}
Vector2(Vector2&& other) = delete;
void operator =(Vector2&& other) = delete;
void Display() {
printf("%f\n%f\n%f\n%f\n\n", x, y, length, angle);
}
float GetLength() {
return sqrt(x * x + y * y);
}
float GetAngle() {
return atan2(y, x);
}
void SetLength(float l) {
float a = GetAngle();
x = cos(a) * l;
y = sin(a) * l;
}
void SetAngle(float a) {
float l = GetLength();
x = cos(a) * l;
y = sin(a) * l;
}
};
int main()
{
Vector2 vec = Vector2(10, 17);
vec.Display();
vec.length = 5;
vec.Display();
}
I'm using Visual Studio Express 2017 to work with OpenGL. My teacher provided the class with this code that worked on the lab computers which had the 2012 version installed, but on my PC it is not working. It keeps giving me 2 errors:
no instance of constructor canvas::canvas matches the argument list. E0289
This error is on the line Canvas cvs(640, 480, "my prog");
Canvas::canvas(canvas&&)
cannot convert argument 3 from 'const char[7] to char*'
C2664
This error is on the line if (n < 3)
Sorry, it's my first time using this site, so I pasted the entire code. It would be great if anyone could help me with this. I'm not really good with programming.
#include "stdafx.h"
#include<Windows.h>
#include<glut.h>
#include<gl/GLU.h>
#include <gl/GL.H>
#include<math.h>
#include<fstream>
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3ub(1.0, 1.0, 1.0);
glPointSize(100.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
class Point2 //single point w/ floating point coordinates
{
public:
Point2() { x = y = 0.0f; }
Point2(float xx, float yy) { x = xx; y = yy; }
void set(float xx, float yy) { x = xx; y = yy; }
float getX() { return x; }
float getY() { return y; }
void draw(void)
{
glBegin(GL_POINTS); //draw this point
glVertex2f((GLfloat)x, (GLfloat)y);
glEnd();
}
private:
float x, y;
};
class IntRect //aligned rectangle with integer coordinates, used for viewport
{
public:
IntRect() { l = 0; r = 100; b = 0; t = 100; }
IntRect(int left, int right, int bottom, int top)
{
l = left; r = right; b = bottom; t = top;
}
void set(int left, int right, int bottom, int top)
{
l = left; r = right; b = bottom; t = top;
}
void draw(void); //draw this rectangle using OpenGL
int getL(void)
{
return l;
}
int getR(void)
{
return r;
}
int getT(void)
{
return t;
}
int getB(void)
{
return b;
}
private:
int l, r, b, t;
};
class RealRect //simlar to IntRect but w/ floating points & used for world window
{
public:
RealRect() { l = 0; r = 100; b = 0; t = 100; }
RealRect(float left, float right, float bottom, float top)
{
l = left; r = right; b = bottom; t = top;
}
void set(float left, float right, float bottom, float top)
{
l = left; r = right; b = bottom; t = top;
}
float getL(void)
{
return l;
}
float getR(void)
{
return r;
}
float getT(void)
{
return t;
}
float getB(void)
{
return b;
}
void draw(void); //draw this rectangle using OpenGL
private:
float l, r, b, t;
};
//<<End Support Classes>>>
class Canvas
{
public:
Canvas(int width, int height, char* windowTitle); //constructor
void setWindow(float l, float r, float b, float t);
void setViewport(int l, int r, int b, int t);
IntRect getViewport(void); //divulge the viewport data
RealRect getWindow(void); // divulge the window data
float getWindowAspectRatio(void);
void clearScreen();
void setBackgroundColor(float r, float g, float b);
void setColor(float r, float g, float b);
void lineTo(float x, float y);
void lineTo(Point2 p);
void moveTo(float x, float y);
void moveTo(Point2 p);
void moveRel(float dx, float dy);
void turnTo(float angle);
void turn(float angle);
void forward(float dist, int isVisible);
Point2 Tween(Point2 A, Point2 B, float t);
void drawTween(Point2 A[], Point2 B[], int N, float t);
private:
Point2 CP; //current position in the world
float CD; //current direction in the world
IntRect viewport; //the current window
RealRect window; //the current viewport
};
//<<moveTo>>
//changes current point
void Canvas::moveTo(float x, float y)
{
CP.set(x, y);
}
//<<lineTo>>
//draws a line from current point to new point
void Canvas::lineTo(float x, float y)
{
glBegin(GL_LINES);
glVertex2f((GLfloat)CP.getX(), (GLfloat)CP.getY());
glVertex2f((GLfloat)x, (GLfloat)y); //draw the line
glEnd();
CP.set(x, y); //update current point to new point
glFlush();
}
//<<setWindow>>
void Canvas::setWindow(float l, float r, float b, float t)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D((GLdouble)l, (GLdouble)r, (GLdouble)b, (GLdouble)t);
window.set(l, r, b, t);
}
//<<setViewport>>
void Canvas::setViewport(int l, int r, int b, int t)
{
glViewport(l, b, r - l, t - b);
viewport.set(l, r, b, t);
}
IntRect Canvas::getViewport(void)
{
return viewport;
}
RealRect Canvas::getWindow(void)
{
return window;
}
void Canvas::clearScreen(void)
{
glClear(GL_COLOR_BUFFER_BIT);
}
void Canvas::setBackgroundColor(float r, float g, float b)
{
glClearColor(r, g, b, 0.0); //4th variable level of transparency, may need to change
}
void Canvas::setColor(float r, float g, float b)
{
glColor3f(r, g, b);
}
void Canvas::lineTo(Point2 p)
{
glBegin(GL_LINES);
glVertex2f((GLfloat)CP.getX(), (GLfloat)CP.getY());
glVertex2f((GLfloat)p.getX(), (GLfloat)p.getY());
glEnd();
CP.set(p.getX(), p.getY());
glFlush();
}
Canvas::Canvas(int width, int height, char* windowTitle)
{
char* argv[1]; //dummy argument list for glutinit()
char dummyString[8];
argv[0] = dummyString; //hook up the pointer
int argc = 1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition(20, 20);
glutCreateWindow("FourCircle");
setWindow(0, (float)width, 0, (float)height); // default world window
setViewport(0, width, 0, height); //default viewport
CP.set(0.0f, 0.0f); //initialize the cp to (0,0)
}
void Canvas::moveTo(Point2 p) //moves current point CP to point p object
{
float x1, y1;
x1 = p.getX();
y1 = p.getY();
CP.set(x1, y1);
}
float Canvas::getWindowAspectRatio(void) //calculates aspect ratio of world window
{
float width, height, aspectRatio;
width = window.getR() - window.getL();
height = window.getT() - window.getB();
aspectRatio = width / height;
return aspectRatio;
}
void Canvas::moveRel(float dx, float dy)
{
CP.set(CP.getX() + dx, CP.getY() + dy);
}
void Canvas::turnTo(float angle)
{
CD = angle;
}
void Canvas::turn(float angle)
{
CD += angle;
}
void Canvas::forward(float dist, int isVisible)
{
const float RadPerDeg = 0.017453393; //radians per degree
float x = CP.getX() + dist * cos(RadPerDeg * CD);
float y = CP.getY() + dist * sin(RadPerDeg * CD);
if (isVisible)
lineTo(x, y);
else
moveTo(x, y);
}
Point2 Canvas::Tween(Point2 A, Point2 B, float t)
{
Point2 P;
P.set(A.getX() + (B.getX() - A.getX())*t, A.getY() + (B.getY() - A.getY())*t);
return P;
}
class point {
public:
int x, y;
};
void Canvas::drawTween(Point2 A[], Point2 B[], int N, float t)
{
for (int i = 0; i < N; i++)
{
Point2 P;
P = Tween(A[i], B[i], t);
if (i == 0) moveTo(P.getX(), P.getY());
else lineTo(P.getX(), P.getY());
}
}
Canvas cvs(640, 480, "my prog");
void ngon(int n, float x, float y, float radius, float rot)
{
if (n < 3)
return;
double angle = rot * 3.141592 / 180;
double angleinc = 2 * 3.141592 / n;
cvs.moveTo(radius*cos(angle) + x, radius*sin(angle) + y);
for (int i = 0; i < n; i++)
{
angle += angleinc;
cvs.lineTo(radius*cos(angle) + x, radius*sin(angle) + y);
}
glFlush();
}
void display(void)
{
glColor3ub(255, 60, 60);
ngon(100, 300, 320, 60, 90);//top
ngon(100, 300, 200, 60, 90);//bottom
ngon(100, 240, 260, 60, 90);//left
ngon(100, 360, 260, 60, 90);//right
ngon(100, 260, 300, 60, 90);//top left
ngon(100, 340, 300, 60, 90);//top right
ngon(100, 260, 220, 60, 90);//bottom left
ngon(100, 340, 220, 60, 90);//bottom right
glFlush();
}
void main(void)
{
glutDisplayFunc(display);
glutMainLoop();
}
The code is not valid since C++11 and makes use of a deprecated conversion before that. A string literal is not convertible to char*, only to const char*.
So the constructor should read
Canvas(int width, int height, const char* windowTitle); //constructor
^^^^^
Depending on the settings, Visual Studio will accept the wrong version anyway, even in newer standard version modes, but that is not standard-conform.
Interestingly the constructor parameter seems to be unused in the definition of the constructor, so maybe just remove it completely.
The constructor also has another issue: glutInit is supposed to be passed argc and argv from main's parameters directly. The shown code passes a pointer to an uninitialized array instead. Depending on what glutInit does with this, it is likely to cause undefined behavior for reading uninitialized values of dummyString.
void main(void) is also not legal standard C++. The return type of main should always be int. Using void as single parameter for an empty parameter list is allowed, but unconventional, in C++. It comes from C, where it has a different meaning from an empty parameter list. In C++ one would usually just write () instead of (void).
I am trying to figure out how to find the axis of class Square's axis as shown below? But I've been trying for hours and still did not managed to solve it. Can someone with their high level expertise show me the ropes to do it? Because both center function call and axis function call in main() does call the same x() and y(), hence brought me to a state of confusion. I know the inheritance of square from circle is weird. But it is what my school wants. Note: Main() CANNOT be modified! Thanks!
Output:
Square::axis test failed
8.87627 0.284967
3.82567 0.958537
Tests passed: 50%
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
class Object
{
public:
private:
float d;
public:
Object(float n) : d(n){}
Object(){}
float depth() const
{
return d;
}
struct PointType
{
float x2;
float y2;
PointType( float x1, float y1) :x2(x1),y2(y1){}
PointType(){}
float x()
{
return x2;
}
float y()
{
return y2;
}
PointType center()
{
return *this;
}
};
struct VectorType
{
float tx;
float ty;
VectorType( float tx1, float ty1) :tx(tx1),ty(ty1){}
VectorType( ){}
};
virtual ~Object()
{}
};
class Point :public Object
{
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) :Object(y1), mpoint(pt) {}
Point(const PointType& pt):mpoint(pt){}
Point(){}
Point center() const
{
return *this;
}
float x()
{
return mpoint.x2;
}
float y()
{
return mpoint.y2;
}
virtual ~Point(){}
};
class Circle : public Point
{
private:
Object::PointType m_pt;
float r;
public:
Circle(const PointType pts, float rad, float dep)
: Point(m_pt,dep),m_pt(pts),r(rad) {}
Circle(const PointType pts, float rad):m_pt(pts),r(rad){}
Circle(){}
float radius() const
{
return r;
}
Circle center() const
{
return *this;
};
float x()
{
return m_pt.x2;
}
float y()
{
return m_pt.y2;
}
};
class Square: public Circle
{
private:
Object::PointType s_pt;
Object::VectorType v_pt;
float a=getRadius();
public:
Square(const PointType spts,const VectorType vpts,float depth) :
Circle(spts,a,depth),s_pt(spts),v_pt(vpts){}
Square(const Object::PointType& spts, const VectorType vpts):s_pt(spts),v_pt(vpts){}
Square axis() const
{
return Square(s_pt,v_pt,getRadius());
}
Square center() const
{
return *this;
}
float radius() const
{
return a;
}
float getRadius() const
{
float rad= sqrt(v_pt.tx * v_pt.tx + v_pt.ty * v_pt.ty);
return rad;
}
float x() const
{
return s_pt.x2;
// v_pt.tx/radius();
}
float y() const
{
return s_pt.y2;
// v_pt.ty/radius();
}
};
const float EPSILON = 1e-5f;
bool is_near(float x, float y)
{
return std::abs(x - y) < EPSILON;
}
float frand()
{
return 10.0f * float(rand()) / float(RAND_MAX);
}
int main()
{
srand(unsigned(time(0)));
int count = 0;
int max_count = 0;
float x = frand();
float y = frand();
float sx = frand();
float sy = frand();
float depth = frand();
Square square(Square::PointType(x, y), Square::VectorType(sx, sy), depth);
if (is_near(square.center().x(), x) &&
is_near(square.center().y(), y))
{
++count;
}
else
{
std::cout << " - Square::center test failed" << std::endl;
}
++max_count;
float radius = std::sqrt(sx * sx + sy * sy);
if (is_near(square.axis().x(), sx / radius) &&
is_near(square.axis().y(), sy / radius))
{
++count;
}
else
{
std::cout << " - Square::axis test failed" << std::endl;
}
++max_count;
std::cout << square.axis().x()<< " " << sx / radius<<std::endl;
std::cout << square.axis().y()<< " " << sy / radius<<std::endl;
int result = static_cast<int>(
100.0f * static_cast<float>(count) / static_cast<float>(max_count) + 0.5f
);
std::cout << "Tests passed: " << result << "%" << std::endl;
return result;
}
I've created a class called Box that is pretty much what it sounds like. I want to sort these box objects, and I have created a function to do so.
void boxSort(Box array[], int size) {
Box temp;
bool swap;
do {
swap = false;
for(int count=0; count<(size-1); count++) {
int volume1 = array[count].getVolume(array[count].height, array[count].width, array[count].length);
int volume2 = array[count+1].getVolume(array[count+1].height, array[count+1].width, array[count+1].length);
if(volume1 > volume2) {
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
swap = true;
}
}
}
while(swap);
}
This function sorts an array of objects of class Box.
Box class:
class Box {
public:
double height, width, length;
double getVolume(double, double, double);
double getSurfaceArea(double, double, double);
void setHeight(double);
void setWidth(double);
void setLength(double);
Box() {
height = width = length = 1;
}
Box(double h, double w, double l) {
setHeight(h);
setWidth(w);
setLength(l);
}
};
#endif
void Box::setHeight(double h) {
height = h;
}
void Box::setWidth(double w) {
width = w;
}
void Box::setLength(double l) {
length = l;
}
double Box::getVolume(double h, double w, double l) {
double volume = h*w*l;
return volume;
}
double Box::getSurfaceArea(double h, double w, double l) {
double surfaceArea = (h*w)*2 + (h*l)*2 + (l*w)*2;
return surfaceArea;
}
When I run this program I get an error:
linker command failed with exit code 1
This doesn't shown up on any particular line, and I have no idea what it means, so I'm a little lost on how to debug this.
The linker errors are reported if you have any problem with libraries or object files linked. Were you able to successfully build the executable?
I think this error is occurring due to more than one main() function in your code.You can have only one main() function.
Not sure about the error. But I would suggest changing boxSort function to something like this.
change getVolume to this
double Box::getVolume() {
return height*width*length;
}
and boxSort to this
void boxSort(std::array<Box, 3> boxes)
{
struct
{
bool operator()(Box x, Box y)
{
return x.getVolume() < y.getVolume();
}
}compFunc;
std::sort(boxes.begin(), boxes.end(), compFunc);
for(Box a: boxes)
{
std::cout<<a.getVolume()<<" ";
}
}
function call:
std::array<Box,3> boxes = {box1, box2, box3};
boxSort(boxes);
What's wrong with this functions definitions? I've created this two functions and the second one calls the first one, so what i want is, depending on the if in the first one, the calling of AlignCamera(); will change what happens in AlignXAXis();
void AlignCamera();
{
double cx = ox+(0.5*(xmax-xmin))*sx;
double cy = oy+(0.5*(ymax-ymin))*sy;
double cz = oz+(0.5*(zmax-zmin))*sz;
int vx=0;
int vy=0;
int vz=0;
int nx=0;
int ny=0;
int nz=0;
int iaxis = current_widget->GetPlaneOrientation();
if (iaxis == 0)
{
vz = -1;
nx = ox + xmax*sx;
cx = ox + 256*sx;
}
else if (iaxis == 1)
{
vz = -1;
ny = oy + ymax*sy;
cy = oy + 512*sy;
}
else
{
vy = 1;
nz = oz +zmax*sz;
cz = oz + 64*sz;
}
int px = cx+nx*2;
int py = cy+ny*2;
int pz = cz+nz*3;
vtkCamera *camera=ren->GetActiveCamera();
camera->SetViewUp(vx, vy, vz);
camera->SetFocalPoint(cx, cy, cz);
camera->SetPosition(px, py, pz);
camera->OrthogonalizeViewUp();
ren->ResetCameraClippingRange();
renWin->Render();
}
// Define the action of AlignXAxis
void AlignXAxis();
{
int slice_number;
int po = planeX->GetPlaneOrientation();
if (po == 3)
{
planeX->SetPlaneOrientationToXAxes();
slice_number = (xmax-xmin)/2;
planeX->SetSliceIndex(slice_number);
}
else
{
slice_number = planeX->GetSliceIndex();
}
current_widget= planeX;
ui->horizontalScrollBar->setValue(slice_number);
ui->horizontalScrollBar->setMinimum(xmin);
ui->horizontalScrollBar->setMaximum(xmax);
AlignCamera();
}
the variables needed are defined before it, such as ox, oy, oz....
When i run it it says undefined reference to `AlignCamera()' or 'AlignXAxis()'.
void AlignCamera(); //This is merely a prototype, not a declaration.
Remove the Semicolon, or create a declaration afterwards.
void AlignCamera(); //Prototype
void AlignCamera() { //Declaration
//Do Stuff
}
The same applies for the other function. Remove that semicolon.
Remove the semicolons, so:
void AlignCamera();
{
...
}
becomes
void AlignCamera()
{
...
}