ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
/home/bobby/test/src/test.cpp:258: warning: cannot pass objects of non-POD type `class VectorND<int, int[4]>' 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:
Code:
template<class BaseType, class X>
class VectorND : public PointND<BaseType, X>
{
public:
explicit VectorND();
explicit VectorND(BaseType sx ...);
}
This looked nice in that when I initialized a vector, I had a comma separated list.
Code:
int main()
{
VectorND<int, int[3]> v(1, 2, 3); //make a 3d vector
return 0;
}
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.
Code:
InitObj operator=(const int val)
{
InitObj ret(begin(), val); //begin() refers to beginning of the array that represents the vector
return ret;
}
Finally, overload the initializer object's comma operator to take in a value.
Code:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.