QT and C++

This is for discussions about news, politics, sports, other games, culture, philosophy etc.
France Kaiserklein
Pro Player
Posts: 10281
Joined: Jun 6, 2015
Location: Paris
GameRanger ID: 5529322

QT and C++

Post by Kaiserklein »

For only one fucking exercise, I'm supposed to use a code and a software I never used before... And I can't get it to work. There's a QT project, and I'm supposed to code a few functions in C++, about the Lagrange interpolation. So here's the QT project:
pack of shit.zip
(20.06 KiB) Downloaded 15 times

(the main file to work on is Ex1_LagrangePolynomial/Ex1_LagrangePolynomial.cpp)

Here's the link to the exercise sheet: http://gfx.uni-kl.de/~alggeom/exercise/WS1718/Ex1.pdf (it's the exercise 4)

And here's the code I did for one of the functions (which doesn't work because for the life of me I couldn't get variables from a scope to another in fucking C++):

Code: Select all

std::vector<float> LagrangePolynomial::computeLagrangeBasisDenominators() {
    float xi, xj;
    std::vector<float> d = std::vector<float>(); // Array containing all the denominators to return
    for (int i=0; i<3; i++) {
       float di = 1; // Denominator number i
        xi = p[i][0]; // Find a way to bring fucking p in here
        for (int j = 0; j < 3; j++) {
            if(j != i) {
                xj = p[j][0];
                di *= (xi - xj);
            }
        }
        d.push_back(di);
    }
    return d;
}


The code itself should be easy, the algorithmic is basic. But I just can't seem to use this software and I have no idea about C++. I don't even understand how to call a function, which I need to do if I want to test my code.


So plz halp anyone, wud be veri much appreciated :'(
Image
Image
Image
LoOk_tOm wrote:I have something in particular against Kaisar (GERMANY NOOB mercenary LAMME FOREVER) And the other people (noobs) like suck kaiser ... just this ..
User avatar
Nauru Dolan
Ninja
Posts: 13068
Joined: Sep 17, 2015

Re: QT and C++

Post by Dolan »

I can't take a serious look at this right now, because I'm in the middle of an emergency, but from what I remember, you can define a variable in a class with public access and then use that scope to call it in another class.

For example:

Code: Select all

class A {
  public: static int x;   // declare variable x
};

class B {
  public:
    void func() {
      A::x = 2;   // use variable x declared in another scope
    }
};


Then you could instantiate these classes in objects. Ie:

Code: Select all

A *obj = new A(); // on the heap
A obj; // on the stack


Well, these are some generic examples to give you an idea on how to use a variable declared in another scope.
No Flag deleted_user
Ninja
Posts: 14364
Joined: Mar 26, 2015

Re: QT and C++

Post by deleted_user »

I could do it but I don't feel like it.

All that's needed is a simple conversion into Java.
Canada Dankl
Crossbow
Posts: 38
Joined: Feb 13, 2016

Re: QT and C++

Post by Dankl »

I think you want to pass p in as a parameter to the function computeLagrangeBasisDenominators. So, the function definition would look like,
computeLagrangeBasisDenominators (float[3][3] p) {...}.
Then in your main when you want to call the function you would say,
Vector<float> ans = computeLagrangeBasisDenominators(p);
Assuming you have some 3x3 float array called p.

Sorry for formatting. I'm on mobile.
User avatar
Germany QueenOfdestiny
Retired Contributor
Posts: 2139
Joined: Aug 9, 2016
ESO: QueenOfdestiny

Re: QT and C++

Post by QueenOfdestiny »

Nerd thread
shit juice :hmm:
France Kaiserklein
Pro Player
Posts: 10281
Joined: Jun 6, 2015
Location: Paris
GameRanger ID: 5529322

Re: QT and C++

Post by Kaiserklein »

@Dolan the problem is that I need to keep the projects the teachers gave me, and just code 3 functions. Everything else should be left untouched, I think. So I can't change the way they declared their variables.


@Dankl I thought about that, it makes most sense imo. However, in the C++ header file (which I guess kind of declares the functions and global variables used in the C++ source file..?), I have:

Code: Select all

private:
   std::vector<float> computeLagrangeBasisDenominators();

Which means that the computeLagrangeBasisDenominators function doesn't take any parameter, right? And since I'm not supposed to touch the framework I was given, but only the bodies of 3 functions, I'm afraid I can't do that :S


But thanks a lot guys for helping anyway, it's much appreciated
Image
Image
Image
LoOk_tOm wrote:I have something in particular against Kaisar (GERMANY NOOB mercenary LAMME FOREVER) And the other people (noobs) like suck kaiser ... just this ..
User avatar
Netherlands Goodspeed
Retired Contributor
Posts: 13005
Joined: Feb 27, 2015

Re: QT and C++

Post by Goodspeed »

