Gary Wessle wrote:
Quote:
Hi
>
I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of 'Account' with no type
>
I cannot find out why. can you please help?
>
thanks
>
// User.h
#ifndef USER_H
#define USER_H
#include "Account.h"
>
class User{
public:
User();
// ~User();
Account* getAccountWithId(const int accId); //line 9
};
#endif // USER_H
>
//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
>
class Account {
public:
Account();
// ~Account();
};
>
#endif // ACCOUNT_H
|
In order for the compiler to successfully generate any of the above
class types, you need to implement those 2 def ctors since you've
overriden the compiler's ability to generate and implement them for
you. Hence the error.
So:
// User.cpp
#include "User.h"
User::User()
{
}
Account* User::getAccountWithId(const int accId)
{
return 0; // for now
}
// Account.cpp
#include "Account.h"
Account::Account()
{
}
And in the case you need User::getAccountWithId(...) to respect
constant-correctness, and depending on the container employed, use a
const reference instead:
const Account& User::getAccount(const int accId) const
{
return vacc.at(accId); // vector of accounts or whatever
}
If you prefer for some unknown reason to do it with dumb pointers, at
least protect the returned pointer:
Account* const User::getAccount(const int accId) const
{
return 0; // for now
}