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 12-27-2006, 12:40 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
having problem in compiling a simple c++ by gcc


Hi,
I have been away of c++ for 3 years but now I have to work on c++ again. To refresh my mind I had a look at my old, simple c++ codes which were written for DOS compiler. As the result I am not able to compile those codes under linux so I removed <conio.h> and <stdlib.h> and used <iostream> instead of them.

My code looks like this:
Code:
#include <iostream>



void fun(float a[],int n,int *quantity,float *summation)

{

	int i,q;

	float sum;

 	sum=0;

 	q=0;

 	for (i=0;i<n;i++) if (a[i]<a[n-1] && a[i]>0)

	{

		sum+=a[i];

		q++;

	}

 	*quantity=q;

 	*summation=sum;

}


void fun(float[],int,int *,float *);

void main()

{

	float a[20];

	int n,quantity,i;

	float ave,summation;

	cout << "input number of elements of array a"<< endl;

	cin >>n;

	cout << "input elements of array a"<< endl;

	for(i=0;i<n;i++) 
		cin >>a[i];

	fun(a,n,&quantity,&summation);

	if (quantity>0)

       	{

       		ave=summation/quantity;

       		cout <<"ave of positive elements less than last element="<<ave<<endl;

      	}

	else cout <<"there is no elements in array a to satisfy the cond.";
}
Unfortunately I get a very long error and I am not able to compile this program by following command:
Code:
g++ program.cpp
I am quite sure that gcc is configured properly on my ubuntu edgy machine which is almost fresh.

I should be thankful if anyone tell me what's wrong with my new codes.
 
Old 12-27-2006, 12:50 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need to tell us what the errors are. One thing is that main()'s return type should be "int", rather than "void". You probably have errors saying that cout, cin and endl are undeclared. You can:

1. Put "using namespace std;" under your include directive, or

2. put 3 lines: "using std::cout;", "using std::cin;" and "using std::endl;" under your include directive, or

3. change "cout", "cin" and "endl" everywhere to "std::cout", "std::cin" and "std::endl" respectively.
 
Old 12-27-2006, 12:50 AM   #3
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Well without the error message, I doubt anyone can help you fix the problem. I think you should also use the std namespace in your code e.g.
Code:
#include <iostream>
using namespace std
...
 
Old 12-27-2006, 12:59 AM   #4
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Here is the error after adding "using namespace std" (the real error is much longer though):

In file included from /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31,
from lab5.cpp:1:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
lab5.cpp:19: error: ‘::main’ must return ‘int’
lab5.cpp: In function ‘int main()’:
lab5.cpp:25: error: no match for ‘operator>>’ in ‘std::cin >> & n’
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:131: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:137: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:169: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:172: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:175: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>:perator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:178: note: std::basic_istream<_CharT, _Traits>&
 
Old 12-27-2006, 02:03 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
lab5.cpp:25: error: no match for ‘operator>>’ in ‘std::cin >> & n’
This suggests it thinks you're trying to pass a reference to n to cin, but your code says otherwise :/. Is your code exactly the same and you haven't declared n as a pointer?

Edit: the above code compiles fine for me, but I'm using an older version of gcc (3.4.6).

Also, change main's return type to int.

Last edited by Nylex; 12-27-2006 at 02:04 AM.
 
Old 12-27-2006, 02:16 AM   #6
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Thank you. Everything works fine by removing & after "cin >>" and changing void main to int main, although in DOS compiler this program runs properly when it s void main.

Anyhow, DOS is dead and linux is the best platform for compiling c++ codes, so I have to become accustomed to its special tricks.
 
  


Reply

Tags
compiling, gcc



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
gcc compiling problem chiahsin Linux - Software 2 12-02-2004 06:33 PM
simple c++/gcc problem jacksmash Programming 5 10-12-2004 01:13 PM
Trouble Compiling a simple Hellow World on Solaris 9 with GCC 3.4.2 jitendra64 Solaris / OpenSolaris 4 10-11-2004 02:57 PM
GCC compiling problem webwolf70 Linux From Scratch 3 09-25-2004 08:51 PM
Problem compiling gcc gkiagia Programming 4 05-31-2004 03:18 AM

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

All times are GMT -5. The time now is 11:43 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