C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2004, 04:21 AM   #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
starkhorn is offline   Reply With Quote
Old 08-02-2004, 04:43 AM   #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;}
skorman00 is offline   Reply With Quote
Old 08-02-2004, 05:00 AM   #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
starkhorn is offline   Reply With Quote
Old 08-02-2004, 05:03 AM   #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?
skorman00 is offline   Reply With Quote
Old 08-02-2004, 05:07 AM   #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;
}
starkhorn is offline   Reply With Quote
Old 08-02-2004, 05:18 AM   #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.
skorman00 is offline   Reply With Quote
Old 08-02-2004, 05:26 AM   #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
starkhorn is offline   Reply With Quote
Old 08-02-2004, 07:55 AM   #8
Anti-Poster
 
Join Date: Feb 2002
Posts: 1,212
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.
__________________
Rule #1: Every rule has exceptions

Traveller's Dilemma Contest Site - Results posted!
pianorain is offline   Reply With Quote
Old 08-02-2004, 12:54 PM   #9
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Göteborg, Sweden
Posts: 2,038
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
Sang-drax is offline   Reply With Quote
Old 08-03-2004, 02:41 AM   #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
starkhorn is offline   Reply With Quote
Old 06-22-2005, 07:59 AM   #11
C/C++Newbie
 
Antigloss's Avatar
 
Join Date: May 2005
Posts: 210
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
Antigloss is offline   Reply With Quote
Old 06-22-2005, 09:04 AM   #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
smog890 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:16 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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