redefinition of class

I having issues writing a class in multiple files. I keep getting a "Redefinition of 'Ratio'" error and im not sure how to resove it

This is the Ratio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include "Ratio.h"

#include <iostream>

using namespace std;

class Ratio{
public:
    //constructor
    Ratio(int n, int d){
        setNum(n, d);
    }
    
    
    //function to set the number to numerator and denominator
    void setNum(int n = 1, int d = 1){
        numerator = n;
        denominator = d;
    }
    //function to display numerator and denominator
    void getNum(){
        cout << numerator << "\n" << denominator << endl;
    }
    
private:
    int numerator;
    int denominator;
};


and this is in the header File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef RATIO_H
#define RATIO_H

#include <iostream>

using namespace std;

class Ratio{
public:
    //constructor
    Ratio(int n, int d);
    
    //function to set the number to numerator and denominator
    void setNum(int n = 1, int d = 1);
    
    //function to display numerator and denominator
    void getNum();
    
private:
    int numerator;
    int denominator;
};
#endif
Hate to break it to you, but you're redefining the class. Your Ratio.cpp looks almost identical to the .h. Also, in your header file: you usually don't want to use "namespace std;" in there, and you can likely delete the <iostream> include, since nothing iostream is being used.
You're getting a "Redefinition of Ratio" error because you are redefining Ratio :)

Your implementation file, Radio.cpp, should not re-declare the Ratio class ( class Ratio { ... }; ), because that is already in the header.

You should be doing it like this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//constructor
Ratio::Ratio(int n, int d) {
    setNum(n, d);
}
    
//function to set the number to numerator and denominator
void Ratio::setNum(int n, int d) {
    numerator = n;
    denominator = d;
}

//function to display numerator and denominator
void Ratio::getNum() {
        cout << numerator << "\n" << denominator << endl;
}
a
Also noticed you'd probably benefit from friending an output stream, so you may need <iostream> in there after all ;D

Ratio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef RATIO_H
#define RATIO_H
#include <iostream>

class Ratio
{
public:
    Ratio(int n, int d);
    friend std::ostream& operator<<(std::ostream &os, const Ratio& r);

private:
    int numerator_;
    int denominator_;
};

std::ostream& operator<<(std::ostream& os, const Ratio& r)
{
    os << r.numerator_ << "/" << r.denominator_ << std::endl;
    return os;
}
#endif 


Ratio.cpp
1
2
3
4
5
#include "Ratio.h"

Ratio::Ratio(int n, int d) : numerator_(n), denominator_(d)
{
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Ratio.h"
#include <iostream>

using namespace std;

int main()
{
    Ratio r(4, 5);
    cout << r;

    return 0;
}


Untested, but should work.


Last edited on
Topic archived. No new replies allowed.