(which doesn't work because for the life of me I couldn't get variables from a scope to another in fucking C++)
Clarify? Are you unable to call the function or unable to access variables that you need inside the function?
I'm guessing the latter because in your code it says "find a way to get fucking p in here". Is p a field in the same class your function is in? Then you can use it by saying this->p
France Kaiserklein
Pro Player
Posts: 10281
Joined: Jun 6, 2015
Location: Paris
GameRanger ID: 5529322

Re: QT and C++

Post by Kaiserklein »

Goodspeed wrote:
(which doesn't work because for the life of me I couldn't get variables from a scope to another in fucking C++)
Clarify? Are you unable to call the function or unable to access variables that you need inside the function?
I'm guessing the latter because in your code it says "find a way to get fucking p in here". Is p a field in the same class your function is in? Then you can use it by saying this->p

I am unable to access variables from inside the function. Plus, I am also unable to call my function to test it, because I can't get to run my code in QT and get output in a console.

p is a variable that was declared in, I think, another function of the same class:

Code: Select all

LagrangePolynomial::LagrangePolynomial() : OpenGLWindow()

{
   std::vector<Vector> p = std::vector<Vector>();
   Vector v = Vector(4);
   v[3] = 1.0;
   v[0] = -1.0;
   v[1] = v[2] = 0.0;
   p.push_back(Vector(v));
   v[0] = 0.0;
   v[1] = 1.0;
   p.push_back(Vector(v));
   v[0] = 1.0;
   v[1] = -1.0;
   p.push_back(Vector(v));
}


According to what the exercise says, p is indeed the variable I need to use. But it wasn't declared in the header file (which is where some global variables were declared I think), instead it was declared in this weird function. And I need to use it in another function of the same class.
Image
Image
Image
LoOk_tOm wrote:I have something in particular against Kaisar (GERMANY NOOB mercenary LAMME FOREVER) And the other people (noobs) like suck kaiser ... just this ..
User avatar
Argentina AraGun
Lancer
Posts: 516
Joined: Nov 15, 2015
ESO: AraGun_OP
Location: Buenos Aires

Re: QT and C++

  • Quote

Post by AraGun »

Pls report kaiser for cheating on his homework.
Canada Dankl
Crossbow
Posts: 38
Joined: Feb 13, 2016

Re: QT and C++

Post by Dankl »

That weird function is the constructor (because it has the same name as the class). It creates a temporary variable p and stores it in the curve supportPoints. To access it you need the line,

std::vector<Vector> p = supportPoints.getPositions();

inside your function.
India Ashvin
Retired Contributor
Posts: 2432
Joined: Jul 6, 2016
ESO: Octanium

Re: QT and C++

Post by Ashvin »

Passing p as an argument it is kinda hard to see how that would work, are you sure you are not allowed to do that?
Image
France Kaiserklein
Pro Player
Posts: 10281
Joined: Jun 6, 2015
Location: Paris
GameRanger ID: 5529322

Re: QT and C++

Post by Kaiserklein »

Dankl wrote:That weird function is the constructor (because it has the same name as the class). It creates a temporary variable p and stores it in the curve supportPoints. To access it you need the line,

std::vector<Vector> p = supportPoints.getPositions();

inside your function.

Wow, I don't get any error anymore when I declare p the way you said. Sounds good, thanks a lot!
Do you know how I can test it now, though? When I run the project, it just launches an empty command prompt, in which I can't type anything, and nothing happens. I guess I need to call my function somehow?


Ashvin wrote:Passing p as an argument it is kinda hard to see how that would work, are you sure you are not allowed to do that?

I'm pretty sure I'm not, though I could ask. But with Dankl's solution, I guess I don't need to.
Image
Image
Image
LoOk_tOm wrote:I have something in particular against Kaisar (GERMANY NOOB mercenary LAMME FOREVER) And the other people (noobs) like suck kaiser ... just this ..
No Flag deleted_user0
Ninja
Posts: 13004
Joined: Apr 28, 2020

Re: QT and C++

Post by deleted_user0 »

Code: Select all

class A {Somppukunkku, Class D: Princeofkabul
  public: static int x;   // declare variable x = Somppukunkku god. Somppukunkku > Princeofkabul)
};

class B {GAY SECTION: Princeofkabul]
  public:
    void func(Somppukunkku > Princeofkabul) {
      A::x = 2;   // use Taunt 11 to show how bad Princeofkabul is
    }


Here are the solutions for your homeworks....
France Kaiserklein
Pro Player
Posts: 10281
Joined: Jun 6, 2015
Location: Paris
GameRanger ID: 5529322

Re: QT and C++

Post by Kaiserklein »

Taunt 12
Image
Image
Image
LoOk_tOm wrote:I have something in particular against Kaisar (GERMANY NOOB mercenary LAMME FOREVER) And the other people (noobs) like suck kaiser ... just this ..

Who is online

Users browsing this forum: No registered users and 3 guests

Which top 10 players do you wish to see listed?

All-time

Active last two weeks

Active last month

Supremacy

Treaty

Official

ESOC Patch

Treaty Patch

1v1 Elo

2v2 Elo

3v3 Elo

Power Rating

Which streams do you wish to see listed?

Twitch

Age of Empires III

Age of Empires IV