Simple class I'm struggling with

This is my simple main, i'll leave out the includes and header files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 int main()
{
    
 double loyaltyNumber ;
 int loyaltyPoints;
 char loyaltyExpiry[50];
 Loyalty l1(loyaltyNumber, loyaltyPoints, loyaltyExpiry);
 
 cout << "meow" <<endl;
 cin >> loyaltyNumber;
 l1.setLoyaltyNumber(loyaltyNumber);
 
 system("PAUSE");
 return 0;   
}


Then this is my header file for 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
#ifndef LOYALTY_H_INCLUDED  // must be unique!
#define LOYALTY_H_INCLUDED

class Loyalty 
{
      public:
             
      Loyalty();
      
      Loyalty(double loyaltyNumber, int loyaltyPoints, char loyaltyExpiry[]);

      void setLoyaltyNumber(double loyaltyNumber);
      double getLoyaltyNumber();     
      
      void setLoyaltyPoints(int loyaltyPoints);
      int getPoints();       
      
      void setLoyaltyExpiry(char loyaltyExpiry[]);
      char* getLoyaltyExpiry();       
      
            
      private:
      double loyalty_number;
      int loyalty_points;
      char loyalty_expiry[50];
};

#endif 


and finally my function file
1
2
3
4
5
6
7
8
void Loyalty::setLoyaltyNumber(double loyaltyNumber)
{
	loyalty_number = loyaltyNumber;
}
double Loyalty::getLoyaltyNumber()
{
	return loyalty_number;
}


I'm getting this as my error:
[Linker error] undefined reference to `Loyalty::Loyalty(double, int, char*)'
ld returned 1 exit status
Z:\c++\Task 2 - real files\Makefile.win [Build Error] [gameshop.exe] Error 1
Hi


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


 

class Loyalty{
public:      
      Loyalty();
      Loyalty(double loyaltyNumber, int loyaltyPoints, char loyaltyExpiry[]);

      void setLoyaltyNumber(double loyaltyNumber);
      double getLoyaltyNumber();     
      
      void setLoyaltyPoints(int loyaltyPoints);
      int getPoints();       
      
      void setLoyaltyExpiry(char loyaltyExpiry[]);
      char* getLoyaltyExpiry();       
private:
      double loyalty_number;
      int loyalty_points;
      char loyalty_expiry[50];
};




     
#endif


//Loyalty.cpp

#include "Loyalty.cpp"  // include headerfile

void Loyalty::setLoyaltyNumber(double loyaltyNumber)
{
	loyalty_number = loyaltyNumber;
}
double Loyalty::getLoyaltyNumber()
{
	return loyalty_number;
}
 




I really don't see what you changed there other than including the file I was writing in.

#including "Loyalty.cpp" doesn't make sense when I'm coding in loyalty.cpp
Last edited on
Your problem here is that you have declared a constructor Loyalty(double loyaltyNumber, int loyaltyPoints, char loyaltyExpiry[]) in the class definition but haven't provided a function definition for it. So the error is basically telling you this. It knows such a function should exist but it can't find it.
Simply write a function definition for it. (i.e Loyalty::Loyalty(double loyaltyNumber, int loyaltyPoints, char loyaltyExpiry[])
Found out the problem was actually that I was writing an integer to a character member :)

Thanks for the help though :)
Topic archived. No new replies allowed.