so, i have a main method
int main () {
int x = 0;
inc(x);
inc(x);
inc(x);
std::cout << x << std::endl;
}
I'm trying to get my output to be '3' but can't figure out why everytime inc(x) is called x resets to 0.
my inc method:
int inc(int x){
++x;
std::cout << "x = " << x << std::endl;
return x;
}
my output:
x = 1
x = 1
x = 1
0
Why does x reset after every call to inc(x) and how can i fix this without editing my main function
Instead of
inc(x);
I think you need
x = inc(x);
You may slap your head now.
You're passing "x" to inc() by value, so changes made in inc() won't be visible in main().
If you want x to be changed you need to pass it by reference and not as a value. Also inc() would need to return void for that to make sense. here is an example.
// adding & before x passes the reference to x
void inc(int &x){
++x;
std::cout << "x = " << x << std::endl;
}
int main() {
int x = 0;
inc(x);
inc(x);
inc(x);
std::cout << x << std::endl;
}
this prints
x = 1
x = 2
x = 3
3
Related
Just wondering if there is a line change I can make in order to find odds instead of evens? Here's what I got:
#include <iostream>
#include <conio.h>
#include "stdafx.h"
void numOdd(int x, int y)
{
std::cout << x << " ";
if (x < y)
{
numOdd(x + 2, y);
}
}
int main()
{
int x = 0;
int y = 0;
std::cout << "Up to what num to find odd nums? " << std::endl;
std::cin >> y;
numOdd(x, y);
_getch();
}
Initialize x to 1 instead of 0.
Aso, be advised that recursion is not a great fit for this problem; a simple loop would be more appropriate.
Im trying to use perspectiveTransform but I keep getting error. I tried to follow the solution from this thread http://answers.opencv.org/question/18252/opencv-assertion-failed-for-perspective-transform/
_players[i].getCoordinates() is of type Point
_homography_matrix is a 3 x 3 Mat
Mat temp_Mat = Mat::zeros(2, 1, CV_32FC2);
for (int i = 0; i < _players.size(); i++)
{
cout << Mat(_players[i].get_Coordinates()) << endl;
perspectiveTransform(Mat(_players[i].get_Coordinates()), temp_Mat, _homography_matrix);
}
Also, how do I convert temp_Mat into type Point ?
OpenCV Error: Assertion failed (scn + 1 == m.cols) in cv::perspectiveTransform
Basically you just need to correct from
Mat(_players[i].get_Coordinates()) ...
to
Mat2f(_players[i].get_Coordinates()) ...
In the first case you are creating a 2x1, 1 channel float matrix, in the second case (correct) you create a 1x1, 2 channel float matrix.
You also don't need to initialize temp_Mat.
You can also use template Mat_ to better control the types of your Mats. E.g. creating a Mat of type CV_32FC2 is equivalent to create a Mat2f.
This sample code will show you also how to convert back and forth between Mat and Point:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
// Some random points
vector<Point2f> pts = {Point2f(1,2), Point2f(5,10)};
// Some random transform matrix
Mat1f m(3,3, float(0.1));
for (int i = 0; i < pts.size(); ++i)
{
cout << "Point: " << pts[i] << endl;
Mat2f dst;
perspectiveTransform(Mat2f(pts[i]), dst, m);
cout << "Dst mat: " << dst << endl;
Point2f p(dst(0));
cout << "Dst point: " << p << endl;
}
return 0;
}
In c++ I can add a reference to a value type, for example :
int a = 12;
int &b = a;
a--;
cout << "a = " << a << ", b = " << b << endl;
Will give :
a = 11, b = 11
Is there a way to do the same in vala without using pointers ?
Is there a way to do the same in vala
Yes.
without using pointers ?
No.
If, however, you are passing them to a function, you can use a ref parameter:
void decrement (ref value) {
value--;
}
void do_stuff () {
int a = 12;
decrement (ref a);
assert (a == 11);
}
I have a question about PhysX SDK 2.8.1
I'm an actor:
NxActorDesc actorDesc;
NxBodyDesc bodyDesc;
NxSphereShapeDesc sphereDesc;
sphereDesc.radius = 1.5f;
actorDesc.shapes.pushBack(&sphereDesc);
actorDesc.body = &bodyDesc;
actorDesc.density = 10;
actorDesc.globalPose.t = NxVec3(0.0f, 25.0f, 0.0f);
NxActor *dynamicActor = gsc->createActor(actorDesc);
I want the console print out the current position of the actor. How to do it? This below doesn't work:
for (int i = 0; i <= 10; i++) {
//Step PhysX simulation
if (gsc)
StepPhysX();
NxMat34 pose = dynamicActor->getGlobalPose();
cout <<pose.t << endl;
}
Specifically depends on my reading the position Y.
std::cout can't take an NxVec3 (which your post.t is).
If you want to print out the global position of your dynamicActor,
you need to print out X, Y, Z components of your NxVec3 variable separately.
NxVec3 trans = dynamicActor->getGlobalPose().t; // or you could use "dynamicActor->getGlobalPosition()"
std::cout << trans.x << ", " << trans.y << ", " << trans.z << std::endl;
So I've recently started using XCode and have an issue with trying to compile C++ code: Even a basic program like
#include <iostream>
int main()
{
int x = 0;
std::cout << "Enter a value for X: " << std::endl;
std::cin >> x;
std::cout << "X is " + x << std::endl;
return 0;
}
Gives me no output (under "All Output" I get):
Enter a value for X:
5