0

I want to write a function that creates a "Vector" (template), stores a couple of values in it, then returns that vector. I'm defining the function below "main" and so am using the prototype:

Vector<double> VectorMouseClick;

Is this the correct prototype?

After "main" I'm defining the function and am trying to do it this way but am not sure if it's the correct way to do it:

Vector<double> VectorMouseClick()
{
    Vector<double> vector();
     ...some code manipulating values and storing them in "vector"...
    return vector;
}

If a function is returning a class I specify (aka Vector), how do I write the prototype? Since it's a template class do I do it like above: Vector VectorMouseClick; or is it: Vector VectorMouseClick; (as like the other prototypes (int ReturnValues, void SomeFunct, etc)

When exactly can I use "void" on a prototype? Is this only for functions that don't "return" a value? I've looked through the answers on here but as I'm new to C++ some of them are a little hard to understand. Any help would be greatly appreciated!

MCP
  • 4,436
  • 12
  • 44
  • 53
  • 1
    If you're new to C++, then a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will make your life much easier. There are a lot of quirks that will be very difficult to figure out for yourself. – Mike Seymour Jan 06 '12 at 17:41

2 Answers2

2

Is this the correct prototype?

No, you need parentheses to indicate that it's a function (empty, since the function takes no arguments):

Vector<double> VectorMouseClick();

Without the parentheses, you are declaring a variable, not a function.

If a function is returning a class I specify (aka Vector), how do I write the prototype?

In general a function declaration is always return_type function_name(arguments); (perhaps with a few extra bits and pieces). A class is a type, so just give its name as the return type. A class template isn't a class (or a type), so you need to provide the template arguments to make it a class.

When exactly can I use "void" on a prototype? Is this only for functions that don't "return" a value?

That's exactly right. void is used in places where you have to give a type, but there isn't any type to give; in the case of a function return type, it indicates that nothing is returned.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

No, it is not. In general:

T t; 

declares a variable of type T named t.

T t();
T t(void);

is both the same way to declare a function taking no parameter, returning T and being named t.

Using one or the other is merely a style issue, since in C the void was necessary, while in C++ it is not. When you don't return any value, then the return type of the function is void.

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57