Undefined Reference in Main (Using Classes)

Hi guys,

I'm getting an "Undefined Reference Error" in my main() function:

Here are my classes:

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
class CreditAccount // The Class
{
public:
	
	CreditAccount( char [], char [], char [], int, int , double, double); // The constructor
	// **** The Methods ***
	void showAccount();
	void setAcctNum (char []);
	void setName( char [], char []);
	void setExpiration(int , int );
	void setLimit(double );
	void changeBalance( double );
	

	
private:

	char acctNum[20];
	char lastName[21];
	char firstName[21];
	
	int expMonth;
	int expYear;
	double creditLimit;
	double balance;



and here is my main:


1
2
3
4
5
6
7
8
CreditAccount c3( "1234-9876-2468-1357", "Jack", "Alewyin", 7, 2013, 1500.00, 1130.77); //this is where i get the error
CreditAccount c4( "6549-9741-4984-3879", "Lewis", "Kelvin", -2, 2011, -500, 1234.50); // Here as well


c3.showAccount();

c4.setName("Jack", "Lewis");
c4.showAccount();
Please post the error from the compiler, you'd be surprised at the information that you can gather from the messages.

I'm guessing it is complaining about a function... constructor.. that is trying to take a const char * for parameter 1 2 and 3. Either change the constructor to accept string literals or first create a char array...

Either Do this:
1
2
3
4
char account3[] = {"1234-9876-2468-1357"};  //do this for jack, and alewyn..
char account4[] = {"6549-9741-4984-3879"}; //do this for Lewis and Kelvin...
//...
CreditAccount c3(account3, jackBuff, alewynBuff, 7, 2013, 1500.00, 1130.77);

OR change your constructor...
 
CreditAccount( const char *, const char *, const char *, int, int , double, double); // The constructor 
undefined reference to 'CreditAccount::CreditAccount(char*, char*, char*, int, int, double, double)'
The thing is that I can't remember how to assign values to a class constructor.
Do they have to be char array or the likes? If not you can use std::string and just do...


CreditAccount( const char * acct, const char * first, const char * last /*...*/) : acctNum(acct), firstName(first), lastName(last) {//...}
No, they have to be char array.
Then press them as char array and not as pointers
Interesting suggestion. But how do I press them ?

as single like { 'a' 'b' c' ....} ?
can you past full code so that we can analysis what is going on .

xx
Are you linking in the object file containing the code for CreditAccount?
I am linking the Constructor, aren't I ?
Oops not press, pass.

Then pass them as char array and not as pointers.

1
2
3
char account3[] = {"1234-9876-2468-1357"}; //This is a char array, now pass this!
//Pass like this...
CreditAccount c3(account3, jackBuff, alewynBuff, 7, 2013, 1500.00, 1130.77); //Get the idea? 
No dice. I tried that before I tried this.

The thing is that this that I wanted to pass them as char so that I can use less code than strings. Also since strings are a 'added mod' of c++, I wanted to use char.

It should work, shouldn't it ?
Are you not allowed to you std::string? They are far and away easier to use IMO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CreditAccount // The Class
{
public:
   CreditAccount( const std::string &, const std::string &, const std::string& , int, int , double, double); // The constructor
   // **** The Methods ***
private:
   std::string acctNum;
   std::string lastName;
   std::string firstName;
   //...
}

//Constructor implementation...
CreditAccount::CreditAccount(const std::string& acctn,const std::string& lastn, const std::string& firstn, int emonth, int eyear, double limit, double bal) :
	 acctNum(acctn), lastName(lastn), firstName(firstn), expMonth(emonth), expYear(eyear), creditLimit(limit), balance(bal)
	{
	}


Then you can pass your strings as you've already done...
Last edited on
So there is no way to pass them as characters ?

I tried strings and it works, but was hoping to use characters. They should work, right ?
Sure, an array of characters is passed as a pointer to char.
I am linking the Constructor, aren't I ?


No, you aren't. That's what the error is saying.

undefined reference to 'CreditAccount::CreditAccount(char*, char*, char*, int, int, double, double)'


It doesn't know where the constructor is. That might mean that you aren't linking in the object file that contains the constructor.
Topic archived. No new replies allowed.