Public Group

Projectile Motion

This topic is 2794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

//Projectile.h #include<iostream> #include<string> using namespace std; #pragma once class Projectile { public: Projectile(); Projectile(float m_x, float m_y); string m_name; float m_x, m_y, m_angle, m_speed; void setName(string n); string getName(); void setX(float x); float getX(); void setY(float y); float getY(); void setAngle(float angle); void updatePosition(int time); void setSpeed(float speed); } //mainprog.cpp #include<iostream> #include "Projectile.h" #include "math.h" using namespace std; void main() { Projectile p[3]; float angle, speed; int i; string n; for(i = 0; i < 3; i++) { cout << "\nEnter name of Projectile " << i+1 << ": " << endl; cin >> n; p.setName(n); cout << "\nEnter Angle of Projectile " << i+1 << ": " << endl; cin >> angle; p.setAngle(angle); cout << "\nEnter initial speed of Projectile " << i+1 << ": " << endl; cin >> speed; p.setSpeed(speed); } } //Projectile.cpp #include<iostream> #include<string> #include "Projectile.h" void Projectile:setName(string n){ m_name = n; }; string Projectile::getName(){ return m_name; }; void Projectile::setX(float x){ m_x = x; }; float Projectile::getX(){ return m_x; }; void Projectile::setY(float y){ m_y = y; }; float Projectile::getY(){ return m_y; }; void Projectile::setAngle(float angle){ m_angle = angle*3.14159/180; }; void Projectile::updatePosition(int time){ m_x = cos(m_angle)*m_speed*time; m_y = sin(m_angle)*m_speed*time-0.5*9.81*time*time; }; void Projectile::setSpeed(float speed){ m_speed = speed; };

Hi,

I am trying to get this Projectile Motion thing working for some work I have to do. I have another piece of work coming up that requires that this be finished so if I don't get this working then I am screwed

The code above keeps spitting out error C2628: 'Projectile' followed by 'void' is illegal (did you forget a ';'?) for projectile.cpp Line 8 and error C2556: 'Projectile Projectile::setName(std::string)' : overloaded function differs only by return type from 'void Projectile::setName(std::string)' for projectile.cpp Line 8.

I have been staring at it for a while and I am still not seeing the problem (It;s 7:44 am o.o).

Any help would be appreciated.

Cheers,
Chris

Share on other sites

 //Projectile.cpp #include<iostream> #include<string> #include "Projectile.h" void Projectile:setName(string n){ m_name = n; };

The code above keeps spitting out error C2628: 'Projectile' followed by 'void' is illegal (did you forget a ';'?) for projectile.cpp Line 8 and error C2556: 'Projectile Projectile::setName(std::string)' : overloaded function differs only by return type from 'void Projectile::setName(std::string)' for projectile.cpp Line 8.

You've missed a colon ":" before setName().

Edit: Also, you've missed a semi-colon at the end of your class definition in Projectile.h (it happens to the best of us)

Also, also, seeing as you've made the member variables public in the class, you don't need getters/setters. Also, also, also, this is just a hunch, but should the UpdatePosition() really be taking time as an integer? I have no idea how your system works, but if time is in milliseconds, then m_speed would have to be incredible low...

Share on other sites
Thanks MajorTom.

I only started programming in September, before that I had never done it before, had looked into it a bit, but was studying Biology, Chemistry, Physics and Psychology so never needed to do programming

Thank you for helping with those errors (should have took my lecturers advice and had some sleep and looked at it when I woke up , ahh well, now I know what to look for should those errors ever appear again), I am just following an Activity given to my class on a PDF by our lecturer to the best of my (very limited) knowledge.

The Activity given to us is this:

Create a class, include library math.h, that stores details about a projectile, including: projectile name, x position, y position - in metres, including decimals, angle (0 < angle < 1.57), initial speed (m/s). Create suitable constructors, 'get' methods and 'set' methods. Create a setAngle function that converts an input angle to radians - radian angel = angle in degrees*3.14159/180.

A projectile's x and y position can be calculated as follows, assuming start position, x=0 and y=0, then:

x = cos(angle)*speed*time
y = sin(angle)*speed*time-0.5*9.81*time*time

Create a function updatePosition that takes one parameter, time, and use the above equations to re-calculate a projectile's position.

Create a 'main' program and an array of 3 projectiles. Within a loop, ask for a projectile's name, angle of the projectile and its initial speed. Use another loops that displays a projectile's position in 0.5 second intervals until its y position is <= 0.

Projectile 1: initial angle = 45, speed = 25.
Projectile 2: initial angle = 25, speed = 30.
Projectile 3: initial angle = 75, speed = 45.

Assuming x=y=0, output the projectile with its final position closest to 100m.[/quote]

As I have said, my knowledge is pretty limited as of now (That's why I am working at 7:44am on my day off. I am trying to learn more C++ as I feel my lessons are going too slowly (still making stupid mistakes though )) but because it is public, I could make the m_x, m_y etc. private to actually make use of the getters/setters? My lecturer has said to use getters/setters. I suppose it is only to make sure we know how to create/use them.

Looking back at the activity sheet, it seems I would need to time in float anyways, as I have to use 0.5 second increments for the second part.

Any help on clearing up my code would be helpful, just tryna be a better coder

Thanks again,

Chris

EDIT: Now I am getting a Linker error: Error 1 error LNK2019: unresolved external symbol "public: __thiscall Projectile::Projectile(void)" (??0Projectile@@QAE@XZ) referenced in function _main

Share on other sites
Regarding your linker errors, you don't seem to have defined either of your Projectile constructors (by which I mean the provided code doesn't have function bodies for them), which is probably causing the linker error you are getting. In case you are wondering, the function in question is referenced in your main function on the line "Projectile p[3];" which not only declares an array consisting of 3 Projectiles, it also constructs them.

Share on other sites
I started learning C++ around 3.5 years ago, though I didn't learn about creating classes for a long time - it was certainly longer than 6 months. Maybe the course was terrible, who knows?

I could make the m_x, m_y etc. private to actually make use of the getters/setters?

Definately. Though instead of making them private because you want to use the getters/setters - try to think about using getters/setters because you want them to be private - and only if you think the user of the class will need to access them.

Also, a great use for setters is to make sure a value is valid before changing the original. For example, I want to let the user pick a month of the year. In the setter, I can check if the integer value passed in is higher than 12, if it is, it's not valid, so maybe I shouldn't change the original month variable in the class?

just tryna be a better coder

Aren't we all?!

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 23
• 42
• 75