
The Big Programming Thread - Page 294
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Orome
Switzerland11984 Posts
![]() | ||
Zeke50100
United States2220 Posts
On May 03 2013 01:17 Orome wrote: Hey guys, was hoping someone could help me out again real quick. I'm trying to make a vector-based heap of std:: pairs. My solution seems to be identical to the one they provided us with, but somehow it doesn't work. When I try to do something like this:
the program crashes. item is defined as:
heap's a class variable, defined as:
I appreciate any help. ![]() EDIT: Well, you found the error, so I'll spoiler the old answer. + Show Spoiler + I suspect an out-of-bounds error: a std::vector does not have any "space" upon construction - its size dynamically grows as you need to add new stuff to it (or shrinks if you're taking things out of it). heap[1] = item(k, e) heap[1] is out-of-bounds because there isn't a [1] in heap. std::pair<int, std::string> item(5, "apple pie"); The "push_back()" function adds an object to the heap. After having added the item, you can now access it as "heap[indexNumber]" - in this case, "heap[0]" will let you access (5, "apple pie"). Alternatively: std::vector<item> heap(10) This will leave you with 10 default-constructed items (in this case, pairs of ints and strings). std::cout << heap[0].second << '\n'; //will print "apple pie" The at() function can help alleviate this problem. A useful resource on vectors is here, in case you have other issues. | ||
Sverigevader
Sweden388 Posts
I've written a Fraction class and I've overloaded for example the + operator to work when adding integers with fractions Code: + Show Spoiler + friend Fraction operator+ (int i, Fraction& f1); friend Fraction operator+ (Fraction& f1, int i); I'm sure you can figure out my question already: Is there a way to do this without having to do two separate functions for when the terms switch places? | ||
netherh
United Kingdom333 Posts
On May 05 2013 00:37 Sverigevader wrote: Quick question about operator overloading in C++. I've written a Fraction class and I've overloaded for example the + operator to work when adding integers with fractions Code: + Show Spoiler + friend Fraction operator+ (int i, Fraction& f1); friend Fraction operator+ (Fraction& f1, int i); I'm sure you can figure out my question already: Is there a way to do this without having to do two separate functions for when the terms switch places? Nope. | ||
CecilSunkure
United States2829 Posts
Negatory. | ||
ddengster
Singapore129 Posts
On May 05 2013 00:37 Sverigevader wrote: Quick question about operator overloading in C++. I've written a Fraction class and I've overloaded for example the + operator to work when adding integers with fractions Code: + Show Spoiler + friend Fraction operator+ (int i, Fraction& f1); friend Fraction operator+ (Fraction& f1, int i); I'm sure you can figure out my question already: Is there a way to do this without having to do two separate functions for when the terms switch places? think you can do it with template specializations, but it's not worth the amount of code. Usually, you put the operator+ function within Fraction itself so it's treated as a method of the class, so there's no need to have a second global friend function for your 2nd line of code. example: class Fraction { public: Fraction operator+(int i); friend Fraction operator+ (int i, Fraction& f1); }; | ||
Kambing
United States1176 Posts
On May 05 2013 02:50 ddengster wrote: think you can do it with template specializations, but it's not worth the amount of code. Usually, you put the operator+ function within Fraction itself so it's treated as a method of the class, so there's no need to have a second global friend function for your 2nd line of code. example: class Fraction { public: Fraction operator+(int i); friend Fraction operator+ (int i, Fraction& f1); }; That doesn't solve the problem of requiring two separate functions. In general, operator+ may not be commutative, so there is no automatic support for generating both forms of the operator. The simplest way is to implement one operator directly and (either as a free-floating function as you have or as a member function as ddengster described) and then implement the other operator in terms of the first. | ||
WindWolf
Sweden11767 Posts
I know how to program in C++ and I'm a rusty on Java. | ||
obesechicken13
United States10467 Posts
Crosspost on stack: http://stackoverflow.com/questions/16381132/how-do-i-get-text-in-my-html-page-to-display-features-using-the-css-stylesheet Figured it out. Misconfigured stylesheet link. | ||
Shield
Bulgaria4824 Posts
http://en.wikipedia.org/wiki/Extreme_programming | ||
Deleted User 101379
4849 Posts
On May 06 2013 03:46 darkness wrote: What do you think of Extreme Programming methodology? http://en.wikipedia.org/wiki/Extreme_programming Depends on what part of it you mean. Most programmers already do a lot of what extreme programming entails but going the full way has about as many problems as it has advantages, the same as most agile methodologies. The best is to do what works, to get ideas from other methodologies but to be critical and only apply the stuff that you really need and to be wary of overengineering the process instead of putting the effort into actual solutions. There is no silver bullet. | ||
Craton
United States17234 Posts
Mine does / has done a little bit of everything (waterfall, modified waterfall, scrum, other agile methods) and we've found pros and cons for everything (especially when working with the government). | ||
Shield
Bulgaria4824 Posts
| ||
Craton
United States17234 Posts
If you want to be a PM/TM then that's another story, but as someone who develops and hates managerial stuff, I loathed that type of coursework. | ||
Arnstein
Norway3381 Posts
| ||
Blisse
Canada3710 Posts
On May 06 2013 20:17 Arnstein wrote: I have a class Creature with the protected int HP in C++. I have a subclass Enemy. Shouldn't enemy then get the int HP, as a sort of private int? I get the compiler error "class Enemy has no member named HP" A bit harder to diagnose without knowledge of how they're implemented. Is everything defined properly? | ||
Arnstein
Norway3381 Posts
On May 06 2013 20:38 Blisse wrote: A bit harder to diagnose without knowledge of how they're implemented. Is everything defined properly? No, I fixed with the good ol' #ifndef and #define ![]() | ||
Shield
Bulgaria4824 Posts
[[person child] setHeight:argument]; Is there something similar in Java/C++? Perhaps person.child.height = argument; but is inheritance involved here? I'm not sure if I understand code above. Perhaps you use a method from another class without calling an instance? | ||
enigmaticcam
United States280 Posts
On May 07 2013 07:34 darkness wrote: I am not sure how to call it exactly, but Objective-C can do something like this: [[person child] setHeight:argument]; Is there something similar in Java/C++? Perhaps person.child.height = argument; but is inheritance involved here? I'm not sure if I understand code above. Perhaps you use a method from another class without calling an instance? As far as Objective-C is concerned, that block of code is calling the "setHeight" function with the "argument" as a parameter on an object returned from the "child" function of the "person" object. I don't know the Java or C++ equivalent, but essentially it's a nested function call. | ||
Craton
United States17234 Posts
Assume a "lookup" table with a pair of fields "match" and "replace" that have values like: À, A; È, E (such that you have pairs of "accented" characters in the match and "normal" characters in the replace). Assume you have another table with n number of records, any of which have one or more characters matching this pattern. The end translation should go something like ÀNYWHÈRÈ -> ANYWHERE. For a single value, I can use a CONNECT BY LEVEL to split each character into a separate field, join to the lookup table, and then use a MODEL construct to perform the replacement and concatenation of each character (ordering maintained), taking only the final "built" value. However, I can't make it work when I have multiple values (i.e. from a table), since I can't just connect it by level. I feel there might also be a way to skip the CONNECT BY step altogether, but I can't make it happen. Thoughts? | ||
| ||