• What Do You Need Help With? V6
    7,544 replies, posted
Hey all, I'm pretty new to C++ (second semester of learning it at a community college, no experience otherwise), and I'm stuck on one of the problems we've been given. We're supposed to overload the standard increment and decrement operators for use with a class we're designing. I have, to the best of my knowledge, correctly overloaded them, but I'm getting errors telling me that I can't decrement a pointer of my class type. My prototypes in the header file: [code]Month operator ++(); Month operator ++(int); Month operator --(); Month operator --(int);[/code] The operators themselves: [code]Month Month::operator ++(){ monthNumber = ( monthNumber == 12 ) ? 1 : monthNumber += 1; name = cp.getNameFromNum(monthNumber); return *this; } Month Month::operator ++(int){ Month temp(name); monthNumber = ( monthNumber == 12 ) ? 1 : monthNumber += 1; name = cp.getNameFromNum(monthNumber); return temp; } Month Month::operator --(){ monthNumber = ( monthNumber == 1 ) ? 12 : monthNumber -= 1; name = cp.getNameFromNum(monthNumber); return *this; } Month Month::operator --(int){ Month temp(name); monthNumber = ( monthNumber == 1 ) ? 12 : monthNumber -= 1; name = cp.getNameFromNum(monthNumber); return temp; }[/code] The code that calls it: [code]{ Month month(); std::cout << month << std::endl; std::cout << "Now testing the ++ operators." << std::endl; ++month; std::cout << month; month++; std::cout << month; } { Month month(); std::cout << month << std::endl; std::cout << "Now testing the -- operators." << std::endl; --month; std::cout << month; month--; std::cout << month; }[/code] The error specifically: [code]ch14pc7.cpp:18:4: error: ISO C++ forbids incrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:18:4: error: lvalue required as an increment operand ch14pc7.cpp:20:7: error: ISO C++ forbids incrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:20:7: error: lvalue required as an increment operand ch14pc7.cpp:26:4: error: ISO C++ forbids decrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:26:4: error: lvalue required as an decrement operand ch14pc7.cpp:28:7: error: ISO C++ forbids drecrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:28:7: error: lvalue required as an drecrement operand[/code] I'm entirely unsure what it's trying to tell me, despite googling for solutions. I figured it meant I wasn't overloading the operator properly, but I'm following the examples in the textbook as closely as possible.
MS Sql question: Is there a way to 'order by' a nvarchar like: [CODE] @SortString nvarchar(255) = 'UserName DESC, Name ASC' select * from [table] order by @SortString [/CODE] Currently the sql is put into a string-variable and that one is executed like: [CODE]exec @SqlStatement // variables[/CODE]
I need help with creating sets. Basically I have a list of objects [code] container 1: a container 2: a b container 3: a container 4: a b c[/code] And I need to create branching sets like aaaa, aaab, aaac, abaa, abab, abac. It's in C# and everything is in List objects so foreach'es are possible. Also can be variable size. Do some LINQ voodoo or recursive function building?
[QUOTE=aurum481;44233285]I need help with creating sets. Basically I have a list of objects [code] container 1: a container 2: a b container 3: a container 4: a b c[/code] And I need to create branching sets like aaaa, aaab, aaac, abaa, abab, abac. It's in C# and everything is in List objects so foreach'es are possible. Also can be variable size. Do some LINQ voodoo or recursive function building?[/QUOTE] LINQ voodoo: [code]from x1 in c1 from x2 in c2 ... select new []{ x1, x2, ... }[/code] This is shorthand for nested [I].SelectMany[/I] and [I].Select[/I] calls, so you get an enumerable of all the possible arrays (or whatever you create in the select clause). If you have variable amounts of containers then you can use this syntax together with recursive calls, but it's not quite as nice.
[QUOTE=biodude94566;44231819]Hey all, I'm pretty new to C++ (second semester of learning it at a community college, no experience otherwise), and I'm stuck on one of the problems we've been given. We're supposed to overload the standard increment and decrement operators for use with a class we're designing. I have, to the best of my knowledge, correctly overloaded them, but I'm getting errors telling me that I can't decrement a pointer of my class type. My prototypes in the header file: [code]Month operator ++(); Month operator ++(int); Month operator --(); Month operator --(int);[/code] The operators themselves: [code]Month Month::operator ++(){ monthNumber = ( monthNumber == 12 ) ? 1 : monthNumber += 1; name = cp.getNameFromNum(monthNumber); return *this; } Month Month::operator ++(int){ Month temp(name); monthNumber = ( monthNumber == 12 ) ? 1 : monthNumber += 1; name = cp.getNameFromNum(monthNumber); return temp; } Month Month::operator --(){ monthNumber = ( monthNumber == 1 ) ? 12 : monthNumber -= 1; name = cp.getNameFromNum(monthNumber); return *this; } Month Month::operator --(int){ Month temp(name); monthNumber = ( monthNumber == 1 ) ? 12 : monthNumber -= 1; name = cp.getNameFromNum(monthNumber); return temp; }[/code] The code that calls it: [code]{ Month month(); std::cout << month << std::endl; std::cout << "Now testing the ++ operators." << std::endl; ++month; std::cout << month; month++; std::cout << month; } { Month month(); std::cout << month << std::endl; std::cout << "Now testing the -- operators." << std::endl; --month; std::cout << month; month--; std::cout << month; }[/code] The error specifically: [code]ch14pc7.cpp:18:4: error: ISO C++ forbids incrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:18:4: error: lvalue required as an increment operand ch14pc7.cpp:20:7: error: ISO C++ forbids incrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:20:7: error: lvalue required as an increment operand ch14pc7.cpp:26:4: error: ISO C++ forbids decrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:26:4: error: lvalue required as an decrement operand ch14pc7.cpp:28:7: error: ISO C++ forbids drecrementing a pointer of type 'Month (*)()' [-fpermissive] ch14pc7.cpp:28:7: error: lvalue required as an drecrement operand[/code] I'm entirely unsure what it's trying to tell me, despite googling for solutions. I figured it meant I wasn't overloading the operator properly, but I'm following the examples in the textbook as closely as possible.[/QUOTE] The error [code]ISO C++ forbids incrementing a pointer of type 'Month (*)()'[/code] talks about a pointer to a function returning a Month, because the compiler thinks "month" is a function and not an object. The problem is your instantiation syntax of Month: [code]Month month(); // This declares a function month() that returns a Month, you don't want that Month month; // This is what you want, create an instance of Month[/code] [B]edit:[/B] Also, your prefix versions should return a reference to *this, not a copy: [code]Month m(1); // January (just to demonstrate, not sure if this constructor exists) ++++m; // You expect m to be 3/March; for this to work, the operator needs to be applied twice to the same object, hence the reference return value[/code]
Can I get some help please! I'm doing my assignment (first year of college) and I've encountered a problem when doing it and it's been bothering me for a while, this is what I have. [code]#include <stdlib.h> #include <stdio.h> #include <string.h> struct match_struct { char team1[20]; char team2[20]; int team1_score; int team2_score; }; main() { FILE *match1; FILE *match2; struct match_struct match; int i = 0; match1 = fopen("matches.txt", "r"); if (match1 == NULL) { printf("Error opening the file matches.txt.\n"); exit(1); } match2 = fopen("matches.bin", "wb"); if (match2 == NULL) { printf("Error opening the file matches.bin.\n"); exit(1); } while( fscanf(match1, "%[^,] ,%[^,] ,%i, %i", match.team1, match.team2, &match.team1_score, &match.team2_score) != EOF) { fwrite(match, sizeof(struct match_struct), 1, match2); } } [/code] I have to convert the txt file into a binary file. I keep getting the error: Error E2285 assignment.c 37: Could not find a match for 'fwrite(match_struct,unsigned int,int,FILE *)' in function main() Can someone lend me a hand please, it's bugging me. PS it's c, btw.
is general help supposed to go here as well, related to programming? if so, any tips for a guy trying to get into java? I have no real long term goal other than learning to program in it, which I believe is the reason I'm so demotivated to continue my courses. I know a few very basic things though, like how to make a simple terminal calculator which loops. I really want to get into this, but it's hard to keep the motivation up. currently following a course on udemy (which was included in a learn to code bundle) which can be found here [url]https://www.udemy.com/learn-to-program-with-java/#/[/url] afraid that I'll completely ditch the course because of my lack of motivation, and then forget the little I've learned. really want to get into this because I'm having a great time once I actually got the motivation
[QUOTE=Dienes;44233936]The problem is your instantiation syntax of Month: [code]Month month(); // This declares a function month() that returns a Month, you don't want that Month month; // This is what you want, create an instance of Month[/code][/QUOTE] Thanks, I keep doing that every once and a while. :downs:
[QUOTE=Kingbob387;44235335]Can I get some help please! [/QUOTE] Look up the [URL="http://www.cplusplus.com/reference/cstdio/fwrite/"]documentation for fwrite[/URL] The first parameter is a pointer to the first element in an array. An array of size one is just a pointer to one object. You aren't passing a pointer you are passing an object. Basically do this: [code]fwrite(&match, sizeof(struct match_struct), 1, match2);[/code] [editline]14th March 2014[/editline] [QUOTE=PredGD;44235741]is general help supposed to go here as well, related to programming? [/QUOTE] If you're just having general motivation issues and want projects to work on there's [URL="https://projecteuler.net/"]Project Euler[/URL] and [URL="http://community.topcoder.com/tc?module=Static&d1=help&d2=pracArena"]Top Coder Practice Rooms[/URL]. However this won't, of course, actually teach you java or programming or data structures. But if you want stuff to work on to apply the stuff you learn elsewhere these can be nice.
[QUOTE=thrawn2787;44237912]Look up the [URL="http://www.cplusplus.com/reference/cstdio/fwrite/"]documentation for fwrite[/URL] The first parameter is a pointer to the first element in an array. An array of size one is just a pointer to one object. You aren't passing a pointer you are passing an object. Basically do this: [code]fwrite(&match, sizeof(struct match_struct), 1, match2);[/code] [editline]14th March 2014[/editline] If you're just having general motivation issues and want projects to work on there's [URL="https://projecteuler.net/"]Project Euler[/URL] and [URL="http://community.topcoder.com/tc?module=Static&d1=help&d2=pracArena"]Top Coder Practice Rooms[/URL]. However this won't, of course, actually teach you java or programming or data structures. But if you want stuff to work on to apply the stuff you learn elsewhere these can be nice.[/QUOTE] I love you you're a life saver. Have some love from me <3
Hey guys, I'm currently learning about 8086 assembly and the compiler the professor gave us isn't working on my computer since I got win8 64 bits. So does anyone know about an emulator to test my code on or a compiler that works on win8? Thanks. Edit: I found a compiler that works, now how can I run 16 bits exe on a 64 bits OS?
[QUOTE=Kingbob387;44235335]Can I get some help please! I'm doing my assignment (first year of college) and I've encountered a problem when doing it and it's been bothering me for a while, this is what I have. [code]#include <stdlib.h> #include <stdio.h> #include <string.h> struct match_struct { char team1[20]; char team2[20]; int team1_score; int team2_score; }; main() { FILE *match1; FILE *match2; struct match_struct match; int i = 0; match1 = fopen("matches.txt", "r"); if (match1 == NULL) { printf("Error opening the file matches.txt.\n"); exit(1); } match2 = fopen("matches.bin", "wb"); if (match2 == NULL) { printf("Error opening the file matches.bin.\n"); exit(1); } while( fscanf(match1, "%[^,] ,%[^,] ,%i, %i", match.team1, match.team2, &match.team1_score, &match.team2_score) != EOF) { fwrite(match, sizeof(struct match_struct), 1, match2); } } [/code] I have to convert the txt file into a binary file. I keep getting the error: Error E2285 assignment.c 37: Could not find a match for 'fwrite(match_struct,unsigned int,int,FILE *)' in function main() Can someone lend me a hand please, it's bugging me. PS it's c, btw.[/QUOTE] Quick question does this look about right in converting the txt file into a binary file?
[QUOTE=evil-tedoz;44238473]I found a compiler that works, now how can I run 16 bits exe on a 64 bits OS?[/QUOTE] You can't. 16 bit backwards compatibility is not available in 64 bit versions of Windows.
[QUOTE=Z_guy;44242638]You can't. 16 bit backwards compatibility is not available in 64 bit versions of Windows.[/QUOTE] Thanks, I figured out I could use dosbox to run those programs :).
Does anyone understand how hashing works I still am having problems understanding it
[QUOTE=pyschomc;44248159]Does anyone understand how hashing works I still am having problems understanding it[/QUOTE] Take a set of numbers, say, 2 4 6 8 10 12, then add them together. The hash is 42.
Question about primitive restart in OpenGL. I enabled primitive restart: [code]glEnable (GL_PRIMITIVE_RESTART);[/code] and I set my restart index: [code]glPrimitiveRestartIndex (-1);[/code] and after every plane (this is bsp) I am inserting -1 into the index array. But it just refuses to work: with or without that index the result is still the same. Is there something I'm missing?
[QUOTE=Jookia;44248310]Take a set of numbers, say, 2 4 6 8 10 12, then add them together. The hash is 42.[/QUOTE] Pretty much. Usually you'd try to get a somewhat even distribution so performance with hashtables (which use modulo on the hash to sort it into buckets) is better, but generally speaking hashing just means making a small value out of a larger one while losing information. (I think they are also called one-way compression functions.) Other stuff like cryptographic strength divides hash functions into further sub groups, the most well known hash functions just take arbitrary binary strings and turn them into values of a certain size. [editline]16th March 2014[/editline] [QUOTE=Bumrang;44249084]Question about primitive restart in OpenGL. I enabled primitive restart: [code]glEnable (GL_PRIMITIVE_RESTART);[/code] and I set my restart index: [code]glPrimitiveRestartIndex (-1);[/code] and after every plane (this is bsp) I am inserting -1 into the index array. But it just refuses to work: with or without that index the result is still the same. Is there something I'm missing?[/QUOTE] Are you sure you're using a signed number format for the index buffer? Iirc all integer types are unsigned by default in OpenGL and you can get signed versions by prepending s.
[QUOTE=Tamschi;44249110] Are you sure you're using a signed number format for the index buffer? Iirc all integer types are unsigned by default in OpenGL and you can get signed versions by prepending s.[/QUOTE] You mean using GLint instead of GLuint? [editline]rrerr[/editline] if I change the index to something like 65535 I still get the same result
Did you call glPrimitiveRestartIndex with 65535 AND replace your -1s in the array with 65535? Only thing I can think of
Is there something wrong with my trig? The units seem to move all over the place: [code]if (distanceToNextCell() > 0) xspeed += (Math.cos(Math.atan2((x1 - x2, (y1 - y2)))[/code]
[QUOTE=Xystus234;44263357]Is there something wrong with my trig? The units seem to move all over the place: [code]if (distanceToNextCell() > 0) xspeed += (Math.cos(Math.atan2((x1 - x2, (y1 - y2)))[/code][/QUOTE] That's the same as [code]xspeed += (x1 - x2) / sqrt((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1 - y2))[/code]. With usual notation you'd use [I](x2 - x1)[/I] and [I](y2 - y1)[/I] though, at least if the accelerated one is [I]1[/I] and the target is [I]2[/I]. Without drag, you can't target anything by accelerating strictly towards it, it will just put you in (some kind of) orbit.
[QUOTE=thrawn2787;44255775]Did you call glPrimitiveRestartIndex with 65535 AND replace your -1s in the array with 65535? Only thing I can think of[/QUOTE] Turns out I was just fucking up the way I generate my indices. Fixed my algorithm and now everything works.
Hey there, new coder here. In C++, I have 3 objects on a map with 3 different coordinates(of coordinate xpos1, xpos2, xpos3, ypos1, ypos2, ypos3), I need to calculate the distance between these 3 as the first or the second object will move to the frozen object 3 (The closest one will move to the object 3) so I need to determine which one is closer to object 3. The xpos and ypos of objects are inputs from the user. Would appreciate the much needed help
[QUOTE=Zarfa;44270560]Hey there, new coder here. In C++, I have 3 objects on a map with 3 different coordinates(of coordinate xpos1, xpos2, xpos3, ypos1, ypos2, ypos3), I need to calculate the distance between these 3 as the first or the second object will move to the frozen object 3 (The closest one will move to the object 3) so I need to determine which one is closer to object 3. The xpos and ypos of objects are inputs from the user. Would appreciate the much needed help[/QUOTE] The distance between two points is essentially the same as the Pythagorean theorem. distance = sqrt((x1 - x2)^2 + (y1 - y2)^2)
[QUOTE=BackwardSpy;44271346]The distance between two points is essentially the same as the Pythagorean theorem. distance = sqrt((x1 - x2)^2 + (y1 - y2)^2)[/QUOTE] That was easier than I thought, thanks!
[QUOTE=BackwardSpy;44271346]The distance between two points is essentially the same as the Pythagorean theorem. distance = sqrt((x1 - x2)^2 + (y1 - y2)^2)[/QUOTE] you can even save a little processing power by removing the sqrt() part. the distance won't be in units, but it will still accurately tell you which object is closer :)
My units have 6 possible positions of rotation defined as: [code] public enum MobileRotation { TOP_LEFT, TOP_RIGHT, RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT } [/code] They have a current rotation, say BOTTOM_LEFT, and a point they want to rotate to, say TOP_LEFT. What's the best way to find if this unit should rotate clockwise or counter clockwise to reach its goal?
[QUOTE=Asgard;44275053]My units have 6 possible positions of rotation defined as: [code] public enum MobileRotation { TOP_LEFT, TOP_RIGHT, RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT } [/code] They have a current rotation, say BOTTOM_LEFT, and a point they want to rotate to, say TOP_LEFT. What's the best way to find if this unit should rotate clockwise or counter clockwise to reach its goal?[/QUOTE] If you look at the underlying type I think you can use the usual "test both directions" algorithm. [editline]18th March 2014[/editline] Other option: Subtract the current rotation from the target and use a lookup array or switch/ifs in the range [-5;5].
What's the best to study for algorithms? I have an exam tomorrow nad I'm pretty freaking out Here's an question "Assume you have an array of integers (positive, negatives, 0). Design an algorithm to find all pairs of entries in the array such that A[i] + A[j] = C, where C is a given integer constant in linear time i.e. O(N), where N is the number of entries in the array." How would I go with this? I remember someone saying something about a hash, but I wasn't so sure.
Sorry, you need to Log In to post a reply to this thread.