user3652172 user3652172 - 15 days ago 11
C++ Question

VS13 - C++ - program doesn't compile, error in sal.h (didn't edit this file - least not on purpuse)

I'm trying to compile simple operators overload program and I'm getting this error (didn't understand yet how to deal with it).

main.cpp (without anything, just for compile)

#include "Rational.h"
#include <iostream>
using namespace std;


void main()
{

}


Rational.h -

#ifndef __RATIONAL_H
#define __RATIONAL_H
class Rational
{

private:
int numerator;
int denominator;

public:
Rational(int numerator,int denominator);
void setNumerator(int newNumerator);
int getNumerator() const;
void setDenominator(int newdenominator);
int getDenominator() const;
//Rational operator+(Rational& other);
void print() const;
}
#endif


Rational.cpp:

#include "Rational.h"
#include <iostream>
using namespace std;
Rational::Rational(int newNumerator, int newDenominator){
this->setNumerator(newNumerator);
this->setDenominator(newDenominator);
}
void Rational::setNumerator(int newNumerator){
this->numerator = newNumerator;
}
int Rational::getNumerator() const{
return this->numerator;
}
void Rational::setDenominator(int newDenominator){
if (newDenominator != 0){
this->denominator = newDenominator;
}
}
int Rational::getDenominator() const {
return this->denominator;
}
/*
Rational Rational::operator+(Rational& other) {
if (denominator == other.denominator){
return Rational(numerator + other.numerator, denominator);
}
else
{
return Rational(numerator*other.denominator + other.numerator*denominator, denominator*other.denominator);
}
}
*/
void Rational::print() const{
cout << "Num: " << this->denominator << "\"" << this->numerator << endl;
}


and here are the error from the build and the relevant part from sal.h

sal.h & errors

Answer

You forgot a semicolon after your class declaration in the .h file.

#ifndef __RATIONAL_H
#define __RATIONAL_H

class Rational
{

private:
    int numerator;
    int denominator;

public:
    Rational(int numerator, int denominator);
    void setNumerator(int newNumerator);
    int getNumerator() const;
    void setDenominator(int newdenominator);
    int getDenominator() const;
    //Rational operator+(Rational& other);
    void print() const;
};     <------ semicolon here you forgot
#endif

I do it all the time, I can never tell when it needs to have a colon and when it doesn't. And the error messages make no sense when it happens. Class declarations need it, functions, umm, and I can't think what else don't.