question

hello, i have this code ,the problem with the part implimntation i dont know why does not run ,please help me with it

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
//as interface

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

template<class T>
class car{
string name;
T id;
public:
car(string n,T id);
string getname();
T getid();
};

//as implimnation 
#include<iostream>
#include<string>
#include"cm.h"
using namespace std;

car::car(string n,T id){
n=name;
id=i;
}
string car::getname(){return name;}
T car::getid(){return id;}

//part main
#include<iostream>
#include<string>
#include"cm.h"
using namespace std;


int main(){
car<int>obj("none",5);
obj.getid();

system("pause");
return 0;
}

thank you
Line 24,25: You have your assignments backwards.

Line 39: What's the point of this line? The call returns a string, but you're ignoring the return value.
24 ,25 is correct cuz in implimintation not heder file
39: i did in implimntation function wich getthe id

my question is this is implimntation is uncoreect ,so is that how to write implimntation of teampleat implimntation ?if it is not can you please coreect it for me?
24 ,25 is correct cuz in implimintation not heder file

No. The assignments are backwards as I stated. This has nothing to do with it being the implementation.

 
  n=name;

The above statement assigns the value of name to n. Not the other way around.

Since you have no default constructor, name (which is a member variable) is going to be an empty string. This empty string will be assigned to n which is an argument passed by value. The n goes out of scope when your constructor exists and its value is lost.

39: i did in implimntation function wich getthe id

My point is that line 39 calls the getter, but the returned value of the getter is ignored. If you're going to call a getter and ignore the return value, why call the getter at all?

my question is this is implimntation is uncoreect

Yes, your implementation is incorrect for the reasons I've pointed out.




Topic archived. No new replies allowed.