编译错误的指导,Visual Studio 2013上的C ++

时间:2016-11-24 09:29:39

标签: c++ visual-studio

我试图实现简单的运算符重载并不断出错。

知道接下来要检查什么?

*我正在使用Visual Studio 2013环境,代码是用C ++编写的。 谢谢。

的main.cpp

#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;
}

以下是构建中的错误以及来自sal.h的相关部分

sal.h & errors

1 个答案:

答案 0 :(得分:0)

在.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;
};     <------ semicolon here you forgot
#endif

我一直这样做,我永远不知道什么时候需要结肠,什么时候不结肠。并且错误消息在发生时毫无意义。类声明需要它,函数,嗯,我想不出还有什么。