C++ help :s

Soldato
Joined
17 Jul 2005
Posts
3,193
Hi all,

I'l freely admit I suck at programming, decided to try and improve a bit, so took it as a module at uni. Just started trying to get to grips with it, and understand all the basics, and have managed to get classes working etc... but can't work out why this won't work :s (did Java a few years back so maybe thats confusing me!) I basically want to print the details out rather than calling the individual get's...

account.h said:
// Account.h
//Class File

class Account
{
private:

int nAccountNumber;
char strAccName[50];

public:

void SetAccountNumber(int);
void setHolderName(char *);


int getAccountNumber(void);
char * getHolderName(void);
void displayDetails(char*, int);

};

//Actual implementation of class

<SNIP>

void displayDetails(char * accName, int dAccNum)
{
std::cout << "Account name : " << accName << std::endl;
std::cout << "Account number : " << dAccNum << std::endl;
}

And this is the call in my main...

main.cpp said:
cout << Steve.displayDetails() << endl;

Any help appreciated!

OH the error!!

Says there is no such function in account.h ... and then suggests below that that "void displayDetails(char * accName, int dAccNum)" is the closest candidate... won't work though.
 
Last edited:
It's because the signature of the function in the header file is
Code:
void displayDetails(char * accName, int dAccNum)
Whereas your not providing any parameters when you call it in main.


Also, your trying to print a function which returns void, you only need to put
Code:
Steve.displayDetails(accName, dAccNum)
No need for the std::cout
 
How about this approach :

Code:
std::ostream& operator<<(std::ostream& os, const Account& account) {
        os << "account details for : " << account.strAccName << std::endl;
        return os;
}

Then you can output instances of 'Account' :

Code:
Account steve;
/* setup 'steve' here */
cout << steve << endl;

The '<<' operator is basically shorthand for calling a function :

Code:
cout << steve << endl;

is actually :

Code:
(std::ostream::operator<<(std::cout, steve)).operator<<(std::endl);
 
Last edited:
Cheers,

I did that, but now it says that the variables aren't declared in main.cpp ..

from ;

Steve.displayDetails(accName,dAccNum);

I declare them at the top, and it compiles but crashes when you run... I take it im missing something blatently obvious :o
 
NiCkNaMe said:
Cheers,

I did that, but now it says that the variables aren't declared in main.cpp ..

from ;

Steve.displayDetails(accName,dAccNum);

I declare them at the top, and it compiles but crashes when you run... I take it im missing something blatently obvious :o
As the displayDetails function is declared within the class, you dont need to pass in any variables. What you have will be fine, just get rid of the parameters your passing to it. Then call it with no parameters.

Have you done any programming before?

Sorry, was a quick reply before i went to uni :o
 
Just had another look at it and feel really thick, but I still don't understand.

Did some Java last year, and after a while got to grips with it - but find it hard to grasp programming for some reason (should have just done another networking module :o)

I've got this;

main.cpp said:
#include <string>
#include <iostream>
#include "account.h"

using namespace std;

int main(void)


{

int a;

cout << "Bank Account" << endl << endl;

//Creating an account

Account Steve;

//Setting values

Steve.SetAccountNumber(100);
Steve.setHolderName("Ste");

//Printing details

//cout << "Account number is :: " << Steve.getAccountNumber() << endl;
//cout << "Account Holder is :: " << Steve.getHolderName() << endl;

Steve.displayDetails();

cin >> a;


return 0;

}

account.h said:
// Account.h
//Class File

using namespace std;

class Account
{
private:

int nAccountNumber;
char strAccName[50];

public:

void SetAccountNumber(int);
void setHolderName(char *);

void displayDetails(char *, int);
int getAccountNumber(void);
char * getHolderName(void);

};

//Actual implementation of class

int Account::getAccountNumber(void)
{
return nAccountNumber;
}

char * Account::getHolderName(void)
{
return strAccName;
}

void Account::SetAccountNumber(int dAccNum)
{
nAccountNumber = dAccNum;
}

void Account::setHolderName(char * accName)
{
strcpy(strAccName,accName);
}

void Account::displayDetails(char * accName, int dAccNum)
{
cout << "Account name : " << accName;
cout << "Account number : " << dAccNum;
}

I've tried loads of combo's, tried it with the variables in the main... should it even be void in the class file because it's returning something? :confused:
 
FerretBoy said:
void displayDetails();

...

void Account::displayDetails()
{
cout << "Account name : " << accName;
cout << "Account number : " << dAccNum;
}


this is correct as you wish to display details of the class (account) private data members through a class member function (displaydetails)

member functions have full access to private data members so there's no need to have any parameters in the display details.

so your code should look like this:

ACCOUNT.H
Code:
// Account.h
//Class File

using namespace std;

class Account
{
private:

int nAccountNumber;
char strAccName[50];

public:

void SetAccountNumber(int);
void setHolderName(char *);

void displayDetails(char *, int);
int getAccountNumber(void);
char * getHolderName(void);

};

//Actual implementation of class

int Account::getAccountNumber(void)
{
return nAccountNumber;
}

char * Account::getHolderName(void)
{
return strAccName;
}

void Account::SetAccountNumber(int dAccNum)
{
nAccountNumber = dAccNum;
}

void Account::setHolderName(char * accName)
{
strcpy(strAccName,accName);
}

[b]void Account::displayDetails()
{
cout << "Account name : " << strAccName;
cout << "Account number : " << nAccountNumber;
}[/b]





MAIN
Code:
main()
{
      cout << "Bank Account" << endl << endl;

      //Creating an object of class account.  

      Account Steve;

      //Setting values

     Steve.SetAccountNumber(100);
     Steve.setHolderName("Ste");

     //displaying details of the account.
     Steve.displayDetails();
}

you should also have a look at getting into the habbit of using default and parameterised constructors to initalise an object of a class when it's created.
 
Hiya,

Sorry for not replying sooner, been away since I posted!

That code above doesn't work, the error im getting is;

'prototype for `void Account::displayDetails()' does not match any in class `Account''

:s
 
Back
Top Bottom