1

I am wondering is it okay to return a vector in main()? For example,

aSpecialVectorType main()
{
    aSpecialVectorType xxx(vector::zero);
    // do something here;
    return xxx;
}

Do I need to forward declare "class aSpecialVectorType;" before main()?

And btw, is it legal to use another name other than "main" in c++?

Thanks

Edit1:

If not, what is the best way that it can output a vector?

My friend ask me to give him a blackbox that can serve as "vector in and vector out", he will use his matlab code to call my code. That's why I am asking. I know how to vector in, but not sure if there is an easy way to output a vector.

Thanks

Edit2:

I am surprised why C++ has such an standard, any explanation? :)

Daniel
  • 2,576
  • 7
  • 37
  • 51
  • possible duplicate of [What is the proper declaration of main?](http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main) – Flexo May 30 '12 at 17:41
  • int main is the entry point for standard c++. – nurettin May 30 '12 at 17:41
  • 4
    What is going to receive the special vector type from `main()`? What is it going to do with that value? As the answers state, you can't do that legitimately. – Jonathan Leffler May 30 '12 at 17:43
  • What sort of 'black box'? A program? Programs communicate via (integer) exit status, or by an IPC mechanism, or collect information form command line arguments, or by writing to files. There are a few other options, but the IPC term probably covers them. So, you need to describe how your blackbox is invoked. If it is a DLL, then you won't be providing `main()`; you'll provide a function with a different name that can be invoked once the DLL is loaded. If you're on Unix/Linux etc, read SO (shared object) for DLL. – Jonathan Leffler May 30 '12 at 17:49
  • 1
    Once you've returned from main, you've left C++ land, and you're in operating system land. The return value of main is an int, because generally, that's all that's allowed by the operating system. Consequently, the return value of main is not a very effective form of communication between programs. A much more effective means of communication is standard output (cout, printf, etc...) *while* the program is running. – Benjamin Lindley May 30 '12 at 17:53
  • 2
    @BenjaminLindley: I can hear thousands of linux commands crying in their beer after that comment. – John Dibling May 30 '12 at 17:54
  • @JohnDibling: I'm not entirely sure what you mean by that. If you were referring to the fact that linux commands do in fact use the program's return value as a form of communication, it's still not a very effective form of communication, since it is so limited in the amount of information that it can actually convey. – Benjamin Lindley May 30 '12 at 17:59
  • @BenjaminLindley: I was really just being facetious. I don't disagree with you. – John Dibling May 30 '12 at 18:00
  • :( I hope C++ can define some standard to have the ability as to use such as `XYZ=myProgram(input).output()`, so I can use it easily in an external 3rd party program. Now I have to output to file and let matlab read this file. ... Hmm, I am not asking much, I am just asking a very little such as output a vector or a string, so the 3rd party program can talk to the c++ program easily. – Daniel May 30 '12 at 18:01
  • Programs are not functions. You will need to do some rethinking and/or learning about how programs communicate, as opposed to how functions within a program communicate. – Jonathan Leffler May 30 '12 at 18:05
  • @Daniel program interoperability is a big topic, very much dependent on the capabilities of the target operating system. – Sergey Kalinichenko May 30 '12 at 18:06
  • I understand it would be complicated, but I still feel just have an `int` as return type is a little bit too restricted. :( – Daniel May 30 '12 at 18:08
  • "*I know how to vector in*". Please explain how you "vector in". That may give us a clue about how you should "vector out". – Robᵩ May 30 '12 at 18:41
  • @Daniel - what would this buy you that a shared library (or whatever your platform provides) doesn't give you? – Flexo May 31 '12 at 21:51

6 Answers6

9

In C++, main needs to return an int:

C++ standard, 3.6.1.2:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

.

If not, what is the best way that it can output a vector?

To output a vector, you need to copy it to a file or an output stream:

ostream_iterator<int> out_it(cout, ", ");
copy(myvector.begin(), myvector.end(), out_it);

The code fragment above writes the content of vector<int> to the standard output.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

No. main must be declared in one of these two ways:

int main()

or

int main(int argc, char*[] argv)

Anything outside of this is not standard.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 1
    Not true. Every C++ toolchain must support those two forms, but are the Standard allows and encourages support for other parameter sets. Only the return type never changes. – Ben Voigt May 30 '12 at 17:43
  • In particular, many/most *nix implementations support passing a `char** envp` as the third argument. But anything beyond the two prototypes mentioned, is implementation-specific. – cHao May 30 '12 at 17:47
  • AFAIK char** is equivalent of char*[] because of decay convention, isn't it? – m0skit0 May 30 '12 at 17:56
  • 1
    cHao is referring to `int main(int argc, char *argv[],char * envp[])`. VC++ supports this prototype, in addition to most *nix implementations. – Mike Kinghan May 30 '12 at 18:11
2

No.
According to the standard main() must return an int and only that.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
1

No.

main must return int.

Other functions are free to have other names and return anything they want.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

To expand on dashblinkenlight's answer, here is how two programs can communicate. Not by one capturing the return value of the other, but by a process called "piping", directing the standard output of one program to the standard input of another. Here, I'll print out a list of strings in one program, then the other program will expect a list of strings on its standard input, then I'll show you how to use the two together:

// Output program
#include <vector>
#include <string>
#include <iostream>

int main()
{
    using namespace std;

    vector<string> v;
    v.push_back("one");
    v.push_back("two");
    v.push_back("three");

    for (int i=0; i<v.size(); ++i)
        cout << v[i] << '\n';
}

// input program
#include <iostream>
#include <vector>
#include <string>

int main()
{
    using namespace std;

    vector<string> v;
    for (string tmp; cin >> tmp; )
        v.push_back(tmp);

    // print the strings in reverse order
    for (int i=v.size()-1; i>=0; --i)
        cout << v[i] << '\n';
}

If you run the first program by itself, it will just print the 3 strings out. If you run the second program by itself, it will prompt the user for strings until he uses the termination command. But on all of the most widely used operating systems, you can chain the two together. Then the output of the first will become the input of the second. On Unix-like systems, you do it like this:

./output_program | ./input_program

On Windows, I think it's the same, or very similar. Not sure though.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

The answer to your first question is no. The answer to your second question is yes, but you need to specify the name of your entry point to your executable (via linker settings ... may not be available on all linker tools).

Below statement is wrong See Ben's comment below. Useful info that.

Be aware that though the name of the entry-point can change, it MUST conform to the standard parameter and return types.

Dennis
  • 3,683
  • 1
  • 21
  • 43
  • 1
    The calling convention for a user-defined entry point isn't necessarily the convention used for `main`. The default entry point is library code that constructs global objects, finds the parameters for `main`, and then calls the `main()` function. User-defined entry point is NOT `main()` with a different name, it replaces the library startup code. – Ben Voigt May 30 '12 at 17:45
  • The user-defined entry point typically has zero args, as the runtime isn't doing stuff for you anymore (like parsing args, setting up globals, etc). And the OS typically just calls directly into the entry point, assuming that your language (whatever it might be) put it there to set up the environment for you. – cHao May 30 '12 at 17:58