Main Declaration error

This is a discussion on Main Declaration error within the C++ Programming forums, part of the General Programming Boards category; Folks, So I was on the FAQ page (yes sometimes newbie's actually read it!!) to learn more about the main ...

  1. #1
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21

    Main Declaration error

    Folks,

    So I was on the FAQ page (yes sometimes newbie's actually read it!!) to learn more about the main declarations...specifically the below thread.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    So for C++ I put down:-

    Code:
     
    int main(int argc, char *argv[])
    Whereupon I got the below understandable warning from the compiler.

    Code:
     
    warning C4508: 'main' : function should return a value; 'void' return type assumed
    So I added a return statement (as below), to the end of my main function.

    Code:
     
    return 1;
    And then I got the same warning and then the below error. Both of these messages seem to be in conflict of each other.

    Code:
     
    warning C4508: 'main' : function should return a value; 'void' return type assumed
    
    error C2562: 'main' : 'void' function returning a value
          see declaration of 'main'
    I'm using VC++6.0 First it complains that main should be returning a value, and then when I return a value to complains again........any ideas why ?

    Cheers
    Starkhorn

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I'm not sure what is wrong, but out of curiosity does it result in the same warning and error if you declare main as such:
    Code:
    int main(void){return 0;}

  3. #3
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    having a program with just that 1 line obviously worked fine. But when I changed my own program to match that, I got the exact same warning and error.

    I've double-checked to ensure that I've not got any mis-placed curley brackets or anything and all seems fine.

    Cheers
    Starkhorn

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    that's very odd...would you mind posting your entire main.cpp, or whatever you have it called?

  5. #5
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    Here you go. Sorry if it's too long and thanks for taking the time to help.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream> 
    
    using namespace std;
    
    #define GameNo_String "<GameNo"
    #define TurnNo_String "<TurnNo"
    #define NationNo_String "<NationNo"
    #define Name_String "<Name"
    #define Artifact_string "<Artifact"
    #define Char_End_string "</Character"
    
    #define NUM_OF_ARTIFACTS 6
    #define NUM_OF_CHARS 21
    
    typedef struct
    {
    	int id, location;
    }ARTIFACTS;
    
    typedef struct
    {
    	string name;
    	int location;
    	ARTIFACTS arti_list[NUM_OF_ARTIFACTS];
    }CHARACTER;
    
    
    int StringToInt(const string &s)
    {
    	int i;
    
      istringstream myStream(s);
      
      if (myStream>>i)
    	return i;
      else
    	  return 0;
    }
    
    int compare_string(string str1, string str2)
    {
    	if (str1 == str2)
    		return 1;
    	else
    		return 0;
    }
    
    int get_info(ifstream &finput,string str, int &i)
    {
    	getline(finput,str,'<');
    	i++;
    	return StringToInt(str);
    }
    					
    int main(int argc, char *argv[])
    {
    	ifstream fin;
    	ofstream fout;
    	string szLine = "";
    	int game_num = 0, turn_num = 0, nation_num = 0, compare_string_choice = 0;
    	CHARACTER char_list[NUM_OF_CHARS];
    
    	fin.open("test.xml");
    
    	if(fin.fail())
    	{
    		cout << "ERROR\n";
    		return;
    	}
    
    	fout.open("out_test.xml");
    
    	if (fout.fail())
    	{
    		cout << "ERROR output";
    		return;
    	}
    
    	while (!fin.eof())
    	{
    		
    		getline(fin,szLine,'>');
    
    		switch(compare_string_choice)
    		{
    			case 0:
    				if (compare_string(szLine,GameNo_String))
    				{
    					game_num=(get_info(fin,szLine,compare_string_choice));
    					cout << "Game number:- " << game_num << endl;
    				}
    				break;
    
    			case 1:
    				if (compare_string(szLine,TurnNo_String))
    				{
    					turn_num=(get_info(fin,szLine,compare_string_choice));
    					cout << "Turn number:- " << turn_num << endl;
    				}
    				break;
    
    			case 2:
    				if (compare_string(szLine,NationNo_String))
    				{
    					nation_num=(get_info(fin,szLine,compare_string_choice));
    					cout << "Nation number:- " << nation_num << endl;
    				}
    				break;
    
    			default:
    				break;
    		}
    	
    	}
    	fout.close();
    	fin.close();
    //	cout << "The number of argc elements are " << argc << endl;
    //	cout << "The location of the program is " << argv[0] << endl << endl;
    	return 1;
    }

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I'm sorry sir, but I am completely clueless. Some of your stuff is a little "old style" but is by no means wrong, nor should it cause such an error. All I can say is something may be wrong with your projects settings...that error sounds more like something you'd get from a C compiler rather than a C++ compiler. I doubt it would be getting confused on how to compile, because it would scream about iostream and the other headers. Hopefully somebody else can help you out.

  7. #7
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    ok thanks. I'll check out the project settings.....it seems VC++6.0 seems to make things 50 times harder than it needs to be but......

    Just curious now, which parts are "old style" ? I hadn't really realised that there was a new or old style.....I guess I'm showing my age there now.

    Cheers
    Starkhorn

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I'll admit I had no idea what was going on until I tried to compile your code. The problem lies on lines with a return statement but that don't return a value. This happens in both of your if statements when checking for a failed file opening. Add a return value on these lines, and it's fixed.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Wow, that was one really cryptic error message! Points to VC++ 6!

    (good job pianorain)
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    ahhh ok, thank you pianorain...it's so obvious when someone else tells you the answer.

    Seriously thank you....I was totally being confused by the conflicting error messages.

    Cheers
    Starkhorn

  11. #11
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by starkhorn
    Here you go. Sorry if it's too long and thanks for taking the time to help.

    Code:
    int main(int argc, char *argv[])
    {
    	ifstream fin;
    	ofstream fout;
    	string szLine = "";
    	int game_num = 0, turn_num = 0, nation_num = 0, compare_string_choice = 0;
    	CHARACTER char_list[NUM_OF_CHARS];
    
    	fin.open("test.xml");
    
    	if(fin.fail())
    	{
    		cout << "ERROR\n";
    		return;	}
    
    	fout.open("out_test.xml");
    
    	if (fout.fail())
    	{
    		cout << "ERROR output";
    		return;	}
    
    	while (!fin.eof())
    	{
    		
    		getline(fin,szLine,'>');
    
    		switch(compare_string_choice)
    		{
    			case 0:
    				if (compare_string(szLine,GameNo_String))
    				{
    					game_num=(get_info(fin,szLine,compare_string_choice));
    					cout << "Game number:- " << game_num << endl;
    				}
    				break;
    
    			case 1:
    				if (compare_string(szLine,TurnNo_String))
    				{
    					turn_num=(get_info(fin,szLine,compare_string_choice));
    					cout << "Turn number:- " << turn_num << endl;
    				}
    				break;
    
    			case 2:
    				if (compare_string(szLine,NationNo_String))
    				{
    					nation_num=(get_info(fin,szLine,compare_string_choice));
    					cout << "Nation number:- " << nation_num << endl;
    				}
    				break;
    
    			default:
    				break;
    		}
    	
    	}
    	fout.close();
    	fin.close();
    //	cout << "The number of argc elements are " << argc << endl;
    //	cout << "The location of the program is " << argv[0] << endl << endl;
    	return 1;
    }
    Each of the return statements should return an int

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    41

    title

    You really should not use main() to call stuff. Use another function for that. And it's best to always have main at the beginning before any other funcitons.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 03:39 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21