Errors with using templates

Hello,
I've written the following code and keep getting the errors:

Error 1 error C2244: 'Supermarket<temp>::operator =' : unable to match function definition to an existing declaration
Error 2 error C2244: 'Supermarket<temp>::setName' : unable to match function definition to an existing declaration
Error 3 error C2244: 'Supermarket<temp>::setArea' : unable to match function definition to an existing declaration

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
#ifndef SUPERMARKET_H
#define SUPERMARKET_H

#include<string>
#include<iostream>
using namespace std;

template<class temp>
class Supermarket{
private:
	string*Sname;
	int*Area;
	int size;
	string*Products;//array
	temp*Price;//array
public:
	Supermarket(string,int,int);
	~Supermarket();
	Supermarket(Supermarket&);
	Supermarket& operator=(Supermarket&);
	Supermarket& setName(string a);
	Supermarket& setArea(int);
	string Expensive();
	string Cheapest();
	void Print();
};
.
.
.
.
.

template<class temp>
Supermarket<temp>::Supermarket(Supermarket&a){
	size=a.size;
	Sname=new string(*a.Sname);
	Area=new int(*a.Area);
	Products=new string[5];
	for(int i=0;i<5;i++)
		Products[i]=a.Products[i];
	Price=new temp[5];
	for(int j=0;j<5;j++)
		Price[j]=a.Price[j];
}
template<class temp>
Supermarket& Supermarket<temp>::operator=(Supermarket&a){
	if(this!=&a){
		*Sname=*a.Sname;
		*Area=*a.Area;
		if(size!=a.size){
			size=a.size;
			delete[]Products;
			delete[]Price;
			Products=new string[size];
			Price=new temp[size];
		}
		for(int i=0;i<size;i++){
			Products[i]=a.Products[i];
			Price[i]=a.Price[i];
		}
	}
	return*this;
}
template<class temp>
Supermarket& Supermarket<temp>::setName(string a){
	Sname=a;
	return*this;
}
template<class temp>
Supermarket& Supermarket<temp>::setArea(int a){
	Area=a;
	return*this;
}
.
.
.
.


[Update:]
I moved the files to the .h file, and now I'm getting

Error 2 error LNK1120: 1 unresolved externals

Thank you for your help :)
Last edited on
Hmm do you keep those in two separate files, .hpp (or .h) and .cpp?

If yes, move the contents of the .cpp to the header file.
Yeah I do. I moved the contents of .cpp file to the header file, now a LNK error appeared

Error 1 error LNK2001: unresolved external symbol _mainCRTStartup
Be sure to keep main() in a .cpp file.
I seem to have misunderstood you.
I already had the .cpp and .h contents in one file. I just moved the main to the .h file (which was weird ) and got the LNK error.
¿Do you have the template definitions in the .h?
¿What are those dots in lines 27-31?

Sname=new string(*a.Sname); ¿why dynamic allocation?
operator=(const Supermarket&);
Yeah, I do. Now I'm getting linking error (Error 2 error LNK1120: 1 unresolved externals)

(I'm using dynamic allocation because it's what's stated in my HW)

These dots just mean I haven't wrote the whole code, just the one with the problem.
Last edited on
Create a test case.

> I'm using dynamic allocation because it's what's stated in my HW
Please re-read it.
`Products' and `Price' may be dynamic arrays, but you limited their size.
`Name' and `Array' `Area' has no justification.
Last edited on
Now I'm getting linking error (Error 2 error LNK1120: 1 unresolved externals)


We can continue in two ways:

1) you don't give us the full code, and we'll keep guessing what could be the problem

2) you post all the code and label which files they are (of course, each in separate code sections), then we can at least determine if its a source code problem or not (in which case you'll want to check the project/linker settings)
No, I'm pretty sure all the members except size are dynamic.

What do you mean exactly by 'limiting their size'?

In the constructor size gets initialized, and I declared Products like this:

Products=new string[size];

or shouldn't I do that?
Just wanna say, thank you all :) It worked :) (The problem existed in the fact that I stored a a value other than an address in a pointer in Sname=a) thank you again :)
Last edited on
And next time I'll put the whole code, still a newbie :P
Topic archived. No new replies allowed.