ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
/home/bobby/test/src/test.cpp:258: warning: cannot pass objects of non-POD type `class VectorND<int, int>' through `...'; call will abort at runtime
That's the message in its entirety. I looked up what POD means. It means Plain Old Data. Apparently classes are not guaranteed to be passed evenly onto the stack or something. So, va_arg is not guaranteed to work hence the segmentation fault error. Is there any way around this?
Yes, references are instant death. Seems like I'm forced to use pointers which is inconsistent with the rest of the class design. For example, on initialization, I passed data to my vectors using this:
template<class BaseType, class X>
class VectorND : public PointND<BaseType, X>
explicit VectorND(BaseType sx ...);
This looked nice in that when I initialized a vector, I had a comma separated list.
VectorND<int, int> v(1, 2, 3); //make a 3d vector
I like the way that looks. It would be nice to do a simular thing with the cross product.
There are several problems with this design. Corrupting the stack is far too easy. It would be easy to neglect a single number and create a segmentation fault. Hence, it would be nice to get away from variatable functions all together. I decided to change my design recently to use an array as an initializer. This is not a pretty solution, but it forces you to pass the right number of parameters. If you guys have any idea of how I can make this pretty and safe, please let me know. Thanks for all the help so far.
Is there an upper bound to the number of arguments? If yes, you could just use default values (I don't know whether that is possible with non-POD types); if the caller passes too many arguments, your algorithm can simply ignore them.
However, in my opinion, this solution is much uglier than passing an array (respectively a std::vector).
It has been a while since I last posted this question, but I did manage to solve the problem entirely. If you want the answer, read on. I'll be using sudo code since I don't want to spend a lot of time trying to simplify complex code. Basically, have the constructor set the vector to zero. Second, overload the assignment operator to take in a value (such as an integer to the first component) and then have the operator return an intializer object.
InitObj operator=(const int val)
InitObj ret(begin(), val); //begin() refers to beginning of the array that represents the vector
Finally, overload the initializer object's comma operator to take in a value.
InitObj operator,(const int val)
*component = val; //assign a value to the current component
++component; //get ready for the next component
After doing this it is now possible to write.
Vector<int, 3> v;
//calls the vectors operator= and the uses the initializers operator, to initialize the rest
v = 1, 2, 3;
This is neet and compact. Initializing the vectors may be a little slower since the constructor can't be used directly, but this is a nice solution to a hard problem.