v0dKA

Members
  • Content count

    1050
  • Joined

  • Last visited

Community Reputation

568 Good

About v0dKA

  • Rank
    Contributor
  1. C++ Templates Question

    Thanks for all the help! I got this almost solved. It leaves only a little bit to be left desired. I implemented the conversion operator in B<T>, so class B looks like this now: template<class T> class B : public A<T> { public: // Constructor B<T>(T new_data) { this->data = new_data; } // Conversion operator operator A<T> () const { return A<T>(this->data); } }; Then it took some brain racking to figure this next one out. Turns out it's necessary to change the second operator* function in A from this: template<class V> A<T> operator*(V c) To this: template<class V> A<T> operator*(V& c) Otherwise, the compiler would still keep picking the second (generic) operator* function when I multiply A<T> with B<T>, even when I use the conversion operator explicitly. I recall that two member functions can differ only by a reference, which seems kind of silly and useless to me, but it turned out to be part of the problem in my case. But here's the part that leaves a little bit to be left desired. It looks like I'm still required to explicitly cast b1 to type A<T> using the conversion operator. It doesn't seem to happen implicitly. So I still can't really say: A<int> a2 = a1 * b1; I have to say: A<int> a2 = a1 * (A<int>)b1; Not particularly elegant, but I guess as good as it gets? Anyways, thanks again to all. I will be implementing the style/notation suggestions next.
  2. Classical music

    You might also like Beethoven's 3rd symphony. Actually all of them, what the hell. Eventually move on to his piano sonatas, and especially the piano concertos. His piano concertos are best listened to in the order he wrote them, which is not quite the order they're numbered in. Now, I actually have the opposite question. I've been listening to classical music all my life and now I'm interested in rock - not hard rock and roll, just rock (and maybe some "closely related" genres). I love the classics (Bohemian Rhapsody and Hotel California) - what are some others that I MUST listen to? Also, my roommate introduced me to Jack Johnson, which I enjoyed. What are some other singers like him, and maybe some famous songs that go in that "sub-genre"?
  3. C++ Templates Question

    Hello, It's been a while since I've worked in C++, so I got a little stuck. Here's a simplified version of my problem. I have a templatized class called A which has a data member called "data". I'm trying to overload the * operator (multiplication) for it. I want it to be possible to multiply this class with another instance of the same class. So "A<int> times A<int>" should be possible. But I also want it to be possible to multiple the class with a constant (like an int or a double) instead of a class. So "A<int> times float" should also be possible. All works fine so far when I have just this much defined: template<class T> class A { public: // Data member T data; // Constructors A<T>() { this->data = T(); } A<T>(T new_data) { this->data = new_data; } // Multiply an instance of A<T> with another instance of A<T> A<T> operator*(A<T>& rhs) { cout << "Performing A<T> times A<T>" << endl; return A<T>(this->data * rhs.data); } // Multiply an instance of A<T> with some other data type template<class V> A<T> operator*(V c) { cout << "Performing A<T> times V" << endl; return A<T>(this->data * c); } }; Here's some test code: // Initialize some test numbers A<int> a1(5); A<int> a2(7); double b1 = 4.7; // Perform the operations A<int> a3 = a1 * a2; // Causes to print: "Performing A<T> times A<T>" A<int> a4 = a1 * b1; // Causes to print: "Performing A<T> times V" // Print the results cout << a3.data << endl; // Prints 35 cout << a4.data; // Prints 23 (not 23.5 because a2 is parameterized with "int" instead of "double" but oh well) But I want to add another layer of complexity, which is where I encounter some problems. I introduce another class B which is derived from A<T>: template<class T> class B : public A<T> { public: // Constructor B<T>(T new_data) { this->data = new_data; } }; Now suppose I do this: A<int> a1(5); B<int> b1(12); A<int> a2 = a1 * b1; cout << a2.data; The compiler will give an error: Error C2677: binary '*' : no global operator found which takes type 'B<T>' (or there is no acceptable conversion) The error is in the function template<class V> A<T> operator*(V c) Apparently, what happens when I try to perform a1 * b1, the compiler picks the wrong operator* function. I want it to pick the first one ("Multiply an instance of A<T> with another instance of A<T>") because B<T> is derived from A<T>. But the compiler picks the second one ("Multiply an instance of A<T> with some other data type"), I guess because B<T> is not exactly A<T>, and the second operator* function is the general catch-all one. What can I do in this case? I want the compiler to pick the first operator* function when I multiple an A<T> type with a B<T> type. (Second bonus question: How might I make it possible to multiple A<int> with A<double>? At the moment, the 'T' parameter has to match when I multiply one instance of A<T> with another instance of A<T>.) EDIT: Here's the complete code, which doesn't compile due to the one error described above: #include <iostream> using namespace std; template<class T> class A { public: // Data member T data; // Constructors A<T>() { this->data = T(); } A<T>(T new_data) { this->data = new_data; } // Multiply an instance of A<T> with another instance of A<T> A<T> operator*(A<T>& rhs) { cout << "Performing A<T> times A<T>" << endl; return A<T>(this->data * rhs.data); } // Multiply an instance of A<T> with some other data type template<class V> A<T> operator*(V c) { cout << "Performing A<T> times V" << endl; return A<T>(this->data * c); } }; template<class T> class B : public A<T> { public: // Constructor B<T>(T new_data) { this->data = new_data; } }; int main() { A<int> a1(5); B<int> b1(12); A<int> a2 = a1 * b1; cout << a2.data; cin.get(); return 0; }
  4. Snippets in VB.NET - quick question

    Hmmm, ok. I guess it doesn't really get in the way that the green highlighted regions remain highlighted. I'm just very used to working with Visual C#, where I could leave the snippet by hitting return, and once the highlights went away, it gave me a sense of "closure".
  5. Snippets in VB.NET - quick question

    But wait... What about this. When you're inside a class definition, you know how you could type "property<TAB>", and VB.NET fills in the following: Private newPropertyValue As String Public Property NewProperty() As String Get Return newPropertyValue End Get Set(ByVal value As String) newPropertyValue = value End Set End Property And a bunch of those fields are highlighted. You can hit <TAB> and <SHIFT+TAB> to switch between them, and they all change intelligently (i.e., if you change one of the "String" fields to "Double", the other one does too). Well, what do you hit when you're done? It doesn't seem to be <ENTER> nor <ESC>.
  6. Snippets in VB.NET - quick question

    I see, maybe it works a little differently in VB.NET. In C#, I used to type "if<TAB><TAB>condition<ENTER>". I guess in VB I could do "if condition<ENTER>", although I was expecting I could do "if<TAB>condition<ENTER>".
  7. When I type "if" and hit TAB, VB.NET saves me some time and replaces it with this: If True Then End If Then it highlights the "True" in blue and prompts for a replacement. In Visual C#.NET, I'm used to typing out the condition and hitting the return key so that I'm brought into the body of the statement. However, hitting return in VB.NET does not do the same thing at all. What's the shortcut key I could use to tell VB.NET that I'm done typing my condition and ready to start typing the body?
  8. Kindle any good for studying?

    The kindle is nothing more than a special kind of "paper" that has the unique quality that it can change what it displays by way of a clunky and irritatingly slow interface. It does not confer its wielder with any sort of enhanced learning abilities or mental faculty. You might consider a Kindle if: * You find yourself having to carry a large number of heavy books on a regular basis. * You subscribe to a bunch of newspapers. * You find that being able to look up a word in the dictionary very quickly a necessity. Other than that, it's not all that special.
  9. Check if this pattern of behavior carries over to other parts of your life. In that case, you might have a discipline problem, and you would benefit even greater if you were to address the general problem.
  10. What's the beef with Panda3D?

    It's a great engine, but highly lacking in documentation. I hope they get to work on that. I recall having to browse through its source many times just to see what the parameters for a specific function call are meant to do -- and I've only used it for a month.
  11. VS2010 beta impressions

    I'm wondering if they're ever planning on adding code editor features like automatic comment wrapping, or generating Javadoc comment skeletons, and things like that. Especially in C++. I'm aware there's a pretty nifty feature like that in C#, where if you start a comment with three slashes, these features come into play, but I've been really wanting this feature in other languages.
  12. Zahlman, Thank you very, very much! That was very helpful.
  13. Hi, I'm trying to implement a queue class in C++ for my data structures course. The program seems to run fine, but on exit, Visual C++ tells me: Windows has triggered a breakpoint in exp.exe. This may be due to a corruption of the heap, which indicates a bug in exp.exe or any of the DLLs it has loaded. I can't figure out why this is happening. My guess would be that there's a problem with the part of the code that reallocates more memory for the data structure when the user adds more elements than the queue could originally hold (this is in the push function below). Here's my class: Queue.h #pragma once #include <stdexcept> #include <iostream> template<class T> class Queue { public: Queue(void); ~Queue(void); /** * IsEmpty - Returns true if the queue has no elements. */ bool IsEmpty() const; /** * size - Returns the number of elements in the queue. */ int size() const; /** * front - Returns the oldest element in the queue, without * removing the element from the queue. */ T& front() const; /** * back - Returns the most recently added element in the queue, * without removing it from the queue. */ T& back() const; /** * pop - Dequeues the oldest element in the queue. */ void pop(); /** * push - Adds a new element to the back of the queue. * @theElement: the element to be added. */ void push(const T& theElement); // TEMPORARILY PUBLIC, FOR DEBUGGING /** * capacity - The number of elements the queue can currently * hold based on the amount of memory allocated to it. * Distinct from the number of elements actually in * the queue. */ unsigned int capacity; private: /** * The number of elements actually in the queue (as opposed * to capacity). */ unsigned int q_size; /** * The elements of the queue itself. */ T *theQueue; /* * Points to the current start and end of the queue. */ T *q_front, *q_rear; }; template<class T> Queue<T>::Queue(void) { capacity = 10; theQueue = new T[capacity]; q_size = 0; q_front = theQueue; q_rear = theQueue + (capacity-1); } template<class T> Queue<T>::~Queue(void) { delete [] theQueue; } template<class T> bool Queue<T>::IsEmpty() const { return (q_size == 0); } template<class T> int Queue<T>::size() const { return q_size; } template<class T> T& Queue<T>::front() const { if (q_size <= 0) throw std::out_of_range("Attempted to retrieve front item of an empty queue."); return *q_front; } template<class T> T& Queue<T>::back() const { if (q_size <= 0) throw std::out_of_range("Attempted to retrieve rear item of an empty queue."); return *q_rear; } template<class T> void Queue<T>::pop() { if (q_size <= 0) { std::cout << "Warning: pop() called on empty queue. Ignoring request." << std::endl; return; } T *t_front = theQueue + q_front; delete t_front; --q_size; ++q_front; if (q_front - theQueue >= (int)capacity) q_front = theQueue; } template<class T> void Queue<T>::push(const T& theElement) { if (q_size == capacity) { // Allocate more capacity. capacity *= 2; T *newQueue = new T[capacity]; memcpy(newQueue, theQueue, q_size*sizeof(T)); unsigned int frontOffset = q_front - theQueue; delete [] theQueue; theQueue = newQueue; q_front = theQueue + frontOffset; } ++q_size; ++q_rear; if (q_rear - theQueue >= (int)capacity) q_rear = theQueue; *q_rear = theElement; } And here's a little test program, which works as expected (though it doesn't test all functionality of the class): main.cpp #include <iostream> #include "Queue.h" using namespace std; void printinfo(Queue<int>& q) { cout << "Front=" << q.front() << ", Back=" << q.back() << ", Size=" << q.size() << ", Capacity=" << q.capacity << endl; } int main() { Queue<int> queue; queue.push(1); printinfo(queue); queue.push(2); printinfo(queue); queue.push(3); printinfo(queue); queue.push(4); printinfo(queue); queue.push(5); printinfo(queue); queue.push(6); printinfo(queue); queue.push(7); printinfo(queue); queue.push(8); printinfo(queue); queue.push(9); printinfo(queue); queue.push(10); printinfo(queue); queue.push(11); printinfo(queue); queue.push(12); printinfo(queue); cin.get(); return 0; } Thank you, I would really appreciate your help! [Edited by - v0dKA on April 23, 2009 1:11:26 AM]
  14. Ah, I have heard of WPF before. I'd love to try it out, but my C# express is crapping out on me as soon as I try to create a WPF project ("Microsoft.visualstudio.xaml has failed to load properly". Other people have the same issue, but it looks like the workaround is a complete reinstall of pretty much all Microsoft developer tools, which I'm not willing to do with a looming deadline.) I'm curious about a couple things with regard to WPF. 1: I love winforms and .NET because it's so easy to slap together a decent looking GUI with so little effort. What about WPF? I know the results look far, far better -- but does it also take significantly more effort? What are your recommendations on winforms vs WPF, especially for the future? 2: To be honest, I never formally "learned" how to use .NET and the Visual Studio designer for forms. Given experience in other languages and GUI frameworks, the learning curve was essentially non-existent, and whatever I didn't know, I could verify with a quick web search. Will I be able to get away with this attitude if I decide to go with WPF? Or does WPF require more effort to use properly? Sorry to derail my own thread, but I am curious! Thanks.
  15. Hi, I'm designing a GUI application in C#.NET. Look at the image below. It's a screen shot of Acronis TrueHome. I find its interface very appealing and would like to create something similar in my own application: I particularly like the sidebar. It's the same thing you find in Vista's control panel, among other places. Also have a look at this screen shot of Trillian: It's similar, but also very appealing. Can these interfaces be made in .NET? What controls are they using in the side bars? Thanks.