Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the C++ compiler error "looks like a function definition, but there is no parameter list;" mean?

Tags:

c++

#include <iostream>
#include <fstream>

using namespace std;

int main
{
    int num1, num2;
    ifstream infile;
    ostream outfile;

    infile.open("input.dat");
    outfile.open("output.dat");

    infile >> num 1 >> num 2;

    outfile << "Sum = " << num1 + num2 << endl;

    infile.close()
    outfile.close()
    return 0;
}

This is what I did and when I compile it, I got this error that said

error C2470: 'main' : looks like a function definition, but there is no
parameter list; skipping apparent body

Please don't hate me :( I am new at this computer science....

like image 588
SkyBoxer Avatar asked Apr 08 '10 22:04

SkyBoxer


3 Answers

I don't hate you.

Functions have parameters, such as:

void foo(/* parameters here */);

If your function takes none you don't omit the list, but leave it empty or put void:

int main()
// or:
int main(void)

Which you use is up to you. I prefer explicitly stating void.


Note, there are other variants you'll encounter. This is the second most common variant:

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

It gives you a count of the arguments and what they were. Such as:

myExe andAOne andATwo andIDontHateYou

You would be able to access those arguments. There can be more, but that should cover it for now. Don't worry about that stuff till later. <3


Concerning your code:

int main(void) // no parameters
{
    int num1, num2;
    ifstream infile;
    ostream outfile;

    infile.open("input.dat");
    outfile.open("output.dat");

    infile >> num1 >> num2; // no spaces in you variable names

    outfile << "Sum = " << num1 + num2 << endl;

    infile.close(); // missing semicolon
    outfile.close(); // missing semicolon

    return 0; // fun fact: optional. In C++, return 0 in main is implicit
}

That should get you started.


The rest of this may not make sense, and that's okay. I'm merely including it for completeness. If it doesn't make sense, ignore it for now:

int main(void)
{
    ifstream infile("input.dat"); // use the constructor to open it
    ostream outfile("output.dat");

    int num1, num2; // don't declare variables until you need them
    infile >> num1 >> num2;

    outfile << "Sum = " << num1 + num2 << endl;

    // closing is done automatically in the destructor of the fstream
}
like image 99
GManNickG Avatar answered Nov 09 '22 13:11

GManNickG


You forgot the parentheses which are required for a function definition. Change that to:

int main()
{
    ...
}
like image 5
R Samuel Klatchko Avatar answered Nov 09 '22 11:11

R Samuel Klatchko


You're missing parentheses after main.

Also, semicolons:

infile.close();
outfile.close();
like image 4
WhirlWind Avatar answered Nov 09 '22 12:11

WhirlWind