vector - String to name functions or parameters (I don't know how it is) -
i'm learning vectors , want create function called "createvector2d", have code:
#include <iostream> #include <vector> #include <math.h> using namespace std; class vector2d { public: float x, y; vector2d(float x, float y) { x = x; y = y; } float getmagnitude() { return sqrt((x * x) + (y * y)); } }; void createvector (float x, float y, string vectorname){ vector2d vectorname(x,y); cout<<"vector created!\nname: "<<vectorname<<"\nx: "<<x<<"\ny:"<<y<<endl; } int main(){ float x; float y; string vecname; printf ("vector velocidad (x,y)\ninserta x\n"); cin>>x; system("cls"); printf ("vector velocidad (%f,y)\ninserta y\n", x); cin>>y; system("cls"); printf ("vector velocidad (%f,%f)\n", x, y); printf ("escribe el nombre del vector\n"); cin>>vecname; createvector (x,y,vecname); printf ("-1. calcular magnitud (modulo)\n"); printf ("-2. enseƱar vector\n"); int op = 0; cin>>op; switch(op){ case 1: cout<<vecname.getmagnitude()<<endl; break; case 2: cout<<"x: "<<vecname.x<<endl; cout<<"y: "<<vecname.y<<endl; break; default: cout<<"no elegiste nada"<<endl; break; } system("pause>null"); } the error string can not works that, , don't know how transform works correctly vector name of class vector2d, knows? :d read.
you want have createvector() return value:
vector2d createvector(float x, float y) { vector2d vec(x, y); return vec; } the return value of function assigned vecname variable:
vector2d vecname; vecname = createvector(1, 2); but, don't want such function anyway, since vector2d has public constructor works fine:
vector2d vecname(1, 2);
Comments
Post a Comment