problem with Header and cpp files.

Hello,

I wrote a class that called CRational.

I wrote its header , and its cpp files.

I want to use my CRational class in other cpp file(that file is located in the same folder where CRational.h and CRational.cpp are located)

So in the start of the file , I wrote this :

1
2
#include <iostream>
#include "CRational.h" 


but When I am compile my code , I got some errors like that

undefined reference to `CRational::setNumerator(int)
undefined reference to `CRational::setDenominator(int)
undefined reference to `CRational::operator+(CRational)

etc...
and when I am trying to include CRational.cpp , it says that I am doing redefinition of main....

anyone know what do?

thanks for the helpers. |
Sounds like you might have put something which should have gone into the header file into the cpp file, or vice versa. Can you post CRational.h and CRational.cpp?
this is CRational.h

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
#ifndef CRATIONAL_H_INCLUDED
#define CRATIONAL_H_INCLUDED
#pragma once

class CRational
{
    private:
    int denominator , numerator;
    public:
    CRational(int , int);
    CRational () {};
    void setDenominator(int);
    void setNumerator(int);
    int getDenominator();
    int getNumerator();
    CRational reciprocal();
    void recReciprocal();
    CRational operator +(CRational);
    CRational operator -(CRational);
    CRational operator *(CRational);
    CRational operator /(CRational);
    CRational reduce();
    double printDouble();
    void printAsFraction();
    void printOneLine();
};

#endif // CRATIONAL_H_INCLUDED 



and this is CRational.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
#include "CRational.h"

using namespace std;



CRational::CRational(int a , int b)
{
    denominator=a;
    numerator=b;
}

int CRational::getDenominator()
{
    return denominator;
}

int CRational::getNumerator()
{
    return numerator;
}

void CRational::setDenominator(int dem)
{
    denominator=dem;
}

void CRational::setNumerator(int nem)
{
    numerator=nem;
}

CRational CRational::reciprocal()
{
    int a;
    a=denominator;
    denominator=numerator;
    numerator=a;
    CRational r(denominator , numerator);
    return r;
}

CRational CRational::operator+ (CRational param)
{
    CRational temp;
    int nem = numerator*param.getNumerator();
    temp.setNumerator(nem);
    int dem1 = (nem/numerator) * denominator;
    int dem2 = nem/param.getNumerator() * param.getDenominator();
    temp.setDenominator(dem1+dem2);
    return temp;
}

CRational CRational::operator- (CRational param)
{
    CRational temp;
    int nem = numerator*param.getNumerator();
    temp.setNumerator(nem);
    int dem1 = (nem/numerator) * denominator;
    int dem2 = nem/param.getNumerator() * param.getDenominator();
    temp.setDenominator(dem1-dem2);
    return temp;
}

CRational CRational::operator* (CRational param)
{
    CRational temp;
    int nem = numerator*param.getNumerator();
    temp.setNumerator(nem);
    int dem = denominator*param.getDenominator();
    temp.setDenominator(dem);
    return temp;
}

CRational CRational::operator/ (CRational param)
{
    param=param.reciprocal();
    CRational temp;
    int nem = numerator*param.getNumerator();
    temp.setNumerator(nem);
    int dem = denominator*param.getDenominator();
    temp.setDenominator(dem);
    return temp;
}

CRational CRational::reduce()
{
    CRational temp;
    int little;
    if (denominator>=numerator)
    {
        little=denominator;
    }
    else
    {
        little=numerator;
    }
    int num=0;
    for (int i=1;i<=little;i++)
    {
        if (denominator%i==0 && numerator%i==0)
        {
            num=i;
        }
    }
    temp.setDenominator(denominator/num);
    temp.setNumerator(numerator/num);
    return temp;
}


double CRational::printDouble()
{
    double i = (double)denominator/(double)numerator;
    return i;
}

void CRational::printAsFraction()
{
    cout<<denominator<<endl<<"--"<<endl<<numerator<<endl;
}

void CRational::printOneLine()
{
    cout<<denominator<<"/"<<numerator;
}

int returnInt(char a[], int n , int n2)
{
    if (a[n2]=='\0')
    {
        return n;
    }
    n=n*10+a[n2]-'0';
    returnInt(a , n , n2+1);
    return n;
}

void recReciprocal(char a[] , char b[])
{
    if (a[0]=='.' || b[0]==-'.')
    {
        return;
    }
    else
    {
       int dem=returnInt(a , 0 , 0);
       int num=returnInt(b , 0 , 0);
       CRational r(dem , num);
       char c[256];
       char d[256];
       cout<<"enter a number"<<endl;
       cin>>c;
       cout<<"enter a number"<<endl;
       cin>>d;
       recReciprocal(c,d);
       cout<<r.reciprocal().printDouble()<<endl;
    }
}



int main()
{
    char a[256];
    char b[256];
    cout<<"enter a number"<<endl;
    cin>>a;
    cout<<"enter a number"<<endl;
    cin>>b;
    int a2=returnInt(a , 0 , 0);
    int b2=returnInt(b , 0 , 0);
    CRational r(a2 , b2);
    char c[256];
    char d[256];
    cout<<"enter a number"<<endl;
    cin>>c;
    cout<<"enter a number"<<endl;
    cin>>d;
    int c2=returnInt(c , 0 , 0);
    int d2=returnInt(d , 0 , 0);
    CRational r2(c2 , d2);
    (r*r2).reduce().printAsFraction();
    (r*r2).reduce().printOneLine();
    return 0;
}



Not really sure why you are getting the errors you are, but I would move all functions that are not part of the CRational class to another file... for example, your main(), returnInt(), and returnReciprocal() should not be in CRational.cpp.

Also, all the #include's the class needs, and the using namespace commands can be put inside the header file right under the #ifndef/#define guards.

See if that fixes anything..
Are you sure that CRational.cpp is compiled? And that then CRational.o is linked to main.o? (Or whatever your compiler calls these files). What compiler/IDE are you using?
mahlerfive ,
It worked.
thanks a lot.

and thank you too exception.

Last edited on
Topic archived. No new replies allowed.