LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   having problem in compiling a simple c++ by gcc (https://www.linuxquestions.org/questions/programming-9/having-problem-in-compiling-a-simple-c-by-gcc-513949/)

mohtasham1983 12-27-2006 12:40 AM

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.

Nylex 12-27-2006 12:50 AM

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.

reddazz 12-27-2006 12:50 AM

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
...


mohtasham1983 12-27-2006 12:59 AM

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>::operator>>(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>::operator>>(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>::operator>>(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>::operator>>(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>::operator>>(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>::operator>>(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>&

Nylex 12-27-2006 02:03 AM

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.

mohtasham1983 12-27-2006 02:16 AM

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.


All times are GMT -5. The time now is 12:47 AM.