-1

I am getting a compiling error.

||=== Build: Debug in 24.06.01 (compiler: GNU GCC Compiler) ===|

G:\C++\24.06.01\main.cpp|22|error: 'void sum(Sb1, Sb2)' redeclared as different kind of symbol|

G:\C++\24.06.01\main.cpp|5|note: previous declaration 'int sum [40]'|

G:\C++\24.06.01\main.cpp|36|error: 'void sum(Sb1, Sb2)' redeclared as different kind of symbol|

G:\C++\24.06.01\main.cpp|5|note: previous declaration 'int sum [40]'|

G:\C++\24.06.01\main.cpp||In function 'void sum(Sb1, Sb2)':|

G:\C++\24.06.01\main.cpp|38|error: 'void sum(Sb1, Sb2)' redeclared as different kind of symbol|

G:\C++\24.06.01\main.cpp|5|note: previous declaration 'int sum [40]'|

G:\C++\24.06.01\main.cpp||In function 'int main()':|

G:\C++\24.06.01\main.cpp|69|error: 'sum' cannot be used as a function|

||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

please help !!

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int sum[40];
int roll[40];
int n;
class Sb1;
class Sb2;
class Sb1
{
    public:
    float marks1[40];
    void markssb1()
    {
       for(int i=0;i<n;i++)
       {
         cout<<"Enter Marks in subject 1 \n";
         cin>>marks1[40];
       }
    }
    friend void sum(Sb1,Sb2);
};
class Sb2
{
    public:
    float marks2[40];
    void markssb2()
    {
       for(int i=0;i<n;i++)
       {
         cout<<"Enter Marks in subject 2 \n";
         cin>>marks2[40];
       }
    }
    friend void sum(Sb1,Sb2);
};
void sum(Sb1 obj1,Sb2 obj2)
{
    for(int i=0;i<n;i++)
    {
        sum[i]=obj1.marks1[i]+obj2.marks2[i];
    }
}
void display()
{
    cout<<"Serial Number\t\t\tRoll Number\t\t\tTotal Marks\n";
    cout<<"----------------------------------------------------------------------------\n";
    for(int i=0;i<n;i++)
    {
        cout<<i<<"\t\t\t\t"<<roll[i]<<"\t\t\t\t"<<sum[i]<<endl;
    }
}
int main()
{
    Sb1 obj1;
    Sb2 obj2;
   cout<<"Enter number of students (Maximum 40 students)\n";
   cin>>n;
   if(n<=40)
    {
        for(int i=0;i<n;i++)
        {
            cout<<"Enter Roll Number of student "<<i+1<<endl;
            cin>>roll[i];
            obj1.markssb1();
            obj2.markssb2();
        }
        sum(obj1,obj2);
        display();
    }
    else
    {
      cout<<"Number of students entered exceed 40\n";
    }
    return 0;
    getch();
}
  • ``? This does not seem to be C++11. Are you using turbo C++? – Curious Jun 25 '17 at 05:16
  • The error messages are self-explanatory. Rather than panicking over the number of messages, simply read them as a group and get a picture of what is happening. You have an array named `sum` and the `friend` declaration is for a function named `sum` that will be in the same scope. Although you can overload functions, having a function with the same name as a variable is not allowed. Rename one of them. – Peter Jun 25 '17 at 05:27
  • @Peter I didn't understand the error in which it says declared different kind of symbol .Anyways thanks !! – Garvit Kothari Jun 25 '17 at 05:37
  • 1
    @GarvitKothari - No excuse. A key skill you need to apply effort to learn is interpreting a set of error messages from a compiler. Software development and programming are about problem solving. One of the key problems a programmer needs to overcome is that of understanding error messages from their compiler, because a compiler is only smart enough to detect individual problems and report them, not reason out how to fix them. Humans have reasoning skills, and the ability to improve those. Compilers don't, so can't give simple explanations for sets of problems they encounter in code. – Peter Jun 25 '17 at 06:06
  • 1
    @Peter That, or dumping code on SO and letting someone else do the reasoning for you :-) – juanchopanza Jun 25 '17 at 08:35

1 Answers1

0

You have already declared 'sum' as a global variable:

int sum[40];

C++ doesn't allow redifining names. You can either change the variable or the function name to solve this problem.