LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2015, 07:50 PM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Rep: Reputation: Disabled
g++ error message


I have this code:

Code:
void main()  {

     printf("\n  Please enter m, n, x and y: "); 
     scanf("%d %d %lf %lf", &M,&N,&X,&Y);     
     printf("  m = %d, n = %d, x = %f, y = %f\n\n", M, N, X, Y);
     CLPMN(M,N,X,Y,CPM,CPD);
     printf("   m   n    Re[Pmn(z)]     Im[Pmn(z)]    Re[Pmn'(z)]    Im[Pmn'(z)]\n");
     printf(" -------------------------------------------------------------------\n");  
     for (J=0; J<=M; J++) {
       printf("   %d   %d  %12.6f   %12.6f   %12.6f   %12.6f\n", 
	          J, N, CPM[J][N][0], CPM[J][N][1], CPD[J][N][0], CPD[J][N][1]);
     }
     printf("\n");
   }
The compilation with g++ gives me a single error:

Quote:
mclpmn.cpp:250:14: error: ‘::main’ must return ‘int’
void main() {
^
At Preview time I noticed that the caret ^ moved to the left border. In actual Terminal in Ubuntu it points to between two parentheses:

Quote:
main()
What kind of integer does the program expect and why? Thanks, - A.
 
Old 01-03-2015, 08:50 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
You should have started your own thread for this. In any case, the integer main returns is the exit status of the program; generally 0 means success and some other number indicates an error. I suggest picking up a basic C++ programming text book.
 
Old 01-05-2015, 10:56 AM   #3
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
I believe the C++ standard says that the return value from main is implementation defined - primarily because Windows doesn't use the return value. UNIX type OS do however, so any program written for UNIX is going to expect a return value from main. The stdlib header provides some macro definitions i.e. EXIT_FAILURE and EXIT_SUCCESS that can be used. It is always correct to return 0 for success.

Code:
#include <cstdlib>
int main (int argc, char** argv) {
    if (argc < 2) {
        ::exit (EXIT_FAILURE);
    }

    return 0;
}
 
Old 01-05-2015, 11:03 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
In C++, a 'return 0' is defaulted for function main. (Of course it's a bad idea to rely on this.)
 
Old 01-05-2015, 11:42 AM   #5
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
In C++, a 'return 0' is defaulted for function main. (Of course it's a bad idea to rely on this.)
Right. I had forgotten that - he'd still need "int main ()" however.
 
Old 01-15-2015, 11:48 AM   #6
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
The main thread must return a value for the system to prcess error messages. 0 is typical, but other values can be deduced. Not too many kernels rely on this.
 
Old 01-15-2015, 11:51 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The kernel is not interested in the return value, the calling process is (mind you, in some cases the kernel is the caller, eg: /sbin/hotplug)
 
Old 01-15-2015, 01:03 PM   #8
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 55
The problem is that the method header says that the main method returns void. It should say that it returns int.
 
Old 01-19-2015, 01:06 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The answer has been given, but with a ton of side-along comments.

First and foremost, all the compiler is saying is the exact complaint that it wants the TYPE of variable declared for the return of the main() function to be an integer, which is the phrase "int", therefore:
Code:
int main()
That's a first part which you need to correct that complaint.

Then because you've declared a variable type, versus VOID, as the return for the main function, you then need to have a return statement providing an integer value as part of the return. That can be any of:
Code:
return 0;
OR:
Code:
return(0);
OR a non-zero value, which still classifies as an integer. Minus infinity, 0, through plus infinity.

And the secondary stuff are:
  1. You can't use minus infinity or plus infinity, you have to stay within the allowable max range of an integer in your programming environment, also the computer you're using, which is to say that the machine may be 32-bits and therefore whatever the largest negative and positive numbers which can be represented by 32-bits. Those are like plus/minus 2.15 billion, so a lot of numbers. It's safe to just use -1, -2, 0, 1, 2, a few selections, no need to use -9123456 you know?
  2. Some of the discussion there is that if you did not include a "return 0;" statement, the compiler might imply one anyways because it's smarter than you and knows likely what you meant, or that you didn't care enough to give a return or a value, so it adds one for you. Nice, but you don't need to know that level of sophistication and really you should follow the rules as opposed to conveniently skirting them, and learning bad practices at the get go
  3. @btmiller, are you saying that AlexBB is not the Original Poster? What's up with saying they should've started their own thread, I see post #1 where there's a program, the errors, and a question ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Server Hung with error message: ERROR: Message hist queue is filling up iprince Linux - Enterprise 7 02-10-2014 09:40 AM
Got this error message from process, what does this message mean? Vita Linux - Newbie 3 07-20-2013 08:53 AM
Error 502 : Display Fatal Error Message, Error pushing image, dbpaCT failed! HaloCheng Linux - Newbie 1 09-12-2012 12:02 PM
Strange Repeating Error message in /var/log/message lucktsm Linux - Security 2 10-27-2006 08:29 AM
Error message received from system Error while reading filter description for true Steel_J Linux - Software 2 03-04-2006 06:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration