linker error

I have a linking error in my program that I'm having difficulty fixing. I'll post my program below (in parts because I can't fit it in one post)

header DollarAmount.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
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
#ifndef DollarAmount_h
#define DollarAmount_h
using namespace std;

class DollarAmount{
    
public:
    /* precondition: d>=0, c>=0, c<100 */
    DollarAmount (int d=0, int c=0){
        dollar = d;
        cent = c;
    }
    
    /* postcondition: the dollar amount represented by
     the calling object is displayed in English */
    void PrintInEnglish();
    
    friend istream & operator >> (istream &in, DollarAmount & d);
    
    
    void print(ostream & stream) const;
    void input (istream & stream);
    friend DollarAmount operator+(DollarAmount &,DollarAmount &);
    friend bool operator> (DollarAmount &, DollarAmount &);
    friend bool operator< (DollarAmount &, DollarAmount &);
    friend bool operator== (DollarAmount &, DollarAmount &);
    friend DollarAmount operator/(DollarAmount &, int);
    int getdollar();
private:
    int dollar;
    int cent;
    
    /* some helper function to be used by PrintInEnglish */
    static void PrintDollar (int d);
    
    static void Print0_99 (int value);
    
    /* display ten, twenty, ..., nighty
     @param ten_digit: is the tenth digit's value
     @precondition: ten_digit has a value from 1 to 9
     @postcondition: ...
     */
    static void PrintTens (int ten_digit);
    
    static void PrintOnes (int one_digit);
    
    /* display eleven, ... nineteen
     @param one_digit: the one-th digit
     @precondition: one_digit has a value from 1, 2, ...,9
     @postcondition: ... */
    static void PrintTeens (int one_digit);
    
};

#endif /* DollarAmount_h */  
DollarAmount.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
188
189
190
191
192
193
194
#include <iostream>
#include <assert.h>
#include <sstream>
#include "DollarAmount.h"

using namespace std;

void DollarAmount::print(ostream & stream) const
{
    stream <<dollar<<"."<<cent;
}

void DollarAmount::input (istream & stream)
{
    
    int d, c;
    char ch;
    bool success=false;
    
    do {
        stream >> d>> ch >> c;
        if (stream.fail()){
            cout <<"failed to enter\n";
            stream.clear();
            stream.ignore (2048,'\n');
        } else {
            if (d<0 || c<0 || c>99)
                cout <<"Value not in range\n";
            else
                success=true;
        }
        if (!success)
            cout <<"try again\n";
    } while (!success);
    
    dollar = d;
    cent = c;
}



//a friend function is not a member function
istream & operator >> (istream & in, DollarAmount & d)
{
    d.input (in);
    return in;
}

ostream & operator<< (ostream & out, const DollarAmount & d)
{
    d.print (out);
    return out;
}


/* postcondition: the dollar amount represented by
 the calling object is displayed in English */
void DollarAmount::PrintInEnglish()
{
    PrintDollar(dollar);
    
    if (cent!=0)
    {
        cout <<" and " << cent <<"%100"<<endl;
    }
    
}

/* some helper function to be used by PrintInEnglish */

void DollarAmount::PrintDollar (int d)
{
    assert (d>=0 && d<=9999);
    
    int hundreds = d/100;
    Print0_99 (hundreds);
    cout <<" hundred ";
    
    int remaining = d%100;
    if (remaining!=0)
    {
        Print0_99 (remaining);
    }
}

/* precondition: value>=0, value <=99 */
void DollarAmount::Print0_99 (int value)
{
    int tens=value/10;
    int ones=value%10;
    
    if (tens==0)
    {
        cout <<" and ";
        PrintOnes (ones);
    } else if (tens==1)
    {
        PrintTeens (ones);
    }
    else
    {
        PrintTens (tens);
        cout <<" ";
        PrintOnes (ones);
    }
}

/* display ten, twenty, ..., nighty
 @param ten_digit: is the tenth digit's value
 @precondition: ten_digit has a value from 1 to 9
 @postcondition: ...
 */
void DollarAmount::PrintTens (int ten_digit)
{
    string name[10]={"","ten","twenty","thirty","forty","fifty",
        "sixty","seventy","eighty","ninety"};
    
    assert (ten_digit>=0 && ten_digit<=9);
    cout <<name[ten_digit];
}

void DollarAmount::PrintOnes (int one_digit)
{
    string name[10]={"zero","one","two","three","four","five",
        "six","seven","eight","nine"};
    
    assert (one_digit>=0 && one_digit<=9);
    cout <<name[one_digit];
    
}

/* display eleven, ... nineteen
 @param one_digit: the one-th digit
 @precondition: one_digit has a value from 1, 2, ...,9
 @postcondition: ... */
void DollarAmount::PrintTeens (int one_digit)
{
    string name[10]={"ten","eleven","twelve","thirteen","forteen","fifteen",
        "sixteen","seventeen","eighteen","nineteen"};
    
    assert (one_digit>=0 && one_digit<=9);
    cout <<name[one_digit];
}
bool operator>(DollarAmount &d, DollarAmount &d1){ //Operator overload >
    bool ret=false; //return value
    if(d.dollar > d1.dollar){ //if d1 > d2 it returns true
        ret=true;
    }
    if(d.dollar == d1.dollar){
        if (d.cent > d1.cent){
            ret=true;
        }
    }
    return ret;
}
bool operator<(DollarAmount &d, DollarAmount &d1){
    bool ret=true;
    if(d > d1){
        ret=false;
    }
    if(d==d1){
        ret=false;
    }
    return ret;
}
bool operator==(DollarAmount &d, DollarAmount &d1){
    bool ret=false;
    if(d.dollar==d1.dollar && d.cent==d1.cent){
        ret=true;
    }
    return ret;
}
DollarAmount operator+(DollarAmount &d1, DollarAmount &d2){ //Operator Overload for +
    DollarAmount d3;
    d3.dollar=d1.dollar+d2.dollar; //adds dollars together
    d3.cent=d1.cent+d2.cent;//adds cents together
    if(d3.cent > 100){ //if cents are greater than 100, it adds 1 to the dollar amount
        d3.cent= d3.cent-100;
        d3.dollar= d3.dollar+1;
    }
    return d3;
}
DollarAmount operator/(DollarAmount &d1, int x){ //operator overload for division of a dollar and an integer
    DollarAmount d2;
    d2.dollar=d1.dollar/x; //divides dollars
    d2.cent=d1.cent/x; //divides cents
    if(d1.cent==0){
        d2.cent=50;
    }
    return d2;
}
int DollarAmount::getdollar(){
    return (dollar);
}


main.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
template<class T>
void sort(T a[], int size);
#include <iostream>
#include "DollarAmount.h"
#include "DollarAmount.cpp"
using namespace std;
//function Declarations
DollarAmount sum(DollarAmount a[], int size);
void median(DollarAmount a[], int size);
void copy(DollarAmount a[], DollarAmount b[], int size);

int main(){
    int DEFAULTSIZE(10), size(0); //Creates variables for default size, and size of the array
    bool stop=false;
    DollarAmount* ptr=NULL; //Creates a pointer of dollar amounts
    ptr= new DollarAmount[DEFAULTSIZE]; //creates an array of type dollar DYNAMICALLY
    do{
        cout << "Enter the dollar amount of the Item (e.g. $1.50,  enter -1.00 to end):$";
        cin >> ptr[size]; //user input
        if (ptr[size].getdollar()==-1){ //stops the loop if -1
            stop=true;
        }
        else{
            size++; //increases size of array
        }
        if (size==DEFAULTSIZE){ //if the user enters more than the default amount
            DollarAmount *temp= new DollarAmount[2*size]; //it creates an array thats double the size of the last
            copy(temp, ptr, size); //copies the array
            delete[]ptr; //deletes old ptr
            ptr=temp;
        }
    }while(!stop);
    sort(ptr, size); // sorts them in least to greatest
    for (int x=0; x<size; x++){
        cout << ptr[x] << endl; // displays it
    }
    DollarAmount s;
    s=sum(ptr, size); //total sum of the array
    cout << "Your total today is:" << s << " ("; //displays total
    s.PrintInEnglish(); //prints the total in english
    cout << ")" <<endl;
    median(ptr, size);
    return 0;
}
template<class T>
void sort(T a[], int size){ //bubble sort function
    int last(size-1);
    for(int x=0; x<size; x++){
        for(int i=0;i<last;i++){
            if(a[i] > a[i+1]){
                T temp=a[i];
                a[i]=a[i+1];
                a[i+1]=temp;
            }
        }
    }
}
DollarAmount sum(DollarAmount a[], int size){ // calculates the sum of the Array of dollars
    DollarAmount s{};
    for(int x=0; x<size; x++){
        s=s+a[x];
    }
    return(s);
}
void median(DollarAmount a[], int size){ // calculates the median of the array
    int median;
    if(size%2==!0){
        median= size/2;
        cout << "The median is " << a[median] <<endl;
    }
    else{
        median= size/2;
        DollarAmount m {};
        m= a[median]+a[median-1];
        m= m/2;
        cout << "The median is " << m <<endl;
    }
}
void copy(DollarAmount a[], DollarAmount b[], int size){ // copy function, copies contents of array b to array a.
    for(int x=0; x<size; x++){
        a[x]=b[x];
    }
}

Hello cash,

The linker error comes from:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class T>
void sort(T a[], int size);
#include <iostream>
#include "DollarAmount.h"
#include "DollarAmount.cpp"

using namespace std;

//function Declarations
DollarAmount sum(DollarAmount a[], int size);
void median(DollarAmount a[], int size);
void copy(DollarAmount a[], DollarAmount b[], int size);

int main()

"DollarAmount.cpp" is most likely included in the project and quite possibly compiled first before "main". All you are doing is adding code to "main" that you already have.

When I put a comment on this line the linker error went away.

There are still other errors in the program like:
1
2
3
DollarAmount s;

cout << "Your total today is:" << s << " ("; //displays total 

Here you are trying to print an entire class and the "cout" has no idea how to do that. Did you maybe want cout << "Your total today is:" << s.getdollar() << " ("; //displays total ?

The errors I get from VS2017 are:

Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	E0349	no operator "<<" matches these operands	cash linker error	F:\VS 2017\cash linker error\cash linker error\Main V1.cpp	100	
Error (active)	E0349	no operator "<<" matches these operands	cash linker error	F:\VS 2017\cash linker error\cash linker error\Main V1.cpp	108	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'DollarAmount' (or there is no acceptable conversion)	cash linker error	f:\vs 2017\cash linker error\cash linker error\main v1.cpp	47	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'DollarAmount' (or there is no acceptable conversion)	cash linker error	f:\vs 2017\cash linker error\cash linker error\main v1.cpp	54	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'DollarAmount' (or there is no acceptable conversion)	cash linker error	f:\vs 2017\cash linker error\cash linker error\main v1.cpp	100	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'DollarAmount' (or there is no acceptable conversion)	cash linker error	f:\vs 2017\cash linker error\cash linker error\main v1.cpp	108	
Warning	C4805	'==': unsafe mix of type 'int' and type 'bool' in operation	cash linker error	f:\vs 2017\cash linker error\cash linker error\main v1.cpp	97	


In the function"median" the if statement if (size % 2 == !0). I believe what you want is if (size % 2 != 0).

Hope that helps,

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.