-3

I am getting some strange compiler errors with my code. I have pasted both the code and the class definitions below. I have tried Googling it but to no avail; the only thing I get is my constructor for Grade is wrong somewhere.

These are the build errors:

Evalution.cpp: In constructor ‘Evaluation::Evaluation(char*, Grade*)’:
Evalution.cpp:20:55: error: no match for call to ‘(Grade) (char [30],    double*)’
     Grades[num](Graded[num].comments,&Graded[num].mark);

This is Evaluation's definition:

    #include <iostream>
    #include <cstring>
    #include "Evalution.h"
    Evaluation::Evaluation(){
        code[0]='\0';
        for(int num=0;num<50;num++)
            Grades[num];
    }   
    Evaluation::Evaluation(char coursecode[],Grade Graded[]){
            if(coursecode[0]='\0'&& strlen(coursecode) !=6) {
                for(int num=0;num<50;num++){
                    if(Graded[num].mark < 0)
                Evaluation();
                numgrades=num;
            }
        }
            else{
                strcpy(code,coursecode);
                for(int num=0;num < 50; num++) {
                    Grades[num](Graded[num].comments,&Graded[num].mark);
                    numgrades=num;

            }
        }
    }
    bool Evaluation::empty()const{
        if(code[0]=='\0'){
            for(int num=0;num<numgrades;num++){
                if(Grades[num].mark < 0)
                    return  true;
            }
        }
        else
            return false;
    }
    float Evaluation::calculateAverage() const{
        float total=0;
        for(int num=0;num<=numgrades;num++)
            total+=Grades[num].mark;
        return total/numgrades;
    }
    void Evaluation::display(std::ostream& os) const{
        if(empty())
            return;
        else{
            float average=calculateAverage();
            os<< code << average <<endl;
            for(int num=0;num<=numgrades;num++)
                Grades[num].display(std::cout);
        }
    }   
    bool operator<(const Evaluation& tested, double pass){
        float average=tested.calculateAverage();
        if (average < pass)
            return true;
        else 
            return false;
    }
    std::ostream& operator<<(std::ostream& os, const Evaluation& input){
        os<<input.display(&os);
    }

This is Grade's definition:

    #include <iostream>
    #include "Grade.h"
    #include <cstring>
    using namespace std;
    Grade::Grade(){
        mark = 0;
        comments[0] = '\0';
    }
    Grade::Grade(char commented[], double &marked){
        if (commented == '\0' && marked < 0)
            Grade();
        else{
            strcpy(comments, commented);
            mark = marked;
        }
    }
double Grade::get() const{
    return mark;
}
void Grade::display(std::ostream& os) const{
    os << mark <<"-"<< comments << endl;
}

Also if there are any logical issues in the Evaluation class please let me know. Am getting the below linker errors now:

/tmp/cc1F2Crw.o: In function `main':
 15 w7part2.cpp:(.text+0x44): undefined reference to `Grade::Grade(char*, double)'
 16 w7part2.cpp:(.text+0x6e): undefined reference to `Grade::Grade(char*, double)'
 17 w7part2.cpp:(.text+0x98): undefined reference to `Grade::Grade(char*, double)'
 18 w7part2.cpp:(.text+0xb3): undefined reference to `Evaluation::Evaluation(char*, Grade*)'
 19 w7part2.cpp:(.text+0xc7): undefined reference to `Evaluation::display(std::ostream&) const'
 20 w7part2.cpp:(.text+0xef): undefined reference to `operator<(Evaluation const&, double)'
 21 collect2: error: ld returned 1 exit status
Nick Krause
  • 1
  • 1
  • 8
  • You didn't post any of the class definitions. You posted some member function bodies. "Class definition" looks like: `class Foo { ...stuff.... };`. You will need to post the actual class definitions because we need to know the meaning of `Grades` in order to answer your question, but it is not shown in the code you have posted so far. – M.M Mar 16 '15 at 03:55
  • Here are the Class definitions#include using namespace std; class Grade { friend class Evaluation; private: double mark; char comments[30]; public: Grade(); Grade(char commented[], double &mark); double get() const; void display(std::ostream& os) const; }; – Nick Krause Mar 16 '15 at 03:57
  • 2
    `std::string` is nice, you should take a look at it since you're programming C++ and not C. – jpw Mar 16 '15 at 03:58
  • 2
    Try to [explain to a rubber duckie](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) exactly what `os< – M.M Mar 16 '15 at 03:58
  • I known about but I can't :( this is not my work otherwise I would be already using lambas e.t.c and for each#include using namespace std; class Grade { friend class Evaluation; private: double mark; char comments[30]; public: Grade(); Grade(char commented[], double &mark); double get() const; void display(std::ostream& os) const; }; – Nick Krause Mar 16 '15 at 03:59
  • edit your post to include the class definitions (and format them properly) – M.M Mar 16 '15 at 04:00
  • @MattMcNabb, +1 for the rubber duckie method, works like a charm – taylorc93 Mar 16 '15 at 04:04

1 Answers1

0

I suspect you meant to say

Grades[num] = Grade(Graded[num].comments, Graded[num].mark);

instead of

Grades[num](Graded[num].comments,&Graded[num].mark);

Now, let me try to explain the syntax error in a round about way. If you have a class like:

class foo
{
   public:
      void operator()(int, int) {}
};

You could use:

Foo foos[10];
foos[0](10, 20);   // This calls the operator() function of the class.

In your statement, you have:

Grades[num](Graded[num].comments,&Graded[num].mark);
^^ Like foos[num]

Grades[num](Graded[num].comments,&Graded[num].mark);
           ^^ Like (10, 20)

Since Grade doesn't have such a function, the compiler is letting you know that but in a language that is not apparent to you.

EDIT

Now that I have thought about it, I am not sure what you are trying to do. If you use:

Grades[num] = Grade(Graded[num].comments, Graded[num].mark);

that is the same thing as self assignment.

EDIT 2

To take care of the second error message, use:

// Evaluation::display(...) returns void.
// Can't use:
// os<<input.display(...);
// Also, lose the & from the argument.
input.display(os);

instead of

os<<input.display(&os);
R Sahu
  • 204,454
  • 14
  • 159
  • 270