Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 561 562 [563] 564 565 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 902568 times)

Lightningfalcon

  • Bay Watcher
  • Target locked. Firing main cannon.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8430 on: November 20, 2015, 12:12:49 am »

Currently converting my comp sci project from c to c++. According to our prof we can apparently convert a single set of files at a time, which is... kind of working, I don't really know. Not getting errors about the rest of the project with a few exceptions.
So, main problem, I have very little experience with c++ still, don't really have any code to look back at yet, and my ability to google for stuff seems to have degraded. So can anyone point out what I'm doing wrong here?
First error that seems fixable (Don't get me started on what the fuck is going on with the unions)-

rt.c:83:79: error: cannot convert ‘Vector (*)()’ to ‘Vector*’ in argument passing
 wtf = scene.obj[j].intersect(ray, scene.obj[j], &normal, &intpnt, &scale);

intersect is a function pointer which will be altered into a virtual function in the future(I think). 
int (*intersect)(RAY_T ray, struct OBJ_T obj, Vector  *normal, Vector *intpnt, double *scale);

Next error is
 error: assignment of function ‘Vector closeNormal()’
closeNormal = normal;

closeNormal and normal are both supposed to be Vectors, and I just want closeNormal to equal normal. Both were declared as-
Vector closeNormal();
Vector normal();

All of this worked when Vector was just a struct, and I haven't changed any of the actual logic yet. Just changed vector, and then went though and made sure everything matched the new function and class names.

Sorry about poor formatting, really tired from looking at a minimum of 60+ errors per compile.
Logged
Interdum feror cupidine partium magnarum circo vincendarum
W-we just... wanted our...
Actually most of the people here explicitly wanted chaos and tragedy. So. Uh.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8431 on: November 20, 2015, 12:32:56 am »

Quote
rt.c:83:79: error: cannot convert ‘Vector (*)()’ to ‘Vector*’ in argument passing
 wtf = scene.obj[j].intersect(ray, scene.obj[j], &normal, &intpnt, &scale);
...
int (*intersect)(RAY_T ray, struct OBJ_T obj, Vector  *normal, Vector *intpnt, double *scale);

Well, this sounds like you're passing a function pointer instead of a Vector. And the other line explains the typo:

Quote
Vector closeNormal()

^ this shouldn't have brackets on it, since "Vector thing()" is actually the declaration of a function called "thing" that returns a Vector. You just need to drop the brackets.
« Last Edit: November 20, 2015, 12:48:01 am by Reelya »
Logged

Lightningfalcon

  • Bay Watcher
  • Target locked. Firing main cannon.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8432 on: November 20, 2015, 01:09:55 am »

Got rid of the parentheses, which actually got me back to a different set of errors that I originally had before adding the quotes. Double checked my header and realized I forgot to include a statement for the constructor with no arguments. Oops. That seems to have solved that set of errors. Now I have less then thirty. I can scroll through the list without my eyes bleeding!

So now it seems that the majority of my problems mainly deal with scope. Compiler is complaining that x, y, and z are all out of scope.
in header file-
 class Vector
{
   public:
   double x, y, z;

   Vector add(Vector v2);
}

in cpp file-
Vector add(Vector v2)
{
   Vector vec(x + v2.x,
              y + v2.y,
              z + v2.z);
   return vec;
}
Its not complaining about v2.x being out of scope, just x.  I have it set up in another program I did in lab exactly like this, and it worked there.
Logged
Interdum feror cupidine partium magnarum circo vincendarum
W-we just... wanted our...
Actually most of the people here explicitly wanted chaos and tragedy. So. Uh.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8433 on: November 20, 2015, 01:23:48 am »

you need to write:

Vector Vector::add(Vector v2)

For the function body when It's outside the class. That lets the compiler know that it's the Vector class' "add" function and not some random function that's also called "add".

But you have a semantic issue here. Your "add" function doesn't actually change the class that it's part of

Quote
Vector v1;
Vector v2;
v1.add(v2); // this line should add v2 to v1, and change v1.

If you want a function that adds two vectors and doesn't change either of them, define the whole thing outside the class

Vector add(Vector v1, Vector v2);
« Last Edit: November 20, 2015, 01:28:17 am by Reelya »
Logged

Lightningfalcon

  • Bay Watcher
  • Target locked. Firing main cannon.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8434 on: November 20, 2015, 01:28:10 am »

you need to write:

Vector Vector::add(Vector v2)

For the function body when It's outside the class. That lets the compiler know that it's the Vector class' "add" function and not some random function that's also called "add".
Thanks. Now I just need to do that to the rest of my functions, and I can actually get started on converting the rest of this program. Its going to be a long weekend.
Logged
Interdum feror cupidine partium magnarum circo vincendarum
W-we just... wanted our...
Actually most of the people here explicitly wanted chaos and tragedy. So. Uh.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8435 on: November 20, 2015, 01:29:34 am »

One last recommendation to keep things semantically clear in your programs that use objects:

Your "add" function doesn't actually change the class that it's part of

Quote
Vector v1;
Vector v2;
v1.add(v2); // this line should add v2 to v1, and change v1 in the process.

If you want a function that adds two vectors and doesn't change either of them, define the whole thing outside the class

Vector add(Vector v1, Vector v2); // adds two vectors together and makes a new one.

You can make regular functions outside any particular instance of a class when it makes more semantic sense to do so.

But you can also think in terms of verbs and nouns. A verb "add" should add something to something else. If you just want information that maybe should be a noun:

Vector sum(Vector v1, Vector v2);

It's more clear that this returns a value rather than adds one of the values to the other. It's even better if you write an operator+ outside the class, then there's no possibility of someone mistaking "sum" for a command that adds one thing to the other:

Vector Operator+(Vector v1, Vector v2); // then you can write Vector v3 = v1 + v2;
« Last Edit: November 20, 2015, 01:35:12 am by Reelya »
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8436 on: November 20, 2015, 01:32:49 am »

You can do it within the class, though too, unless javascript is dumb.

In C#, for example, I would do v1 = Vector.add(v1, v2);

where the function is a static function for the vector class.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8437 on: November 20, 2015, 01:37:10 am »

but then you have additional syntax to keep track of. We're talking C++ btw, none of this is relevant to Javascript.

In C++ it would have to be a static function, and you'd write it as Vector::add(v1, v2); which is worse than add(v1, v2); or v1+v2

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8438 on: November 20, 2015, 01:40:30 am »

C++ I'd do it with operator overloading if I could get away with it.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8439 on: November 20, 2015, 01:43:17 am »

The operator overloading in c++ is a more versatile than C# from what I've seen

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8440 on: November 20, 2015, 01:47:59 am »

C++ you can overload the () operator, even. That's the only difference I've encountered so far, though.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8441 on: November 20, 2015, 01:57:28 am »

No, there are a ton more.

https://msdn.microsoft.com/en-us/library/8edha89s%28v=vs.71%29.aspx
http://en.cppreference.com/w/cpp/language/operators

Here's a partial list of things that can be overloaded in C++ but not in C#

() and [],
= assignment operators,
- new and delete, for custom memory-management (e.g. pooling).
-  && and ||
-> operator (though it must still return a valid pointer)

Lightningfalcon

  • Bay Watcher
  • Target locked. Firing main cannon.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8442 on: November 20, 2015, 02:02:04 am »

I have plans for adding operator overloading eventually, and I think it is actually a requirement in the list of things we have to have. Going to get all the code working in C++ first before I dare to touch even the naming schemes, any more then I have to. I do have some plans for some organizational overhaul, but this way I know that any problems with the code is coming from the port, rather then any kind of new addition.
Logged
Interdum feror cupidine partium magnarum circo vincendarum
W-we just... wanted our...
Actually most of the people here explicitly wanted chaos and tragedy. So. Uh.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8443 on: November 20, 2015, 02:05:28 am »

There's no reason you can't have the function versions and the operator versions both implemented at the same time, so don't strip out the old code, just copy the needed "add" functions etc and change the function name to be the operator version. That's often all you have to do.

e.g. "Vector add(Vector rhs)" like you had before, just becomes "Vector operator+(Vector rhs)" and it's done and working.
« Last Edit: November 20, 2015, 02:08:21 am by Reelya »
Logged

Mesa

  • Bay Watcher
  • Call me River.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8444 on: November 20, 2015, 03:36:31 am »

lol, just put a damn semi-colon at the end of each line to save yourself a whole layer of bullshit later on. That Javascript "too few semi-colons" problem is self-inflicted and actually makes you worse at doing proper c-style languages that Javascript is based on. In other words, for the few c-style languages where semi-colons are optional, just put them in anyway. It's one less thing you have to give a fuck about then.

I think I had to remove them to fix some other issue, but that might've just been my misuse of vars (since for some reason we never use them in my class, so I wasn't fully aware of where to put them...).
Either way it's fixed now and I get that at the of the day it was me who fucked up. Lesson learned.

Anyway, yesterday I also worked in a proper jumping animation (not a huge addition overall but it took me like 40 minutes total with my puny skills).

I'll be doing some work on adding a score system and fiddle around with JSON (was going to use XML, but for JavaScript JSON seems like the more straightforward solution) so that I can have multiple levels (saved as external JSON files), though I will need some way of actually switching between them.

Either way I'll tackle those problems when I get to them. :v
Logged
Pages: 1 ... 561 562 [563] 564 565 ... 796